Editable Label Controls

末鹿安然 提交于 2019-11-30 19:31:31

问题


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


回答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.




回答2:


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




回答3:


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



回答4:


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).



来源:https://stackoverflow.com/questions/2309662/editable-label-controls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!