1

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... with 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?

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. I never would have thought of that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.