以编程方式更改表单控件 [英] Programmatically changing form controls

查看:38
本文介绍了以编程方式更改表单控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可以完成,但我想知道是否有可能以编程方式完全改变表单上的控件,类似于安装程序发生的情况;当您单击下一个按钮时,表单不会隐藏或关闭并打开下一个表单,它只会加载一组不同的控件(至少,它是这样显示的).

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).

是否可以在 Visual Studio 中使用 C# 执行此操作?还是必须使用选项卡或隐藏面板?

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

推荐答案

我认为您正在寻找的是 UserControl.UserControl 就像一个表单,您可以将其用作其他控件的容器.您可以在此处找到演练.

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.

对于您提到的安装程序或一般的向导,您可以为每个步骤设计不同的 UserControl,并将它们中的每一个加载到表单的保留区域中.保留区域可以是面板.例如,假设您有一个按钮,它调用一个以向导步骤号为参数的方法:

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;
      }
}

这篇关于以编程方式更改表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆