110,545
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
LableEdit lableEdit = new LableEdit();
this.Controls.Add(lableEdit);
}
}
public class LableEdit : UserControl
{
private Label _lbl;
private TextBox _txt;
public LableEdit()
{
_lbl = new Label();
_lbl.Dock = DockStyle.Fill;
_lbl.Click += LableClick;
_lbl.Visible = true;
_lbl.Text = "SocketUp";
_txt = new TextBox();
_txt.Dock = DockStyle.Fill;
_txt.KeyUp += TextboxKeyUp;
_txt.Visible = false;
_txt.BorderStyle = BorderStyle.None;
this.Controls.Add(_lbl);
this.Controls.Add(_txt);
}
private void LableClick(object sender, EventArgs e)
{
_lbl.Visible = false;
_txt.Visible = true;
_txt.Text = _lbl.Text;
_txt.Focus();
}
private void TextboxKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
_lbl.Visible = true;
_txt.Visible = false;
_lbl.Text = _txt.Text;
}
}
}
}