答案 0 :(得分:1)
您应该创建自己的UserControl,其中包含一个标签和一个文本框。实现您想要的功能。
我已经创建了一个示例usercontrol来让您了解它...
<强>更新强>
按照以下步骤使用此自定义控件。
右键点击您的项目,然后点击&#39;添加 - >用户控件&#39;
将其命名为EditableLabelControl&#39;然后单击“添加”。
转到&#39; EditableLabelControl.Designer.cs&#39;并替换下面的部分类 Code1 。
然后转到&#39; EditableLabelControl.cs&#39;并通过下面的 Code2 替换第二个分部。
构建您的解决方案。
您应该可以将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
}
Code2
public partial class EditableLabelControl : UserControl
{
public EditableLabelControl()
{
InitializeComponent();
}
private void label1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Visible = true;
textBox1.BringToFront();
textBox1.Focus();
}
private void textBox1_Leave(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
textBox1.Visible = false;
textBox1.SendToBack();
}
}
只需将此EditableLabelControl添加到您的表单即可。
答案 1 :(得分:1)
这是一个快速的解决方案:
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
}
在TextBox
中嵌入 Label
。将Label
设置为足够大,以便预期的用户输入!
预计不会嵌入其他控件。
如果您不止一次需要它,请考虑从此代码创建自定义可编辑标签!
请注意,要工作,您需要点击焦点可以去的地方,而不仅仅是周围的空白区域。为了解决您可以为周围空间编写Click事件的问题,可能是这样的:
private void unClickLabel(object sender, EventArgs e) {label1_Click(null, null);}
在表单构造函数中,将此添加到所有不会引起焦点的“外部”控件,例如Form
或TabPage
或PicureBox
或{{1} }:
Panel
请注意,新的public Form1()
{
InitializeComponent();
this.Click += unClickLabel;
tabPage1.Click += unClickLabel;
pictureBox1.Click += unClickLabel;
..
}
文字不会持久程序运行!要允许您需要将其存储在某些外部用户设置中并在启动时加载它们。
答案 2 :(得分:0)
尝试使用{{1}}代替Label。
答案 3 :(得分:0)
如果您希望拥有不可更改的文本字段,请使用TextBox控件并将ReadOnly
属性设置为true
。但是,请记住,TextBox不会自动调整大小,也不支持透明度。
您可以将属性BackColor
更改为Control
,将BorderStyle
更改为none
,这将显示为标签。
通过更多努力,您可以创建UserControl并在Lable和TextBox之间切换。
答案 4 :(得分:0)
我建议改用textBox。
用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;
}
}