Editable Label Controls

14,323

Solution 1

You can create a custom control (requires some work). The control can have a standard label control internally and when the user clicks the label (or goes to the editing mode somehow) you can instantiate a textbox control and show it where the label control was. So the user would get the illusion of the label control being "converted" to a textbox. The user can edit the label text in the textbox and when the editing finished, all you have to do is hide the textbox and apply the changes to the label text.

If you need to edit styles as well, you will have to display a panel with all the editable settings on it instead of a one single textbox.

Solution 2

Make it indirectly.

E.g. register the double click event and show a borderless form with a TextBox, where the user can enter the new name. Example:

LabelEditor

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class LabelEditor : Form
    {
        private System.Windows.Forms.TextBox textBox;

        public LabelEditor()
        {
            InitializeComponent();

            this.textBox = new System.Windows.Forms.TextBox();

            this.textBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textBox.Location = new System.Drawing.Point(0, 0);
            this.textBox.Name = "textBox";
            this.textBox.Size = new System.Drawing.Size(100, 20);
            this.textBox.TabIndex = 0;
            this.textBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);

            this.AutoSize = true;
            this.ClientSize = new System.Drawing.Size(100, 20);
            this.Controls.Add(textBox);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MinimumSize = new System.Drawing.Size(100, 20);
            this.Name = "LabelEditor";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
        }

        public override string Text
        {
            get
            {
                if (textBox == null)
                    return String.Empty;

                return textBox.Text;
            }
            set
            {
                textBox.Text = value;
                ResizeEditor();                
            }
        }

        private void ResizeEditor()
        {
            var size = TextRenderer.MeasureText(textBox.Text, textBox.Font);
            size.Width += 20;

            this.Size = size;
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    DialogResult = DialogResult.Cancel;
                    this.Close();
                    break;
                case Keys.Return:
                    DialogResult = DialogResult.OK;
                    this.Close();
                    break;
            }
        }
    }
}

Form

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        private Label EditableLabel;

        public Form1()
        {
            InitializeComponent();

            this.EditableLabel = new System.Windows.Forms.Label();

            this.EditableLabel.AutoSize = true;
            this.EditableLabel.Location = new System.Drawing.Point(102, 81);
            this.EditableLabel.Text = "Click me to change...";
            this.EditableLabel.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.LabelMouseDoubleClick);

            this.Controls.Add(this.EditableLabel);
        }

        private void LabelMouseDoubleClick(object sender, MouseEventArgs e)
        {
            var label = sender as Label;

            if (label != null)
            {
                var editor = new LabelEditor();

                editor.Location = label.PointToScreen(new Point(e.X + 5, e.Y + 5));
                editor.Text = label.Text;

                if (DialogResult.OK == editor.ShowDialog())
                {
                    label.Text = editor.Text;
                }
            }
        }
    }
}

Solution 3

you can simply use TextBox control and when you need them not be able to edit it. just turn it's readOnly property to true.

Have a nice day

Share:
14,323
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    Does anybody know how I could go about creating an Editable Label Control? I need my users to be able to Edit Labels (Also change parts of its style info), but have found no helpful info anywhere online.

    Any help at all is appreciated

    Thank you