如何在usercontrol c#winform中设置标签文本
如何在usercontrol c#winform中设置标签文本,c#,winforms,user-controls,label,C#,Winforms,User Controls,Label,我将设计一个userControl,它包含一个文本框和一个标签。 如何设置标签文本的公共属性 这是我的代码: public partial class CurrencyTextBoxWithLable : UserControl { public CurrencyTextBoxWithLable() { InitializeComponent(); } private string _lblText; public string
我将设计一个userControl,它包含一个文本框和一个标签。 如何设置标签文本的公共属性 这是我的代码:
public partial class CurrencyTextBoxWithLable : UserControl
{
public CurrencyTextBoxWithLable()
{
InitializeComponent();
}
private string _lblText;
public string LabelText
{
get
{
return _lblText;
}
set
{
_lblText = value;
}
}
}
但它不起作用。。。
任何帮助都将不胜感激
设计代码:
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(6, 6);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = this.LabelText;//Oooppsss!
}
它不起作用,因为您正在覆盖属性中的值。在设计器文件中,您将调用此:
this.label1.Text = this.LabelText;
查看getter,它返回\u lblText
值:
get
{
return _lblText;
}
由于未初始化,\u lblText
的值为空字符串(“”)。尝试将\u lblText
的值设置为某个初始值,然后再次运行代码。例如,添加以下内容:
private string _lblText = "Label1";
编辑:
将标签添加到表单时,如下所示:
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(46, 17);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
因此,“label1”的值与属性中的值相同。当您在属性中更改它时,比如说测试标签字符串
,设计器的值为:
this.label1.Test = "Test label string";
如果标签对您不起作用,请尝试再次删除和添加标签。您应该能够再次通过属性更改其值。这有什么问题。您是如何尝试访问标签的?“它不起作用”…您是什么意思?什么不起作用?“它不起作用”意味着当我在“属性”中更改“LabelText”属性时,标签的文本不会更改。或者当它初始化时,它没有文本我试过了,但它只是初始化了标签。如何从“属性”面板设置LabelText属性。@mohammadboluki不确定我是否得到它,默认情况下,您可以从“属性”面板更改它的值。检查编辑answer@mohammadboluki哦,我想我现在明白了,您正试图通过属性面板设置变量的值。用VisualStudio的设计是不可能的。Visual studio属性面板生成设计器文件,您不能更改其工作方式。