在 C# 中为 Windows 窗体创建向导

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2340566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:35:29  来源:igfitidea点击:

Creating Wizards for Windows Forms in C#

c#winformswizard

提问by Ravi

I am new in Creating Wizards for Windows Forms Application in C# .Net. So i don't have any idea in wizard creation. Please give me some ideas about creating Multiple wizard.

我是在 C# .Net 中为 Windows 窗体应用程序创建向导的新手。所以我对向导创建没有任何想法。请给我一些关于创建多向导的想法。

Regards, ravi

问候,拉维

回答by Jojo Sardez

You need to create your own to meet your own preferences. A tip will be for you to create a base form named like "frmWizard" then all your wizard windows will inherit from it. You should put common objects or wizard objects on the base class and modify \ override them on the derived class if needed.

您需要创建自己的以满足您自己的喜好。一个提示是让您创建一个名为“frmWizard”的基本表单,然后您的所有向导窗口都将从它继承。您应该将公共对象或向导对象放在基类上,并根据需要在派生类上修改\覆盖它们。

回答by Hans Passant

Lots of ways to do it. Creating a form for each wizard step is possible, but very awkward. And ugly, lots of flickering when the user changes the step. Making each step a UserControl can work, you simply switch them in and out of the form's Controls collection. Or make one of them Visible = true for each step. The UC design tends to get convoluted though, you have to add public properties for each UI item.

有很多方法可以做到。为每个向导步骤创建一个表单是可能的,但非常笨拙。当用户更改步骤时,丑陋,大量闪烁。使每个步骤都成为 UserControl 可以工作,您只需将它们切换到窗体的 Controls 集合中或从中切换出来。或者为每一步设置一个 Visible = true。UC 设计往往会变得复杂,您必须为每个 UI 项添加公共属性。

The easy and RAD way is to use a TabControl. Works very well in the designer since it allows you to switch tabs at design time and drop controls on each tab. Switching steps is trivial, just change the SelectedIndex property. The only thing non-trivial is to hide the tabs at runtime. Still easy to do by processing a Windows message. Add a new class to your form and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

简单且 RAD 的方法是使用 TabControl。在设计器中效果很好,因为它允许您在设计时切换选项卡并在每个选项卡上放置控件。切换步骤很简单,只需更改 SelectedIndex 属性即可。唯一重要的是在运行时隐藏选项卡。通过处理 Windows 消息仍然很容易做到。向您的表单添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。

using System;
using System.Windows.Forms;

class WizardPages : TabControl {
  protected override void WndProc(ref Message m) {
    // Hide tabs by trapping the TCM_ADJUSTRECT message
    if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
    else base.WndProc(ref m);
  }
}

回答by Cluster

class WizardPages : TabControl
{
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
        else base.WndProc(ref m);
    }        

    protected override void OnKeyDown(KeyEventArgs ke)
    {
        // Block Ctrl+Tab and Ctrl+Shift+Tab hotkeys
        if (ke.Control && ke.KeyCode == Keys.Tab) 
            return;
        base.OnKeyDown(ke);
    }
}