Load Textboxes from array
My form has several rows of textboxes. The first row is named txtL000 through txtL009, the second row txtL100 through txtL109, and so on. Under each of these rows is another row of textboxes named txtT000 through txtT009, etc. When the user opens the form, I want to load the textboxes named txtL... wi开发者_如何学Goth the strings in an array, depending on what's in the corresponding textbox named txtT..0. For instance, if txtT000 has "land" in it, I want to load txtL000 through txtL009 with the strings from array arrLand. If it has "point" in it, I want to load txtL000 through txtL009 with the strings from array arrPoint. What's the most efficient way to do this?
The simplest way I can think of is to use a Dictionary to store the arrays:
//Use any collection you prefer instead of 'List' if you want.
Dictionary<String, List> arrays = new Dictionary<String, List>();
private void OnTextChanged(object source, EventArgs e)
{
if (source == txtT000)
loadTextBoxes(txtT000.Text, txtL000, txtL001, txtL002,
txtL003, txtL004, txtL005, txtL006, txtL007, txtL008,
txtL009);
//etc
}
private void loadTextBoxes(string key, params TextBox[] textboxes)
{
List myList = arrays[key];
//Check for both constraints on the loop so you don't get an exception
//for going outside either index of textboxes array or myList.
for (int i = 0; ((i < textboxes.length) && (i < myList.Count)); i++)
textboxes[i].Text = myList[i].ToString();
}
精彩评论