Setting the ValueMember of a listbox in Windows Forms...

I have been doing everything in asp.net lately so I am struggling with this simple issue in a Windows Forms Application.  In Asp.net I would create a ListItem and then add it to the listbox

ex.. ListItem li = new ListItem("DisplayValue", "ValueMember);

and then with my list box

lstItems.Add(li);

In windows forms the ListBox.Add() method does not accept a ListItem.

I do not have a DataTable that I can bind to the listbox and use something that looks like this...

lstItems.DataSource = dtItems;
lstItems.DisplayMember = "DisplayColumn";
lstItems.ValueMember = "ValueColumn";

I am using the add method and I'm trying to set the ValueMember with no success...  I will post the code that I am attempting to execute...

private void btnGetProcedures_Click(object sender, System.EventArgs e)
            {
                  lstStoredProcedures.Items.Clear();
                  ListBox.SelectedIndexCollection indexes = lstTables.SelectedIndices;
                  foreach (int index in indexes)
                  {
                        int iTableID = getTableID(lstTables.Items[index].ToString());
                        _intTableID = iTableID;
                        TableStoredProcedures tspTableProcedures = new TableStoredProcedures(_intTableID);
                        _arStoredProcedures = GetStoredProcedures();
                        tspTableProcedures.addStoredProcedures(_arStoredProcedures);
                        _arTableStoredProcedures.Add(tspTableProcedures);
                  
                        foreach(StoredProcedure spProcedure in _arStoredProcedures)
                        {      
                              lstStoredProcedures.Items.Add(spProcedure.ProcedureName);
                              //This is where i need to set the value member...
                        }
                  }
                  
            }
All i need is to set the ValueMember when setting the DisplayMember...
If anyone can help the question is worth 500pts...
Thanks in advance...
Rick

TMPSupportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

quoclanCommented:
ValueMember is set in case where you bind data. So, you must specify DataSource of ListBox. ListBox can accept any object that implements the IList interface, such as DataSet (DataTable) or Array, as its DataSource.
I use ArrayList to do this.

public class MyObject
{
    string strDisplay="";
    string strValue="";
    string strOther="";

    public MyObject(string strDis, string strVal, string strOth)
   {
        strDisplay = strDis;
        strValue=strVal;
       strOther=strOth;
    }
    public string DisplayString
    {
         get{return strDisplay;}
         set{strDisplay=value;}
    }
    public string ValueString
    {
         get{return strValue;}
         set{strValue=value;}
    }
    public string OtherString
    {
         get{return strOther;}
         set{strOther=value;}
    }
}

private void btnGetProcedures_Click(object sender, System.EventArgs e)
          {
               lstStoredProcedures.Items.Clear();
               ListBox.SelectedIndexCollection indexes = lstTables.SelectedIndices;
               foreach (int index in indexes)
               {
                    int iTableID = getTableID(lstTables.Items[index].ToString());
                    _intTableID = iTableID;
                    TableStoredProcedures tspTableProcedures = new TableStoredProcedures(_intTableID);
                    _arStoredProcedures = GetStoredProcedures();
                    tspTableProcedures.addStoredProcedures(_arStoredProcedures);
                    _arTableStoredProcedures.Add(tspTableProcedures);
               
                   ArrayList al = new ArrayList();
                    foreach(StoredProcedure spProcedure in _arStoredProcedures)
                    {    
                         //lstStoredProcedures.Items.Add(spProcedure.ProcedureName);     -->Comment this
                         //This is where i need to set the value member...
                            MyObject mObject = new MyObject(spProcedure.ProcedureName, /*your value string*/,/*another ...*/);
                           al.Add(mObject);
                    }
                    lstStoredProcedures.DataSource = al;
                    lstStoredProcedures.DisplayMember = "DisplayString";
                    lstStoredProcedures.ValueMember = "ValueString";
               }
               
          }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TMPSupportAuthor Commented:
Thanks quoclan...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.