日期:2014-05-18 浏览次数:20971 次
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;
            }
        }
    }
}
------解决方案--------------------
DotnetBar 的textBox有个属性能实现楼主的要求!你看看