【问题标题】:How to populate a textbox with an array index from the combobox selected index of a different array如何使用来自不同数组的组合框选定索引的数组索引填充文本框
【发布时间】:2016-06-13 16:22:24
【问题描述】:

我从一个文本文件创建了两个字符串数组,并用 array1 填充了一个组合框。我想了解的是,如何获得一个文本框来显示与组合框(array1)的选定索引匹配的array2的索引?

我认为这样的事情可能会奏效:

if(phoneComboBox.Text == cPhone[index])
{
    nameTextBox.Text = cName[index]; //show equal index item to cPhone/phoneComboBox
}

但这似乎不起作用。我也尝试过 foreach 循环,也许我做错了。我在 window_loaded 事件中读取了文本文件和数组,但不知道这是否是问题所在。我已经看到 SelectedIndexChanged 事件在类似问题中提到了很多,但我没有该事件可以使用,只有 SelectionChanged。

有人可以指出我的正确方向吗?我知道数组在这里可能不是最好的用途,但它们是我用过的,所以请帮助我正确理解它。

这就是我读取数组的方式:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    //read file on start
    int counter = 0;
    string line;
    StreamReader custSR = new StreamReader(cFileName);
    line = custSR.ReadLine();

    while (line != null)
    {
        Array.Resize(ref cPhone, cPhone.Length + 1);
        cPhone[cPhone.Length - 1] = line;
        counter++;
        line = custSR.ReadLine();

        Array.Resize(ref cName, cName.Length + 1);
        cName[cName.Length - 1] = line;
        counter++;
        line = custSR.ReadLine();

        phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]);
    }
    custSR.Close();

    /*string changeAll = string.Join(Environment.NewLine, cPhone);
    string allOthers = string.Join(Environment.NewLine, cName);

    MessageBox.Show(changeAll + allOthers);*/

    //focus when program starts
    phoneComboBox.Focus();
}

【问题讨论】:

  • 显示代码你是如何填充数组的?

标签: c# arrays wpf combobox


【解决方案1】:

如果你在比较文本字符串,那么你应该使用函数“.Equals”而不是
"=="

if(phoneComboBox.Text.Equals(cPhone[index]))
{
    nameTextBox.Text = cName[phoneComboBox.SelectedIndex];
}

【讨论】:

  • 仍然得到一个空白文本框并从SelectedIndex中删除了 ()
  • 选择了if(phoneComboBox.Text != -1),因为这对我从下面的答案中得到的 outOfRange 异常进行了排序。
【解决方案2】:

不需要检查条件,只需获取选中的索引即可:

nameTextBox.Text = cName[phoneComboBox.SelectedIndex];

WPF 中有SelectionChanged

SelectedIndexChanged 用于winform

最佳实践:

但我建议您使用Tag 属性来实现此目的的更好方法。您不必为此更改很多代码。

//Create "ComboBoxItem" instead of "array"
while (line != null)
{
    //initialize
    ComboBoxItem cmItem = new ComboBoxItem();

    //set Phone as Display Text
    cmItem.Content = line;  //it is the Display Text

    //get Name
    line = custSR.ReadLine();

    //set Tag property
    cmItem.Tag = line;  //it is the attached data to the object

    //add to "Items"
    phoneComboBox.Items.Add(ComboBoxItem);
}

现在在SelectionChanged 事件中获取所选项目非常简单:

void phoneComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    nameTextBox.Content = (e.AddedItems[0] as ComboBoxItem).Tag;
}

您不再需要处理这些数组。

【讨论】:

  • 这在SelectionChanged 事件中有效,但是如果我尝试键入与组合框中的项目不同的数字,我会得到一个 indexOutOfRange 异常。哦,好吧,还有一个问题要解决。
  • 我已经发布了一个更好的方法来实现它,请实施它你不会有任何问题。
  • 它必须是数组,但感谢您的提示,我已经找到了一种方法来使其在此处的帮助下工作。
【解决方案3】:

感谢其他答案,这是我想出的有效答案。修复了我得到的 indexOutOfRange 异常。

private void phoneComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (phoneComboBox.SelectedIndex != -1)
    {
        nameTextBox.Text = cName[phoneComboBox.SelectedIndex]; 
    }
    else
    {
        nameTextBox.Text = string.Empty;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多