3

有人知道我该如何创建可编辑标签控件吗?我需要我的用户能够编辑标签(同时更改其样式信息的一部分),但在网上任何地方都找不到有用的信息。

任何帮助表示赞赏

谢谢

4

4 回答 4

3

您可以创建自定义控件(需要一些工作)。控件内部可以有一个标准的标签控件,当用户单击标签(或以某种方式进入编辑模式)时,您可以实例化一个文本框控件并将其显示在标签控件所在的位置。所以用户会得到标签控件被“转换”为文本框的错觉。用户可以在文本框中编辑标签文本,编辑完成后,您所要做的就是隐藏文本框并将更改应用于标签文本。

如果您还需要编辑样式,则必须显示一个包含所有可编辑设置的面板,而不是一个单独的文本框。

于 2010-02-22T09:39:45.397 回答
1

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;
                }
            }
        }
    }
}
于 2010-02-22T10:08:17.170 回答
1

您可以简单地使用 TextBox 控件,当您需要它们时无法对其进行编辑。只需将它的 readOnly 属性设置为 true。

祝你今天过得愉快

于 2010-02-22T08:32:20.030 回答
0

如果您还想提供编辑样式属性的可能性,您可以在表单上使用PropertyGrid控件(与您在 Visual Studio 中用于编辑控件属性的相同)。

于 2010-02-22T08:35:04.107 回答