我正在尝试使用Windows应用来计算GPA。用户可以输入主题数量,应用程序创建与主题数量一样多的文本框。但我无法从文本框中获取值。这是我正在尝试的代码
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < int.Parse(textBox1.Text); i++)
{
Label supjects = new Label();
supjects.Text = "supject " + (i+1) ;
supjects.Location = new System.Drawing.Point(60, 70+i*41);
supjects.Name = "supject" + i;
this.Controls.Add(supjects);
TextBox fullMark = new TextBox();
fullMark.Location = new System.Drawing.Point(162, 70 + i * 41);
fullMark.Size=new System.Drawing.Size(57, 24);
fullMark.Name = "fullMark" + i;
this.Controls.Add(fullMark);
TextBox yourMark = new TextBox();
yourMark.Location = new System.Drawing.Point(272, 70 + i * 41);
yourMark.Size = new System.Drawing.Size(57, 24);
yourMark.Name = "yourMark" + i;
this.Controls.Add(yourMark);
}
答案 0 :(得分:0)
声明像
这样的字典Dictionary<string, TextBox> textBoxes = new Dictionary<string, TextBox>();
将循环中的文本框添加到其中。
textBoxes.Add("yourMark" + i, yourMark);
现在您可以像
一样使用它var text = textBoxes["yourMark2"].Text
答案 1 :(得分:0)
for (int i = 0; i < int.Parse(textBox1.Text); i++)
{
Label l = this.Controls.Find("supject" + i,true)[0] as Label;
l.Text = "yes";
//whatever
}
找到YourMark,它 标签l = this.Controls.Find(&#34; YourMark&#34;,true)[0]为标签;