Solved

Populating a combo box

Posted on 2008-10-11
9
1,098 Views
Last Modified: 2011-10-19
How do I populate the values in a combo box. Say a combo box for Gender - just a couple of values preferably specified in an Enum. Using designer to build the form

Current code is attached.
Class has a combo box member named GenderCBox
Public Class NewPatient
    Private Sub NewPatient_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Application.Exit()
    End Sub
 
    Private Enum Genders
        Male
        Female
    End Enum
End Class

Open in new window

0
Comment
Question by:sunnycoder
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22693617

Gender[] gs = new Gender[] { Gender.Male,Gender.Female };
this.comboBox1.DataSource = gs;
0
 
LVL 14

Expert Comment

by:Binuth
ID: 22693671
try this
foreach (string val in Enum.GetNames(typeof(Genders)))
{
  GenderCBox.Items.Add(val);
}

Open in new window

0
 
LVL 14

Accepted Solution

by:
Binuth earned 200 total points
ID: 22693686
vb.net code
For Each Item As String In [Enum].GetNames(GetType(Genders))
   GenderCBox.Items.Add(Item)
Next

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 22693699
Hi sunnycoder;

This is how you can do what you want in Visual Basic .Net

        Dim genderType() As Genders = New Genders() {Genders.Male, Genders.Female}
        Me.GenderCBox.DataSource = genderType

Fernando


0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22693791
Binuth,

Your code works perfect. I had a couple of questions ... The combo box has 3 values -male, female and a blank. Where did the blank come from?

Also
[Enum].GetNames(GetType(Genders))

GetType(Genders) -> gets the type that is Enum Genders
GetNames -> gets the strings defined in Enum Genders
[Enum].  -> what is the significance of this piece of code.

Fernando,

Your code works fine too. A question for you too :)
Dim genderType() As Genders = New Genders() {Genders.Male, Genders.Female}
Here we are creating an array of type Enum Genders and filling it up with enum values. Am I right? What if order or members of enum in the original definition changes? Is there a way by which we dont have to worry about keeping this section of code in sync with the original Enum definition.

Thanks guys ... you rock!
sunnycoder
0
 
LVL 1

Assisted Solution

by:zonkerman
zonkerman earned 200 total points
ID: 22693971
You question, "How do I populate the values in a combo box"..."Using designer to build the form".
I think your direct target question is how to do this in the designer so no code is needed.  Here is how with Visual Studio 2005:

1)  Select your combo box control that is on your form designer
2)  Locate your "Properties"  window (press F4) which should be displaying properites for the selected combo box
3)  While your properties window is in "Categorized" order, go to the "Data" section in it and select the "Items" property. When you do this you will see a small button to the right of the property with 3 dots in it.  Click this button and enter your values.
0
 
LVL 45

Author Comment

by:sunnycoder
ID: 22694004
Awesome guys ... I am learning more than any book could have taught me in this time. Thanks zonkerman - that was an nice tip.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 22697757
Hi sunnycoder;

To your question, "Here we are creating an array of type Enum Genders and filling it up with enum values. Am I right?", Actually we are creating and array of Enum Genders value types and not multiple structures of Enum Genders as can be seen from the image of the watch window below. Each element of the array is an individual value of the Enum Genders.

To your question, "What if order or members of enum in the original definition changes? Is there a way by which we dont have to worry about keeping this section of code in sync with the original Enum definition.", When creating an enumeration structure it needs to be carfully considered before hand and any additions should be made to the end of the list so that code compiled aganst it already is not effected and will remain in sync.

Fernando

SP32-105.jpg
0
 
LVL 45

Author Closing Comment

by:sunnycoder
ID: 31505293
Thanks guys :o)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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