[英]C# label text not change in method
I am new in the middle of the c#. 我是C#的新手。
I have a problem, I want to change the text of a textboxbut I do not happen I have to look on the forum but nothing helped me. 我有一个问题,我想更改文本框的文本,但是我没有碰到我必须去看论坛,但是没有任何帮助。 Your help will be welcome.
我们将竭诚为您服务。
create textbox: 创建文本框:
TextBox _DysplayRulsR = new TextBox();
_DysplayRulsR.Text = "Hello message";
_DysplayRulsR.VerticalAlignment = VerticalAlignment.Top;
_DysplayRulsR.HorizontalAlignment = HorizontalAlignment.Center;
_DysplayRulsR.Background = Brushes.Transparent;
_DysplayRulsR.BorderBrush = Brushes.Transparent;
_DysplayRulsR.IsReadOnly = true;
_DysplayRulsR.Foreground = Brushes.White;
_DysplayRulsR.BorderThickness = new Thickness(0, 0, 0, 0);
_DysplayRulsR.Margin = new Thickness(0, 15, 0, 0);
_DysplayRulsR.Width = 900;
_DysplayRulsR.Height = 300;
_DysplayRulsR.FontSize = 30;
_DysplayRulsR.TextAlignment = TextAlignment.Center;
_ContentGrid.Children.Add(_DysplayRulsR);
_ContentGrid = Grid _ContentGrid =网格
method change textbox text: 方法更改文本框文本:
public static void UpdateRules(string status)
{
_DysplayRulsR.Text = "done";
}
The label is displayed well the method and well used but the text of the textbox_DysplayRuls does not want to change. 标签显示的方法和用法都很好,但是textbox_DysplayRuls的文本不想更改。
Sorry for my approximate English I'm really not strong for translation. 对不起,我的英语差不多,我的翻译真的不强。
You need to change the Text
property of your TextBox . 您需要更改TextBox的
Text
属性。
Example: 例:
public static void UpdateRules(string status)
{
_TextRules.Text = "done";
}
However you did show _DysplayRuls
as the name of your TextBox in your above initialization code. 但是,您确实在上面的初始化代码
_DysplayRuls
显示为TextBox的名称。 Make sure you replace _TextRules
in my example with whatever TextBox you want to change. 确保将
_TextRules
中的_TextRules
替换为要更改的任何TextBox。
You need to assign a new value to the Text property of your _DysplayRuls TextBox instance. 您需要为_DysplayRuls TextBox实例的Text属性分配一个新值。
public static void UpdateRules(string status)
{
_DysplayRuls.Text = "done";
}
You have an issue with naming conventions. 您对命名约定有疑问。 This may seem like a nit, but in this case it is a clue to the overall problem.
这可能看起来很简单,但是在这种情况下,这是解决整个问题的线索。
This line: 这行:
TextBox _DysplayRulsR = new TextBox();
...smells bad. ...不好闻。 If a variable name begins with an underscore, it should be a member variable.
如果变量名称以下划线开头,则它应该是成员变量。 Some devs think the underscore is unnecessary, but that is a different point;
一些开发人员认为下划线是不必要的,但这是不同的。 if you're going to use them at all, they should not be used for local variables.
如果您要完全使用它们,则不应将它们用于局部变量。
This leads me to believe that this line: 这使我相信这一行:
public static void UpdateRules(string status)
{
_DysplayRulsR.Text = "done";
}
...is actually referencing a different textbox with a name of _DysplayRulsR
but at member, not local, scope. ...实际上引用的是名称为
_DysplayRulsR
的其他文本框,但位于成员作用域而非本地作用域。
So you have two options: 因此,您有两种选择:
Change this 改变这个
TextBox _DysplayRulsR = new TextBox();
to this: 对此:
_DysplayRulsR = new TextBox();
OR 要么
Add this to your first block of code: 将此添加到您的第一个代码块:
_DysplayRulsR.Name = "_DysplayRulsR";
And change this: 并更改此:
public static void UpdateRules(string status)
{
_DysplayRulsR.Text = "done";
}
to this: 对此:
public static void UpdateRules(string status)
{
_ContentGrid.Children.FindControl("_DysplayRulsR").Text = "done";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.