Goran Nuskov Goran Nuskov - 4 years ago 129
C# Question

Need help to create a process C# : Combobox > Select item > Accept button > User Control > Panel

I need help for writing a code in C#, Visual Studio 2015. The thing is, I want to do the following : The user selects an item from the combobox, clicks the accept button afterwards, and a user control connected to the selected item appears in the panel I created. I will also attach a video so you can get a clearer picture of what I need here. Thanks.

The video attached by me on YouTube : https://www.youtube.com/watch?v=K2y1G94poWY

Answer Source

This is very simplified version for rapidity:

you could create a windows form as the comments suggest and add a combobox (combobox1) and the panel (panel1) and a button (button1) and hook them up with events.

enter image description here

See below and I've really made some simple controls but you could make this much more complex.

namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); comboBox1.Items.Add("item 1"); comboBox1.Items.Add("item 2"); comboBox1.Items.Add("item 3"); }

    private Control myControl;

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (((ComboBox)sender).SelectedItem.ToString())
        {
            case "item 1":
                //your loading logic here
                myControl = new UserControl();
                myControl.Controls.Add(new TextBox());
                break;
            case "item 2":
                //your loading logic here
                myControl = new UserControl();
                myControl.Controls.Add(new Button());
                break;
            case "item 3":
                //your loading logic here
                myControl = new UserControl();
                myControl.Controls.Add(new ComboBox());
                break;
            default:
                myControl = null;
                break;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        panel1.Controls.Clear();
        panel1.Controls.Add(myControl);
    }
 }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download