Combobox woes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Combobox woes

  1. #1
    Ian Lent Guest

    Combobox woes


    Would some kind sould please post sample code on how to load a combobox with
    a string and an associated primary key (integer). I've been trying this for
    days and haven't gotten anywhere. Any help would be much appreciated.

  2. #2
    Cesar Guest

    Re: Combobox woes


    "Ian Lent" <ilent00@hotmail.com> wrote:
    >
    >Would some kind sould please post sample code on how to load a combobox

    with
    >a string and an associated primary key (integer). I've been trying this

    for
    >days and haven't gotten anywhere. Any help would be much appreciated.


    Try this snippet:
    for (int i = 0; i < 10; i++)
    {
    comboBox1.Items.Add("Item" + i);
    }


  3. #3
    Taiwo Guest

    Re: Combobox woes


    Here is an example:

    The following class is a generic class that allows you to create an object
    with a key and value pair:

    Public Class ListData
    Private m_value As Object
    Private m_displayText As String

    Public Property DisplayText() As String
    Get
    Return m_displayText
    End Get
    Set(ByVal Value As String)
    m_displayText = Value
    End Set
    End Property

    Public Property Value() As Object
    Get
    Return m_value
    End Get
    Set(ByVal Value As Object)
    m_value = Value
    End Set
    End Property

    Sub New(ByVal _displayText As String, ByVal _value As Object)
    m_displayText = _displayText
    m_value = _value
    End Sub

    End Class

    You can then add instances of this class to your combo box. Assuming you
    wanted to add US States and their state abbreviation as the key, you'd do
    the following:

    '-- Add states to array list
    Dim listDataArr As ArrayList = New ArrayList(50)
    listDataArr.Add(New ListData("Washington", "WA"))
    listDataArr.Add(New ListData("California", "IL"))
    ...

    '-- set up state combo box list items
    With cboState
    .Items.Clear()
    .DataSource = listDataArr '-- list of State key-value pair objects
    .DisplayMember = "DisplayText" '-- display the "DisplayText" property
    of the ListData class
    .ValueMember = "Value" '-- this is the value returned for the
    SelectedValue property of the combo box.
    End With

    Since ListData's Value property is an object type, I noticed that the combo
    box list works when I specify the actual type when creating ListData
    instances. For example:

    '-- Creating a boolean value combo list
    listDataArr.Add(New ListData("No", System.Boolean.Parse("False")))
    listDataArr.Add(New ListData("Yes", System.Boolean.Parse("True")))


    '-- Creating a numeric value combo list
    listDataArr.Add(New ListData("Female", System.Int16.Parse("0")))
    listDataArr.Add(New ListData("Male", System.Int16.Parse("1")))

    You can avoid having to do this by making your ListData's Value property be
    a specific type.

    ---
    This Posting is provided "AS IS" with no warranties, and confers no rights.
    You assume all risk for your use. 2002 Microsoft Corporation.
    All rights reserved.

    Taiwo Ayedun


    "Cesar" <cesargmercado@msn.com> wrote in message
    news:3c42cd43$1@147.208.176.211...
    >
    > "Ian Lent" <ilent00@hotmail.com> wrote:
    > >
    > >Would some kind sould please post sample code on how to load a combobox

    > with
    > >a string and an associated primary key (integer). I've been trying this

    > for
    > >days and haven't gotten anywhere. Any help would be much appreciated.

    >
    > Try this snippet:
    > for (int i = 0; i < 10; i++)
    > {
    > comboBox1.Items.Add("Item" + i);
    > }
    >




  4. #4
    Taiwo Guest

    Re: Combobox woes


    I meant listDataArr.Add(New ListData("California", "CA")) rather than
    listDataArr.Add(New ListData("California", "IL"))

    ---
    Taiwo

    "Taiwo" <taiwo_a@hotmail.com> wrote in message
    news:3c42f610@147.208.176.211...
    >
    > Here is an example:
    >
    > The following class is a generic class that allows you to create an object
    > with a key and value pair:
    >
    > Public Class ListData
    > Private m_value As Object
    > Private m_displayText As String
    >
    > Public Property DisplayText() As String
    > Get
    > Return m_displayText
    > End Get
    > Set(ByVal Value As String)
    > m_displayText = Value
    > End Set
    > End Property
    >
    > Public Property Value() As Object
    > Get
    > Return m_value
    > End Get
    > Set(ByVal Value As Object)
    > m_value = Value
    > End Set
    > End Property
    >
    > Sub New(ByVal _displayText As String, ByVal _value As Object)
    > m_displayText = _displayText
    > m_value = _value
    > End Sub
    >
    > End Class
    >
    > You can then add instances of this class to your combo box. Assuming you
    > wanted to add US States and their state abbreviation as the key, you'd do
    > the following:
    >
    > '-- Add states to array list
    > Dim listDataArr As ArrayList = New ArrayList(50)
    > listDataArr.Add(New ListData("Washington", "WA"))
    > listDataArr.Add(New ListData("California", "IL"))
    > ...
    >
    > '-- set up state combo box list items
    > With cboState
    > .Items.Clear()
    > .DataSource = listDataArr '-- list of State key-value pair objects
    > .DisplayMember = "DisplayText" '-- display the "DisplayText"

    property
    > of the ListData class
    > .ValueMember = "Value" '-- this is the value returned for the
    > SelectedValue property of the combo box.
    > End With
    >
    > Since ListData's Value property is an object type, I noticed that the

    combo
    > box list works when I specify the actual type when creating ListData
    > instances. For example:
    >
    > '-- Creating a boolean value combo list
    > listDataArr.Add(New ListData("No", System.Boolean.Parse("False")))
    > listDataArr.Add(New ListData("Yes", System.Boolean.Parse("True")))
    >
    >
    > '-- Creating a numeric value combo list
    > listDataArr.Add(New ListData("Female", System.Int16.Parse("0")))
    > listDataArr.Add(New ListData("Male", System.Int16.Parse("1")))
    >
    > You can avoid having to do this by making your ListData's Value property

    be
    > a specific type.
    >
    > ---
    > This Posting is provided "AS IS" with no warranties, and confers no

    rights.
    > You assume all risk for your use. 2002 Microsoft Corporation.
    > All rights reserved.
    >
    > Taiwo Ayedun
    >
    >
    > "Cesar" <cesargmercado@msn.com> wrote in message
    > news:3c42cd43$1@147.208.176.211...
    > >
    > > "Ian Lent" <ilent00@hotmail.com> wrote:
    > > >
    > > >Would some kind sould please post sample code on how to load a combobox

    > > with
    > > >a string and an associated primary key (integer). I've been trying this

    > > for
    > > >days and haven't gotten anywhere. Any help would be much appreciated.

    > >
    > > Try this snippet:
    > > for (int i = 0; i < 10; i++)
    > > {
    > > comboBox1.Items.Add("Item" + i);
    > > }
    > >

    >
    >




  5. #5
    Phil Weber Guest

    Re: Combobox woes

    Ian: Please do not post the same message to multiple groups. The
    vb.dotnet.technical group is the best place for VB.NET technical questions;
    you have received an answer to your question there.

    All: Please do not reply to technical questions in this group. If you don't
    reply, it's simple for me to move the question to the .technical group, but
    it becomes much more difficult to do so when a thread contains several
    messages. Thanks!
    ---
    Phil Weber
    DevX Newsgroup Admin



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center