Hi, kinda new to the whole C# thing and trying to make a small app for myself in C# in Visual Studio.

Basically i have 2 combo boxes, 1 had Classes the other has Spec's in it. When you select the class it populates the second combo box with the "spec's" available to that class.

i've pulled all the specs into seperate array groups based on wether they use mana, rage, runic power or energy.

when you select one of the "specs" i want it so it picks the right array for the group and turns a group box / label visible on my form. Here is my code that i have and obviously it's incorrect. Any Help would be greatly appreciated and i thank you in advance

private void classcmbo_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] ManaUsers = new string[]
            {"Balance","Restoration","BeastMaster","Marksman","Survival",
              "Fire","Frost","Arcane","Holy","Discipline","Shadow","Prot",
              "Retribution","Elemental","Enhance","Affliction","Demonology","Destruction"};
            string[] RageUsers = new string[] { "Arms", "Fury", "Protection", "Bearform" };
            string[] NrgUsers = new string[] { "Assassination", "Combat", "Subtlety", "Catform" };
            string[] RPUsers = new string[] { "Blood","Frost","Unholy" };

            if (classcmbo.Text == "ManaUsers")
            {
                stsmna.Visible = true;
                mnatxt.Visible = true;
            }
        }

Matt

Recommended Answers

All 6 Replies

anyone, i dont know if i missed putting any information in my post or anything... but yeah i'm assuming it couldnt be that difficult to get the result i want and i've spent the past 4 hours looking on google and here trying to find a post like what i'm after but i've come up short, so i would really appreciate help from anyone

Thanks

Matt

Use Dictionary instead of array.

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

        
        Dictionary<string, string[]> name;
        private void Form1_Load(object sender, EventArgs e)
        {
            name = new Dictionary<string, string[]>()
            {
                 {"ManaUsers",new string[]{"Balance","Restoration","BeastMaster","Marksman","Survival","Fire","Frost","Arcane","Holy","Discipline","Shadow","Prot","Retribution","Elemental","Enhance","Affliction","Demonology","Destruction"}},
                 {"RageUsers",new string[]{"Arms", "Fury", "Protection", "Bearform" }},
                 {"NrgUsers",new string[] { "Assassination", "Combat", "Subtlety", "Catform" }},
                 {"RPUsers",new string[] {"Blood","Frost","Unholy" }}
            };
            
            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            comboBox1.DataSource = name.Keys.ToList<string>();
        }

        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.DataSource = name[comboBox1.SelectedValue.ToString()];

        }
    }

Make a list of string arrays and add them to the list in the order that you add the classes to their combobox. Note in this case that comboBox1 is your classcmbo and comboBox2 is the other one.

List<string[]> strlis;

        public Form1()
        {
            InitializeComponent();
            
            //add your class strings to comboBox1
            strlis = new List<string[]>();
            strlis.Add(ManaUsers);
            strlis.Add(RageUsers);
            strlis.Add(NrgUsers);
            strlis.Add(RPUsers);
//.......
//.....
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            foreach(string str in strlis[comboBox1.SelectedIndex])
                comboBox2.Items.Add(str);
        }

EDIT: adatapost beat me to it. You can decide which method works for you.

Use Dictionary instead of array.

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

        
        Dictionary<string, string[]> name;
        private void Form1_Load(object sender, EventArgs e)
        {
            name = new Dictionary<string, string[]>()
            {
                 {"ManaUsers",new string[]{"Balance","Restoration","BeastMaster","Marksman","Survival","Fire","Frost","Arcane","Holy","Discipline","Shadow","Prot","Retribution","Elemental","Enhance","Affliction","Demonology","Destruction"}},
                 {"RageUsers",new string[]{"Arms", "Fury", "Protection", "Bearform" }},
                 {"NrgUsers",new string[] { "Assassination", "Combat", "Subtlety", "Catform" }},
                 {"RPUsers",new string[] {"Blood","Frost","Unholy" }}
            };
            
            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            comboBox1.DataSource = name.Keys.ToList<string>();
        }

        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.DataSource = name[comboBox1.SelectedValue.ToString()];

        }
    }

I think also in the SelectedIndexChanged event, you need to check condition as person want to show some label.

if (combo1.SelectedItem.Value.Equal("ManaUsers"))
{
stsmna.Visible = true;
mnatxt.Visible = true;
}

Use Dictionary instead of array.

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

        
        Dictionary<string, string[]> name;
        private void Form1_Load(object sender, EventArgs e)
        {
            name = new Dictionary<string, string[]>()
            {
                 {"ManaUsers",new string[]{"Balance","Restoration","BeastMaster","Marksman","Survival","Fire","Frost","Arcane","Holy","Discipline","Shadow","Prot","Retribution","Elemental","Enhance","Affliction","Demonology","Destruction"}},
                 {"RageUsers",new string[]{"Arms", "Fury", "Protection", "Bearform" }},
                 {"NrgUsers",new string[] { "Assassination", "Combat", "Subtlety", "Catform" }},
                 {"RPUsers",new string[] {"Blood","Frost","Unholy" }}
            };
            
            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            comboBox1.DataSource = name.Keys.ToList<string>();
        }

        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.DataSource = name[comboBox1.SelectedValue.ToString()];

        }
    }

thats awesome man, i really appreciate it, i just got one more question.. now that thats all in how do i call on it.

say i have 2 values hidden
stsmana and mnatxt

and i want it so whenever someone chooses a class from the "mana" group that was created it will make those 2 values visible...

i tried

if (classcmbo.SelectedItem.Value.Equal("ManaUsers"))
            {
                stsmna.Visible = true;
                mnatxt.Visible = true;
            }

but it gives me the error

Form2.cs(173,40): error CS1061: 'object' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

but that didnt work, so i'm curious how i can call apon the dictionary to make the values visible

if (classcmbo.Text.Equal("ManaUsers")){
   stsmna.Visible = true;
   mnatxt.Visible = true;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.