简体   繁体   English

如何通过将myLable1.Text调用为变量来更改它

[英]How can I change myLable1.Text by calling it as a variable

Let me preface this by saying I have only been learning c# a few days. 首先,我说我只是在学习C#,这是我的序言。 I have no prior programming experience other than basic JavaScript so I'm still unsure on a lot the correct terminology. 除了基本的JavaScript之外,我没有其他编程经验,因此我仍然不确定很多正确的术语。 On to the question... 关于这个问题...

Let's say I have 10 labels; 假设我有10个标签; myLabel1, myLabel2, myLabel3 etc and I have a variable called i. myLabel1,myLabel2,myLabel3等,我有一个名为i的变量。

So how can I change the .Text of myLabel by switching out the end number for the variable i? 那么,如何通过切换变量i的结束号来更改myLabel的.Text? I tried making a new string: 我尝试制作一个新的字符串:

string labelNumber = "myLable" + Convert.ToString(i);

and then: 接着:

lableNumber.Text = "some text";

Clearly this doesn't work because .Text is not a known method on lableNumber. 显然,这是行不通的,因为.text不是lableNumber的已知方法。

C# as most other compiled languages will not let you do that as easily as many scripting languages. 与大多数其他编译语言一样,C#不能像许多脚本语言一样让您轻松地做到这一点。

If you want to access your controls by a string you need to collect them in a Dictionay<string, Control> or, if you only care about Labels , a Dictionay<string, Label> : 如果要通过字符串访问控件,则需要将它们收集在Dictionay<string, Control>或者,如果仅关心Labels ,则需要将其收集在Dictionay<string, Label>

Dictionary<string, Label> labels = new Dictionary<string, Label>();

// you can do this in a loop over i:
Label newLabel = new Label();
newLabel.Name = "myLabel" + Convert.ToString(i);

// maybe set more properties..
labels.Add(newLabel.Name, newLabel );     // <-- here you need the real Label, though!
flowLayoutPanel1.Controls.Add(newLabel )  // <-- or wherever you want to put them

Now you can access each by its name as string: 现在,您可以按名称访问每个字符串:

labels["myLabel3"].Text = "hi there";

Note that to add them to the Dictionary , (or a List<T> if you are happy with accessing them by an index,) you need to do it when you create them in a loop as you can't access them later; 请注意,将它们添加到Dictionary中(或者,如果您愿意通过索引访问它们,则添加到List<T> ),在循环创建它们时需要这样做,因为以后将无法访问它们。 at least not without reflection , which imo is overkill for the situation.. 至少不是没有反思 ,这对于这种情况来说太过猛了。

Also note the difference between the variable name which not a string but a token for the compiler and its Name property , which is a string but not meant to identify the variable as it need not be unique and can be changed at any time.. 还请注意 ,变量名不是字符串,而是编译器标记,而变量名 Name 属性 ,后者字符串,但并不意味着要标识变量,因为它不必唯一 ,可以随时更改

I think you are trying to do something like this: 我认为您正在尝试执行以下操作:

// Create N label xontrols
Labels[] labels = new Labels[n];

for (int i = 0; i < n; i++)
{
    labels[i] = new Label();
    // Here you can modify the value of the label which is at labels[i]
}

// ...

labels[2] .Text = "some text";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过调用方法将 multiple.text 值更改为“”? - How can I change multiple .text values to “”, through calling a method? 如何将文本框文本更改为字符串变量并将其发送给另一个类并在sendkey中使用它 - How can i change textbox text to string variable and send it another class and use it in sendkey 如何在检查器中更改公共变量? - How can I change a public variable in the Inspector? 如何更改Word中文本的方向? - How can I change direction of the text in Word? 如何更改按钮文本的颜色? - How can I change the color of the text of a button? 如何使数字变量不接受文本变量 - How can I make a digital variable not accept the text variable 如何在带有 SpeechRecognizer EventArgs 变量的 TextBox 中放置换行符 - How can i put line change in a TextBox with a SpeechRecognizer EventArgs variable 如何在Global.asax之外更改应用程序变量? - How can I change application variable outside Global.asax? 如何从静态方法中更改实例变量? - How can I change an instance variable from within a static method? 如何让文本框在某个文本之间打印一个变量? - How can I make a textBox print a variable in between a certain text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM