从数组加载文本框

时间:2010-07-28 14:49:17

标签: c# forms

我的表单有几行文本框。第一行名为txtL000至txtL009,第二行名为txtL100至txtL109,依此类推。在这些行的每一行下面是另一行名为txtT000到txtT009等的文本框。当用户打开表单时,我想加载名为txtL ...的文本框,其中包含数组中的字符串,具体取决于命名的相应文本框中的内容txtT..0。例如,如果txtT000中有“land”,我想通过txtL009加载txtL000和来自数组arrLand的字符串。如果它有“点”,我想通过txtL009加载txtL000和来自数组arrPoint的字符串。什么是最有效的方法?

1 个答案:

答案 0 :(得分:1)

我能想到的最简单的方法是使用Dictionary来存储数组:

//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();
}
相关问题