Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改标签上的文本';在运行时,用户自己的单击事件?_C#_Winforms - Fatal编程技术网

C# 更改标签上的文本';在运行时,用户自己的单击事件?

C# 更改标签上的文本';在运行时,用户自己的单击事件?,c#,winforms,C#,Winforms,我所需要的是一个字段,用户只需单击它,就可以根据自己的意愿对其进行重命名。我在这里使用标签作为控件,当用户单击它时,用户可以在标签中输入文本,当他在标签外单击时,输入的文本将保存为标签文本 尝试使用文本框代替标签 如果希望有一个不可更改的文本字段,请使用控件并将ReadOnly属性设置为true。但是,请记住,文本框不会自动调整大小,也不支持透明度 您可以将属性BackColor更改为Control,将BorderStyle更改为none,这将显示为标签 通过更多的努力,您可以创建一个UserC

我所需要的是一个字段,用户只需单击它,就可以根据自己的意愿对其进行重命名。我在这里使用标签作为控件,当用户单击它时,用户可以在标签中输入文本,当他在标签外单击时,输入的文本将保存为标签文本


尝试使用
文本框
代替标签

如果希望有一个不可更改的文本字段,请使用控件并将
ReadOnly
属性设置为
true
。但是,请记住,文本框不会自动调整大小,也不支持透明度

您可以将属性
BackColor
更改为
Control
,将
BorderStyle
更改为
none
,这将显示为标签


通过更多的努力,您可以创建一个UserControl,并在Lable和TextBox之间切换。

您应该创建自己的UserControl,其中包含一个标签和一个TextBox。如您所愿实现其功能

我创建了一个示例usercontrol,让您了解它

更新:

按照以下步骤使用此自定义控件

  • 右键单击项目并单击“添加->用户控制”

  • 将其命名为“EditableLabelControl”,然后单击“添加”

  • 转到“EditableLabelControl.Designer.cs”并替换下面的部分类Code1

  • 然后转到“EditableLabelControl.cs”,用下面的代码2替换第二个部分类

  • 构建您的解决方案

  • 您应该能够将EditableLabelControl添加到表单中(它将显示在工具箱中)

  • 代码1

     partial class EditableLabelControl
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    
        #region Component Designer generated code
    
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            this.label1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.label1_MouseClick);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Name = "textBox1";
            this.textBox1.TabIndex = 1;
            this.textBox1.Visible = false;
            this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
            // 
            // EditableLabelControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoSize = true;
            this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label1);
            this.Name = "EditableLabelControl";
            this.Size = new System.Drawing.Size(103, 23);
            this.ResumeLayout(false);
            this.PerformLayout();
    
        }
    
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
        #endregion
    }
    

    只需将此EditableLabelControl添加到表单中,它就会工作。

    我建议改用textBox

    用文本框替换标签

    //Set properties. Make the textBox looks like label
    yourTextBox.BackColor = SystemColors.Control;
    yourTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
    
    //Allow user to change text by double clicking it
    yourTextBox.DoubleClick += new EventHandler(this.doubleClick);
    
    //Do stuff when user double click it. Double click again to end editing.
    public void doubleClick(object sender, EventArgs e)
    {
        TextBox temp = (TextBox)sender;
        temp.ReadOnly = !temp.ReadOnly;            
        temp.DeselectAll();
        if (temp.ReadOnly)
        {
            //Make the textbox lose focus
            this.ActiveControl = null;
        }
    }
    

    这是一个快速解决方案:

    private void label1_Click(object sender, EventArgs e)
    {
        TextBox tb = null;
        if (label1.Controls.Count > 0)  // do we already have created our TextBox?
        {
            tb = ((TextBox)label1.Controls[0]);  // yes. set reference.
            // is it already visible? we got clicked from outside, so we hide it:
            if (tb.Visible) { label1.Text = tb.Text; tb.Hide(); return; };
        }
        else if (sender == null) return;  // clicked from outside: do nothing
             else  // we need to create the textbox
             {
               tb = new TextBox();
               tb.Parent = label1;     // add it to the label's Controls collection
               tb.Size = label1.Size;  // size it
               // set the event that ends editing when focus goes elsewhere:
               tb.LostFocus += (ss, ee) =>   { label1.Text = tb.Text; tb.Hide(); };
             }
    
        tb.Text = label1.Text;  // get current text
        tb.Show();              // display the textbox in place
    }
    
    文本框
    嵌入
    标签
    。设置
    标签的样式,使其足够大,可以容纳预期的用户输入

    它希望没有其他控件嵌入其中

    如果您不止一次需要考虑从这个代码创建一个自定义可编辑标签! 请注意,要工作,您需要单击焦点所在的位置,而不仅仅是周围的空白区域。要解决此问题,您可以为周围空间编写单击事件代码,可能如下所示:

    private void unClickLabel(object sender, EventArgs e) {label1_Click(null, null);}
    
    在表单构造函数中,将其添加到所有不会聚焦的“外部”控件,如
    表单
    选项卡页
    PicureBox
    面板

    public Form1()
    {
       InitializeComponent();
       this.Click += unClickLabel;   
       tabPage1.Click += unClickLabel;   
       pictureBox1.Click += unClickLabel;
       ..
    }
    

    请注意,新的
    标签
    文本将不会持续程序运行!要允许,您需要将其存储在一些外部用户设置中,并在启动时加载。

    能否添加您的示例代码。。所以我们可以确定你到底需要什么。。。更多信息。我认为您的问题是无法编辑标签中的文本。正如名字所说,它只是一个标签。你需要文本框或Inputform或其他东西。也许你可以让你自己的文本框看起来像标签控件。好吧,那么最好使用其他控件?你的目标是什么:Winforms、WPF、ASP。。?始终正确标记您的问题!EditableLabelControl?不。只需将上述类添加到项目中,您就可以将此控件添加到表单中。在类型为“atHotel.EditableLabelControl”的声明中缺少部分修饰符;此类型的另一个部分声明存在于C:\Users\Danish\Desktop\atHotel11-4-16\EditableLabelControl.cs 12 18 atHotelpublic EditableLabelControl(){InitializeComponent();}private void InitializeComponent()中{我可以更改此函数名吗?我已更新了答案。现在看看它是否适用于您。它是否透明?您不能将textBox的背景色设置为透明。但将背景色设置为SystemColor。控件意味着它与窗体的颜色相同。因此它看起来是“透明的”我是一名初级开发人员,如果你详细解释一下会很有帮助的。请帮助我。我已经添加了一些评论,也更新了一些答案。如果你有问题,请写下来!
    public Form1()
    {
       InitializeComponent();
       this.Click += unClickLabel;   
       tabPage1.Click += unClickLabel;   
       pictureBox1.Click += unClickLabel;
       ..
    }