0

我怎样才能更有效地做到这一点?理想情况下,我可以在没有任何参数的情况下调用 showTag 并返回正确的组合框。

我添加了参数表来定义 showTag 返回的值是来自comboBox1 还是comboBox2。因此,如果我想要 comboBox1 的值,我调用 showTag(1),如果我想要 comboBox2 的值,我调用 showTag(2)

//Return the currently selected language
public string showTag(int table)
{
    string LanguageSelection = String.Empty;
    if (table == 1)
    {
        LanguageSelection = comboBox1.SelectedItem.ToString();
    }
    else
    {
        LanguageSelection = comboBox2.SelectedItem.ToString();
    }
    string[] LanguageTag = LanguageSelection.Split(' ');
    string tag = LanguageTag[1].Replace("<", "").Replace(">", "");
    return tag;
}
4

1 回答 1

1

您必须在您的 2 组合框的同一功能上添加 SelectedIndexChanged 事件(转到您的两个组合框的属性并单击事件和 SelectedIndexChanged)

private void ComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
            ComboBox cb =  (ComboBox )sender ;
            LanguageSelection = cb.SelectedItem.ToString();
            string[] LanguageTag = LanguageSelection.Split(' ');
            //string tag have to be declared in your application at a global level
            tag = LanguageTag[1].Replace("<", "").Replace(">", "");

}
于 2012-08-17T23:40:48.277 回答