Jump to content

listbox how select index

- - - - -

  • Please log in to reply
3 replies to this topic

#1
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Hello i have code:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                string selected = Path.GetFileName(lblist.IndexFromPoint(e.Location).ToString());
                foreach (ListBox i in lblist2.Items)
                {
                    if (selected == i.Name)
                    {
                        lblist2.SelectedIndex = i.SelectedIndex;
                    }
                }
            }
        }
My application has two listboxes. listbox1 shows me full file paths. listbox2 shows me only filenames.
On first i click the slected item.
I want to find on listbox2 the same string and sekect it as in listbox1 . I wrote this code, however i stuck and of course it doesn't work. I tried. Help.
THink positive :)

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
 foreach (ListBox i in lblist2.Items)
                {
                    if (selected == i.Name)
                    {
                        lblist2.SelectedIndex = i.SelectedIndex;
                    }
                }
aha there is the mistake
it should be something along the lines,
foreach(listbox item var1 in Listbox1.Items)
{
get the selected index
}
if i am wrong , please do get the error message Visual Studio is reporting you

#3
analysis_junky

analysis_junky

    Newbie

  • Members
  • Pip
  • 9 posts
foreach(ListItem item in Listbox1.Items)
{
     if(it.Selected)
     {
          // Do something here
     }
}


#4
analysis_junky

analysis_junky

    Newbie

  • Members
  • Pip
  • 9 posts
Misspelled item, this should work

foreach(ListItem item in Listbox1.Items)
{
     if(item.Selected)
     {
          // Do something here
     }
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users