我想写自己做一个像微软和QQ里修改信息的“label”即放上去有button的效果,点击会变成“textbox”失去焦点又变回“label”,如图:
但是没什么思路,难道动态改变控件?要是从一个控件继承,那又如何实现其他的效果,不想没什么思路就在那乱写,所以还是请高手解答~~

解决方案 »

  1.   

    自己自定一个控件吧。判断鼠标是否在上面,如果是的话,DGI画出来按钮,不是的话,
      

  2.   

    用JS
    var _input = document.createElement("INPUT"); 
                    _input.style.width=40;
                    _input.value="";
      

  3.   

    仅供参考
    不完善,你自己调试改改吧。
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;namespace wintest.Controls
    {
    /// <summary>
    /// Description of LabelButton.
    /// </summary>
    public class LabelButton : System.Windows.Forms.UserControl
    {
    private Label label;
    private Button button;
    private TextBox textBox;

    private String _text;

    private bool _modified;

    public LabelButton()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    }

    #region Windows Forms Designer generated code
    /// <summary>
    /// This method is required for Windows Forms designer support.
    /// Do not change the method contents inside the source code editor. The Forms designer might
    /// not be able to load this method if it was changed manually.
    /// </summary>
    private void InitializeComponent() {
    // 
    // LabelButton
    // 
    this.BackColor = System.Drawing.SystemColors.Control;
    this.Name = "LabelButton";
    this.Size = new System.Drawing.Size(100, 23);

    // Institance Controls
    this.label = new Label();
    this.label.Size = this.Size;
    this.label.TextAlign = ContentAlignment.MiddleLeft;
    this.label.Dock = DockStyle.Fill;

    this.label.MouseEnter += new System.EventHandler(this.LabelMouseEnter);

    this.Controls.Add(this.label);

    //
    this.button = new Button();
    this.button.Size = this.Size;
    this.button.TextAlign = ContentAlignment.MiddleLeft;
    this.button.Dock = DockStyle.Fill;

    this.button.MouseLeave += new System.EventHandler(this.ButtonLeaveEnter);
    this.button.Click += new System.EventHandler(this.ButtonClick);

    this.Controls.Add(this.button);

    //
    this.textBox = new TextBox();
    this.textBox.Size = this.Size;
    this.textBox.Dock = DockStyle.Fill;

    this.textBox.Leave += new System.EventHandler(this.TextBoxLeave);
    this.textBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxKeyDown);

    this.Controls.Add(this.textBox);

    //
    this._modified = false;

    }
    #endregion

    public String TextValue
    {
    get {
    return this._text;
    }
    set {
    this.label.Text = value;
    this.button.Text = value;
    this.textBox.Text = value;
    this._text = value;
    }
    }

    //Event Handle
    void LabelMouseEnter(object sender, System.EventArgs e)
    {
    this.label.Visible = false;
    this.button.Visible = true;
    this.textBox.Visible = false;
    }

    void ButtonLeaveEnter(object sender, System.EventArgs e)
    {
    if(!_modified) {
    this.label.Visible = true;
    this.button.Visible = false;
    this.textBox.Visible = false;
    }
    }

    void ButtonClick(object sender, System.EventArgs e)
    {
    this._modified = true;
    this.label.Visible = false;
    this.button.Visible = false;
    this.textBox.Visible = true;
    this.textBox.Focus();
    }

    void TextBoxLeave(object sender, System.EventArgs e)
    {
    this._modified = false;
    this.label.Visible = true;
    this.button.Visible = false;
    this.textBox.Visible = false;
    this.TextValue = this.textBox.Text;
    }

    void TextBoxKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    if(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Escape) {
    TextBoxLeave(sender,null);
    e.Handled = false;
    }
    }
    }
    }