Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

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.
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

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.

            }
            
ASKER CERTIFIED SOLUTION
Avatar of dungla
dungla
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;}
}
What I need to do is select an item in a ListBox by ValueMember.
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.
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);
}
Noted....thanks!