?
Solved

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

Posted on 2006-04-14
2
Medium Priority
?
1,624 Views
Last Modified: 2009-09-11
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

0
Comment
Question by:TMPSupport
2 Comments
 
LVL 2

Accepted Solution

by:
quoclan earned 2000 total points
ID: 16456294
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
 

Author Comment

by:TMPSupport
ID: 16457466
Thanks quoclan...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question