Question

I'm creating a simple user registration form with fields to enter first name, last name, user name, password, and two combobox drop downs for major and concentration.
I believe I understand how to pass the enum data to the ComboBox, but not sure where (while file) to add the code.
Do I double click the ComboBox and add it there?
Do I create another class, add the enum data and code to display that data in the combobox there?

I believe I found out the way to add, here is my code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public enum MajorList { Engineering = 1, Science, Humanities, Art, Business };
    public enum EngConcentrations { Mechanical = 1, Electrical, Chemical, Civil };
    public enum SciConcentrations { Computer, Biology };
    public enum HumConcentrations { English, History };
    public enum ArtConcentrations { Graphics, Painting, History, Music };
    public enum BusConcentrations { Administration, Economics, Accounting };

    private void Form1_Load_1(object sender, EventArgs e)
    {
        foreach (var item in Enum.GetValues(typeof(MajorList)))
        {
            majors.Items.Add(item);
        }            
    }
}

Would it be possible to add one of the other enum based off of which major I selected? For example, I select Engineering, and the second combobox would have the drop down of the Engineering concentrations?

Était-ce utile?

La solution

It looks like you solved your original question. That code will add the values you want, and placing it in the form's Load event is fine.

As for the second question, yes, you could switch a second ComboBox based on the value selected in the "major" ComboBox. Subscribe to the SelectedValueChanged event:

void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
    var selectedMajor = (MajorList)comboBox1.SelectedItem;

    switch (selectedMajor)
    {
        case MajorList.Art:
            // populate the second combo box with ArtConcentrations values
            break;

        case MajorList.Business:
            // populate the second combo box with BusConcentrations values
            break;

        ...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top