情况是这样的:我有多个文本框。在发生 textChanged 事件时,文本框应存储在数组中,以便我可以在其他功能中使用它。
private void txt_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox t;
t = (TextBox)sender;
}
现在我有了负责该事件的文本框。现在我必须将这个和更多存储在一个数组中,以便可以在另一个函数的其他地方访问这些。
如果你愿意,你可以把它放在一个列表中。不知道为什么你真的想这样做......
List<TextBox> txtbxList = new List<TextBox>();
private void txt_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox t;
t = (TextBox)sender;
txtbxList.Add(t);
}
我不知道您为什么需要将 TextBoxes 存储在 List 或 Array 中,但您可以使用通用 List 来实现。
表示可以通过索引访问的对象的强类型列表。提供搜索、排序和操作列表的方法。
List<TextBox> myTextBoxes = new List<TextBox>();
// Add a TextBox
myTextBoxes.Add(myTextBox);
// get a TextBox by Name
TextBox t = myTextBoxes.Where(x => x.Name == "TextBoxName").FirstOrDefault();
假设您要存储 TextBoxes 中的文本,您可以使用这样的字典:
private Dictionary<string, string> dictionary = new Dictionary<string, string>();
private void txt_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = (TextBox)sender;
string key = textBox.Name;
string value = textBox.Text;
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
}
else
{
dictionary[key] = value;
}
}