i keep forgetting this - Find List Box item by ValueMember not List Box Index

If I pick an item from a ListBox that is assigned a DataSource and has the DisplayMember and ValueMember assigned for each item in the list......when I pick an item in the ListBox....I want to store that value....and then use that value to programmatically select the same item in another list box........which I know will have the same ValueMember value as the other ListBox item had.
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
dunglaConnect With a Mentor Commented:
Use ListBox.SelectedValue (object) and cast to your prefer datatype.

private void listBoxRDOnly_DoubleClick(object sender, System.EventArgs e)
          {              
               //get ID
               int tempAEID = ReturnListBoxSelectedValue(this.listBoxRDOnly);
               //navigate to first tab
               this.tabControl1.SelectedTab = 1;
               //check Active filter if it is unchecked
               if(!this.checkBoxShowActive.Checked)
               {
                    this.checkBoxShowActive.Checked = true;
               }
               //refresh list box
               RefreshAEPOOLList();
         

     //programmatically pic the AE ID   //////////////////////////    HOW DO I DO THIS......THIS IS A DIFFERENT LIST BOX on another TAB PAGE.....BUT HAS THE SAME DATASOURCE, etc.

          }
private int ReturnListBoxSelectedValue(ListBox lb)
{
if (lb.SelectedValue != null)
{return int.Parse(lb.SelectedValue.ToString());}
else { return 0;}
}
0
 
Tom KnowltonWeb developerAuthor Commented:
For example:



private void listBoxRDOnly_DoubleClick(object sender, System.EventArgs e)
            {                  
                  //get ID
                  int tempAEID = ReturnListBoxSelectedValue(this.listBoxRDOnly);
                  //navigate to first tab
                  this.tabControl1.SelectedTab = 1;
                  //check Active filter if it is unchecked
                  if(!this.checkBoxShowActive.Checked)
                  {
                        this.checkBoxShowActive.Checked = true;
                  }
                  //refresh list box
                  RefreshAEPOOLList();
            

      //programmatically pic the AE ID   //////////////////////////    HOW DO I DO THIS......THIS IS A DIFFERENT LIST BOX on another TAB PAGE.....BUT HAS THE SAME DATASOURCE, etc.

            }
            
0
 
dunglaCommented:
private void listBoxRDOnly_DoubleClick(object sender, System.EventArgs e)
          {              
               //get ID
               int tempAEID = ReturnListBoxSelectedValue(this.listBoxRDOnly);
               //navigate to first tab
               this.tabControl1.SelectedTab = 1;
               //check Active filter if it is unchecked
               if(!this.checkBoxShowActive.Checked)
               {
                    this.checkBoxShowActive.Checked = true;
               }
               //refresh list box
               RefreshAEPOOLList();
         

     //programmatically pic the AE ID   //////////////////////////    HOW DO I DO THIS......THIS IS A DIFFERENT LIST BOX on another TAB PAGE.....BUT HAS THE SAME DATASOURCE, etc.
// set selected value to list box
ListBox.SelectedValue = tempAEID;

          }
private int ReturnListBoxSelectedValue(ListBox lb)
{
if (lb.SelectedValue != null)
{return int.Parse(lb.SelectedValue.ToString());}
else { return 0;}
}
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Tom KnowltonWeb developerAuthor Commented:
What I need to do is select an item in a ListBox by ValueMember.
0
 
Tom KnowltonWeb developerAuthor Commented:
private void listBoxRDOnly_DoubleClick(object sender, System.EventArgs e)
{                  
      //get ID
      int tempAEID = ReturnListBoxSelectedValue(this.listBoxRDOnly);
      //navigate to first tab
      this.tabControlAETabPages.SelectedTab = this.tabPageManageAE;
      //check Active filter if it is unchecked
      if(!this.checkBoxShowActive.Checked)
      {
            this.checkBoxShowActive.Checked = true;
      }
      //refresh list box
      RefreshAEPOOLList();
      //programmatically pic the AE ID

      this.listBoxAEtoManage.SelectedValue = tempAEID;
                  
}




This code works....but the last line HIGHLIGHTS the right ITEM......but it does not function like a MOUSE CLICK....the Selected Index Changed event does not fire.
0
 
dunglaCommented:
You can use ListBox.SetSelected(index, true). For example

private void listBoxRDOnly_DoubleClick(object sender, System.EventArgs e)
{              
     //get ID
     int tempAEID = ReturnListBoxSelectedValue(this.listBoxRDOnly);
     //navigate to first tab
     this.tabControlAETabPages.SelectedTab = this.tabPageManageAE;
     //check Active filter if it is unchecked
     if(!this.checkBoxShowActive.Checked)
     {
          this.checkBoxShowActive.Checked = true;
     }
     //refresh list box
     RefreshAEPOOLList();
     //programmatically pic the AE ID

     //find the index of selected item in list
     int index = listBoxAEtoManage.Items.IndexOf(tempAEID);
     // set selected for list box
     this.listBoxAEtoManage.SetSelected(index, true);
}
0
 
Tom KnowltonWeb developerAuthor Commented:
Noted....thanks!
0
All Courses

From novice to tech pro — start learning today.