views:

180

answers:

4

Hi All

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

A: 

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

amr osama
Thank you. However, this is not a good approach. First of all, the TextBox cannot resize automatically, second, it does not provide support for Transparency etc...
lucifer
+1 for explaining why not simply use a textbox, but why not include these two good points about label v. textbox in your question. it would add to the usefulness of the question
hawbsl
+1  A: 

If you want to give the possibility to edit style properties as well, you might use a PropertyGrid control on your form (the same you use from Visual Studio to edit the controls' properties).

Paolo Tedesco
Not exactly what I was looking for... But interesting, and I may just use it as a last resort. Thank you :)
lucifer
A: 

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.

ravinsp
+1  A: 

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;
                }
            }
        }
    }
}
Oliver