Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populating a combo box

Posted on 2008-10-11
9
Medium Priority
?
1,109 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 800 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
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.

 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 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 800 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 64

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

Technology Partners: 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!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

597 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