Reputation: 53
I have 2 Listbox on different tabpages that uses the same datasource
Basically its tabpage1 + listbox1 and tabpage2 + listbox2
I'm trying to do the following :
When I select Item from listbox1 on tabpage1 , I want the same item selected to listbox2 on tabpage2
I tried this:
listbox1.SelectedItem = listBox2.SelectedItem;
also this :
string sitem = "";
sitem = listbox1.SelectedItem.ToString();
listbox2.SelectedItem = sitem
nothing works as expected, I'm wondering if its possible ?
Upvotes: 1
Views: 626
Reputation: 421
Set SelectedIndex
property of listbox2
:
listbox1.SelectedIndexChanged += delegate(object sndr, EventArgs args)
{
var lst = (ListBox) sndr;
listbox2.SelectedIndex = listbox2.Items.IndexOf(lst.SelectedItem);
};
Upvotes: 0
Reputation: 53
Finally I made it with the example of PaulF
here is my working code :
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string sitem = listBox1.SelectedItem.ToString();
int index = listBox2.FindString(sitem);
listBox2.SetSelected(index, true);
}
so when I select item in listbox1, it also select it in listbox2
Upvotes: 2
Reputation: 1434
Make sure the tabControl is declared as public
or internal
. if not then change the tabControl from private to public in the designer.cs file
private System.Windows.Forms.TabControl tabControl1;
public System.Windows.Forms.TabControl tabControl1;
and then
using (Form form = new Form())
{
form.listbox1.SelectedItem = form.listBox2.SelectedItem;
}
Upvotes: 1