C# 如何根据名称中的索引Id访问一组文本框
我的C# 如何根据名称中的索引Id访问一组文本框,c#,winforms,loops,C#,Winforms,Loops,我的表单中有16个文本框,其名称的后缀分别为1到16 i、 e.16个测试框为名称TextBox1,'TextBox2。。。。一直到第16个,名为TextBox16` 我想循环阅读这16个文本框的内容,并根据特定条件修改Ith文本框的内容或属性 如何执行此操作?如果您使用WinForms,最简单的方法是将文本框引用存储在数组中的窗口构造函数中: TextBox[]data=newtextbox[16]{textBox1,textBox2,[…],textBox16} 然后您可以使用for循环来访
表单中有16个文本框
,其名称的后缀分别为1到16
i、 e.16个测试框为名称TextBox1
,'TextBox2。。。。一直到第16个,名为
TextBox16`
我想循环阅读这16个文本框的内容,并根据特定条件修改I
th文本框的内容或属性
如何执行此操作?如果您使用WinForms,最简单的方法是将文本框引用存储在数组中的窗口构造函数中:
TextBox[]data=newtextbox[16]{textBox1,textBox2,[…],textBox16}代码>
然后您可以使用for循环来访问它们。如果您使用WinForms,最简单的方法是将文本框引用存储在数组中,存储在窗口的构造函数中:
TextBox[]data=newtextbox[16]{textBox1,textBox2,[…],textBox16}代码>
然后您可以使用for循环访问它们。您可以尝试以下方法:
Dictionary<string, string> yourValues = new Dictionary<string, string>();
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
yourValues.Add(((TextBox)x).Name, ((TextBox)x).Text);
}
}
List<TextBox> textboxes = this.Controls.OfType<TextBox>().ToList();
foreach (TextBox box in textboxes)
{
box.Text = "something";
}
Dictionary yourValues=newdictionary();
foreach(此.Controls中的控件x)
{
如果(x是文本框)
{
添加(((文本框)x).Name,((文本框)x).Text);
}
}
注意:关于您未来的问题,请提供更多信息,并使您的问题更清楚。您可以尝试以下方法:
Dictionary<string, string> yourValues = new Dictionary<string, string>();
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
yourValues.Add(((TextBox)x).Name, ((TextBox)x).Text);
}
}
List<TextBox> textboxes = this.Controls.OfType<TextBox>().ToList();
foreach (TextBox box in textboxes)
{
box.Text = "something";
}
Dictionary yourValues=newdictionary();
foreach(此.Controls中的控件x)
{
如果(x是文本框)
{
添加(((文本框)x).Name,((文本框)x).Text);
}
}
注意:关于您未来的问题,请提供更多信息,并使您的问题更清楚。我将尝试使用Linq查找和修改:
using System.Linq;
//...
int x = 5; //number of textbox to search
var textbox = this.Controls.OfType<TextBox>().Single(tb => tb.Name.EndsWith(x.ToString()));
textbox.Text = "abc";
使用System.Linq;
//...
int x=5//要搜索的文本框的数目
var textbox=this.Controls.OfType().Single(tb=>tb.Name.EndsWith(x.ToString());
textbox.Text=“abc”;
如果必须循环遍历表单中的所有texbox,可以执行以下操作:
Dictionary<string, string> yourValues = new Dictionary<string, string>();
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
yourValues.Add(((TextBox)x).Name, ((TextBox)x).Text);
}
}
List<TextBox> textboxes = this.Controls.OfType<TextBox>().ToList();
foreach (TextBox box in textboxes)
{
box.Text = "something";
}
List textboxs=this.Controls.OfType().ToList();
foreach(文本框中的文本框)
{
box.Text=“某物”;
}
我会尝试使用Linq查找和修改:
using System.Linq;
//...
int x = 5; //number of textbox to search
var textbox = this.Controls.OfType<TextBox>().Single(tb => tb.Name.EndsWith(x.ToString()));
textbox.Text = "abc";
使用System.Linq;
//...
int x=5//要搜索的文本框的数目
var textbox=this.Controls.OfType().Single(tb=>tb.Name.EndsWith(x.ToString());
textbox.Text=“abc”;
如果必须循环遍历表单中的所有texbox,可以执行以下操作:
Dictionary<string, string> yourValues = new Dictionary<string, string>();
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
yourValues.Add(((TextBox)x).Name, ((TextBox)x).Text);
}
}
List<TextBox> textboxes = this.Controls.OfType<TextBox>().ToList();
foreach (TextBox box in textboxes)
{
box.Text = "something";
}
List textboxs=this.Controls.OfType().ToList();
foreach(文本框中的文本框)
{
box.Text=“某物”;
}
根据您指定的内容,最简单的方法是使用Linq
假设您有3个TextBox
es:
// 1st -> which is named meTextBox1
// 2nd -> which is named meTextBox2
// 3rd -> which is named meTextBox3
正如你从上面所看到的,每一行只因数字不同而不同(索引..你想叫什么就叫什么)
现在,您可以进行如下基本“查询”:
const string TB_NAME = "meTextBox{0}";
您现在可以假定,这将在string.Format
方法中使用。现在,要检索所需的TextBox
,您只需做Linq
语句:
string boxName = string.Format(TB_NAME, 7); // retrieve 7th text box
TextBox tBox = Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == boxName);
要使用上述方法,您可以这样调用它:
public class MeForm : Form
{
//.. your code here ..
void meImaginaryMethodToRetrieveTextBox()
{
int meRandomIndex = 7;
TextBox tb = ByIndex(meRandomIndex, this);
// rest of the code...
}
}
根据您指定的内容,最简单的方法是使用Linq
假设您有3个TextBox
es:
// 1st -> which is named meTextBox1
// 2nd -> which is named meTextBox2
// 3rd -> which is named meTextBox3
正如你从上面所看到的,每一行只因数字不同而不同(索引..你想叫什么就叫什么)
现在,您可以进行如下基本“查询”:
const string TB_NAME = "meTextBox{0}";
您现在可以假定,这将在string.Format
方法中使用。现在,要检索所需的TextBox
,您只需做Linq
语句:
string boxName = string.Format(TB_NAME, 7); // retrieve 7th text box
TextBox tBox = Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == boxName);
要使用上述方法,您可以这样调用它:
public class MeForm : Form
{
//.. your code here ..
void meImaginaryMethodToRetrieveTextBox()
{
int meRandomIndex = 7;
TextBox tb = ByIndex(meRandomIndex, this);
// rest of the code...
}
}
为什么不使用文本框数组?试试这个。进行语法更正并添加示例,以帮助观众更好地理解所问问题,从而提供有用的答案/解决方案。为什么不使用一系列文本框呢?试着这样做。进行语法更正并添加示例,以帮助观众更好地理解所问问题,从而提供有用的答案/解决方案。