Solved

Populating a combo box

Posted on 2008-10-11
9
1,100 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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