我有一个表单中的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);
}
答案 0 :(得分:0)
尝试用于列表视图
listView1.ItemSelectionChanged + = new ListViewItemSelectionChangedEventHandler(listView1_ItemSelectionChanged);
void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
this.listBox1.SetSelected(this.listView1.FocusedItem.Index, true);
}
答案 1 :(得分: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;
}
}
}