0

我有 ListBox 和 listview,在一个表单中,我必须在它们之间同步,例如,如果我选择列表框中的第一项,则必须在列表视图中选择它,如果我从列表视图,它必须在 ListBox 中被选中。

我有成功,从列表框到列表视图,但不是对他们两个,我尝试使用标志,这不起作用。

我的列表框代码是这样的:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{    
    this.listView1.Items[listBox1.SelectedIndex].Selected = true;
}

ListView代码是:

 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
 {
     this.listBox1.SetSelected(this.listView1.FocusedItem.Index, true);
 }
4

2 回答 2

0

尝试用于列表视图

listView1.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(listView1_ItemSelectionChanged);

    void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {
        this.listBox1.SetSelected(this.listView1.FocusedItem.Index, true);
    }
于 2013-04-12T13:06:28.190 回答
0

我在我的电脑上尝试,以下代码有效:

namespace  WindowsFormsApplication5
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
                listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
                listView1.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(listView1_ItemSelectionChanged);
                listView1.Items.Add("3434");
                listView1.Items.Add("13434");
                listBox1.Items.Add("3434");
                listBox1.Items.Add("13434");
            }

            void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
            {
                this.listBox1.SetSelected(this.listView1.FocusedItem.Index, true);
            }

            void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                this.listView1.Items[listBox1.SelectedIndex].Selected = true;
            }
        }
    }
于 2013-04-17T12:03:10.233 回答