Arcadian
Arcadian

Reputation: 1373

Programmatically changing form controls

I'm not sure if this can be done but I'd like to know if it is possible to completely alter what controls are on the form programmatically, similar to what happens with installers; when you click the next button the form doesn't hide or close and open the next form, it mearly loads a different set of controls(at least, that's how it appears).

Is it possible to do this with C# in Visual Studio? Or do you have to use tabs or hidden panels?

Upvotes: 0

Views: 1303

Answers (2)

VahidNaderi
VahidNaderi

Reputation: 2488

I think what you're looking for is UserControl. A UserControl is like a form in the way you can use it as a container for other controls. You can find a walkthrough here.

In the case of installers you mentioned or wizards in general you can design a different UserControl for each step and load each of them in a reserved area in the form. The reserved area can be a panel. for example assume you have a button which calls a method with wizards step number as parameter:

UserControl _step1Control = new UcStep1Control;
UserControl _step2Control = new UcStep2Control;
private void SetStep(int stepNumber)
{
    panel1.Controls.Clear();
    switch(stepNumber)
    {
        case 1:
            panel.Controls.Add(_step1Control);
            break;
        case 2:
            panel1.Controls.Add(_step2Control);
            break;
        default:
            break;
      }
}

Upvotes: 1

user520288
user520288

Reputation:

Yes you can do anything to the controls programatically. The designer that you use also generates C# code in the background.

In order to add a new control to your form, you use the the Form.Controls.Add(Control c) method. Any class which inherits from Control (Button, ListBox, etc) can be used. To remove it, you use the Remove method instead of Add.

Upvotes: 1

Related Questions