Solved

Populating a combo box

Posted on 2008-10-11
9
1,096 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I get the unique ID after insert? 87 90
Paging GridView 7 34
System32Int Error 8 44
How to open a link from vb.net app 3 9
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now