Hi
I am trying to link selected item from one list box to another list box and make the second Item selected there as well.
Lets say I have 2 list box populated by the city names in two different listbox in different orders and from different source, now I want to link this two listbox in way that if I select a city in lust box 1 that item has been selected in list box two as well.

Can you please let me know how I can do that?

write the SelectedIndexChanged event of the list box1 and in that event add the following code

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i=0;
            while (listBox1.SelectedItem != listBox2.Items[i])
                i++;
            listBox2.SelectedIndex = i;
        }
commented: Perfecr +1

Thanks Man
This is exactly what I was looking for ,however I still could not understand how it is doing this!
Thanks again

so what i would suggest is do the following:
gather what item from one box you will select, then from there get the index of that and find the value of the item at that corisponding index on the next listbox. I've had to do this before.
Also I would highly recommend making your listboxes double buffered for this, stops flickering some.
Code for listboxes:

private void specialButton3_Click(object sender, EventArgs e)
    {//search for items
        if (textBox3.Text != string.Empty)  //make sure textbox3 has text
        {
            if (listBox1.Items.Contains(textBox3.Text)) //tries to match textbox1 text with items from listbox1
            {
                listBox1.SetSelected(listBox1.Items.IndexOf(textBox3.Text), true);
                listBox2.SetSelected(listBox1.SelectedIndex, true);
                listBox3.SetSelected(listBox1.SelectedIndex, true);
            }
            //MessageBox.Show("Item: " + textBox3.Text + " was found.", "Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else //display error message
            {
                MessageBox.Show("Item: " + textBox3.Text + " was not found.", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

Notes: SpecialButton is something I made, ignore it, it is just a button, code still applies. Also, textBox3 is a search box
Code for double buffered control:

public Form1()
{
    InitializeComponent();

    this.SetStyle(
        ControlStyles.UserPaint |
        ControlStyles.AllPaintingInWmPaint |
        ControlStyles.DoubleBuffer, true);
}
commented: Irrelevant post +0

Thanks Man
This is exactly what I was looking for ,however I still could not understand how it is doing this!
Thanks again

Here in this code when you select an item in listbox1 the application starts locating the index of the item in listBox2 and when it finds that it breaks the loop and set the index selected in listbox2 :) hope you understand.... Another way to solve this problem is

listBox2.SelectedIndex = listBox2.Items.IndexOf(listBox1.SelectedItem);

Thanks abelLazm,
Second solution helped me a better understanding of both now
Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.