0

我的窗口中有 3 个列表框、3 个 texboxes、3 个按钮,当我将 listbox1 中的数据输入到 textbox1 并单击按钮或输入时,其他 2 个相同索引的列表框项目应该出现在其他 2 个文本框中。

这是写到现在的代码

private void Get_Click(object sender, RoutedEventArgs e)
{
    int x = listbox1.SelectedIndex;
    listbox2.SelectedIndex = x;
    listbox3.SelectedIndex = x;

    ListBoxItem lb1 = (listbox1.SelectedItem as ListBoxItem);
    tb1.Text = lb1.Content.ToString();


    ListBoxItem lb2 = (listbox2.SelectedItem as ListBoxItem);
    tb2.Text = lb2.Content.ToString();

    ListBoxItem lb3 = (listbox3.SelectedItem as ListBoxItem);
    tb3.Text = lb3.Content.ToString();


}


private void Add_Click(object sender, RoutedEventArgs e)
{
    int x = listbox1.SelectedIndex;
    listbox2.SelectedIndex = x;
    listbox3.SelectedIndex = x;

    listbox1.Items.Add(tb1.Text);
    listbox2.Items.Add(tb2.Text);
    listbox3.Items.Add(tb3.Text);

}

private void Delete_Click(object sender, RoutedEventArgs e)
{
    int x = listbox1.SelectedIndex;
    listbox2.SelectedIndex = x;
    listbox3.SelectedIndex = x;

    listbox1.Items.RemoveAt(listbox1.Items.IndexOf(listbox1.SelectedItem));
    listbox2.Items.RemoveAt(listbox2.Items.IndexOf(listbox2.SelectedItem));
    listbox3.Items.RemoveAt(listbox3.Items.IndexOf(listbox3.SelectedItem));


}
4

1 回答 1

1

目前还不清楚你在追求什么,但我认为你试图让 tb1 中的文本成为驱动程序,而不是从 listbox1 中选择。我在 Get_Click 事件中添加了一些简单的代码:

private void Get_Click(object sender, RoutedEventArgs e)
{
    foreach (ListBoxItem lbi in listbox1.Items)    //new code
    {                                              //new code
        if (lbi.Content.ToString() == tb1.Text)    //new code
        {                                          //new code
            lbi.IsSelected = true;                 //new code
            break;                                 //new code
        }                                          //new code
    }                                              //new code

    int x = listbox1.SelectedIndex;
    listbox2.SelectedIndex = x;
    listbox3.SelectedIndex = x;

    //ListBoxItem lb1 = (listbox1.SelectedItem as ListBoxItem);   //updated
    //tb1.Text = lb1.Content.ToString();                          //updated

    ListBoxItem lb2 = (listbox2.SelectedItem as ListBoxItem);
    tb2.Text = lb2.Content.ToString();

    ListBoxItem lb3 = (listbox3.SelectedItem as ListBoxItem);
    tb3.Text = lb3.Content.ToString();
}

这是你追求的那种东西吗?

于 2013-10-15T07:17:16.247 回答