Solved

Populating a combo box

Posted on 2008-10-11
9
1,101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 63

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 63

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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