3

編集可能なラベルコントロールを作成する方法を知っている人はいますか?ユーザーがラベルを編集できるようにする必要があります(スタイル情報の一部も変更できます)が、オンラインのどこにも役立つ情報が見つかりませんでした。

どんな助けでも大歓迎です

ありがとうございました

4

4 に答える 4

3

カスタム コントロールを作成できます (多少の作業が必要です)。コントロールは内部に標準のラベル コントロールを持つことができ、ユーザーがラベルをクリックすると (または編集モードに移行すると)、テキスト ボックス コントロールをインスタンス化して、ラベル コントロールがあった場所に表示することができます。そのため、ユーザーは、ラベル コントロールがテキスト ボックスに「変換」されているように錯覚します。ユーザーはテキスト ボックス内のラベル テキストを編集できます。編集が終了したら、テキスト ボックスを非表示にしてラベル テキストに変更を適用するだけです。

スタイルも編集する必要がある場合は、1 つのテキストボックスではなく、編集可能なすべての設定を含むパネルを表示する必要があります。

于 2010-02-22T09:39:45.397 に答える
1

TextBoxコントロールを使用するだけで、必要なときに編集できなくなります。readOnlyプロパティをtrueに変更するだけです。

良い1日を

于 2010-02-22T08:32:20.030 に答える
1

間接的に作ってください。

たとえば、ダブルクリック イベントを登録し、ユーザーが新しい名前を入力できる TextBox を使用してボーダレス フォームを表示します。例:

ラベルエディタ

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

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 に答える
0

スタイル プロパティも編集できるようにする場合は、フォームでPropertyGridコントロールを使用できます (コントロールのプロパティを編集するために Visual Studio から使用するものと同じです)。

于 2010-02-22T08:35:04.107 に答える