使用多个视图构建 C# .NET windows 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25158/
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-01 09:14:09  来源:igfitidea点击:

Building C# .NET windows application with multiple views

提问by Peter Olsson

I'm rewriting an old application and use this as a good opportunity to try out C# and .NET development (I usually do a lot of plug-in stuff in C).

我正在重写一个旧的应用程序,并以此作为尝试 C# 和 .NET 开发的好机会(我通常用 C 做很多插件的东西)。

The application is basically a timer collecting data. It has a start view with a button to start the measurement. During the measurement the app has five different views depending on what information the user wants to see.

该应用程序基本上是一个收集数据的计时器。它有一个带有开始测量按钮的开始视图。在测量过程中,应用程序有五种不同的视图,具体取决于用户想要查看的信息。

What is the best practice to switch between the views? From start to running? Between the running views?

在视图之间切换的最佳做法是什么?从开始到运行?在运行视图之间?

Ideas:

想法:

  • Use one form and hide and show controls
  • Use one start form and then a form with a TabControl
  • Use six separate forms
  • 使用一种形式并隐藏和显示控件
  • 使用一个起始表单,然后使用一个带有 TabControl 的表单
  • 使用六种不同的表格

采纳答案by Chris Karcher

Creating a bunch of overlaid panels is a design-time nightmare.

创建一堆重叠面板是设计时的噩梦。

I would suggest using a tab control with each "view" on a separate tab, and then picking the correct tab at runtime. You can avoid showing the tab headers by putting something like this in your form's Load event:

我建议在单独的选项卡上使用带有每个“视图”的选项卡控件,然后在运行时选择正确的选项卡。您可以通过在表单的 Load 事件中放置类似的内容来避免显示选项卡标题:

tabControl1.Top = tabControl1.Top - tabControl1.ItemSize.Height;
tabControl1.Height = tabControl1.Height + tabControl1.ItemSize.Height;
tabControl1.Region = new Region(new RectangleF(tabPage1.Left, tabPage1.Top, tabPage1.Width, tabPage1.Height + tabControl1.ItemSize.Height));

回答by Adam Haile

Tabbed forms are usually good... but only if you want the user to be able to see any view at any time... and it sounds like you might not.

选项卡式表单通常很好……但前提是您希望用户能够随时看到任何视图……听起来您可能不会。

Separate forms definitely works, but you need to make sure that the switch is seemless...if you make sure the new form appears the same exact size and location of the old form, it will look like it thew same for with changing controls.

单独的表单肯定可以工作,但是您需要确保切换是无缝的……如果您确保新表单的大小和位置与旧表单完全相同,那么更改控件时看起来会一样。

The method I often use is actually to pre-setup all my controls on individual "Panel" controls and then show and hide these panels as I need them. The "Panel" control is basically a control container... you can move the panel and all controls on it move relative. And if you show or hide the panel, the controls on it do the same. They are great for situations like this.

我经常使用的方法实际上是在单个“面板”控件上预先设置我的所有控件,然后根据需要显示和隐藏这些面板。“Panel”控件基本上是一个控件容器……您可以移动面板,其上的所有控件都可以相对移动。如果您显示或隐藏面板,其上的控件也会执行相同的操作。它们非常适合这样的情况。

回答by Brian Ensink

The method I often use is actually to pre-setup all my controls on individual "Panel" controls and then show and hide these panels as I need them.

我经常使用的方法实际上是在单个“面板”控件上预先设置我的所有控件,然后根据需要显示和隐藏这些面板。

Instead of making each view a panel within a single form you could make each view a UserControl. Then create a single form and write code to create and display the correct UserControl in the Form and to switch from one to the next. This would be easier to maintain because you will have a separate class for each view instead of a single Form class with 6 panels each with their own controls -- that seems difficult and error prone to maintain.

您可以将每个视图设为 UserControl,而不是将每个视图设为单个表单中的面板。然后创建单个窗体并编写代码以在窗体中创建和显示正确的 UserControl,并从一个窗体切换到另一个窗体。这将更容易维护,因为您将为每个视图创建一个单独的类,而不是一个具有 6 个面板的单个 Form 类,每个面板都有自己的控件——这似乎很难维护并且容易出错。

回答by Hath

What I do is to have a Panel where your different views will sit on the main form. then create user controls for your different views.

我所做的是有一个面板,您的不同观点将位于主窗体上。然后为您的不同视图创建用户控件。

Then when I want to switch between a'view' you dock it to Panel on the main form.. code looks a little like this.

然后,当我想在“视图”之间切换时,将它停靠在主窗体上的面板上.. 代码看起来有点像这样。

i preffer this because you can then reuse your views, like if you want to open up a view in a tab you can dock your user controls inside tab pages.. or even inherit from tabpage instead of usercontrol to make things a bit more generic

我更喜欢这个,因为你可以重用你的视图,比如如果你想在选项卡中打开一个视图,你可以将用户控件停靠在选项卡页面内..或者甚至从 tabpage 继承而不是 usercontrol 使事情更通用

public partial class MainForm : Form
{
    public enum FormViews
    {
        A, B
    }
    private MyViewA viewA; //user control with view a on it 
    private MyViewB viewB; //user control with view b on it

    private FormViews _formView;
    public FormViews FormView
    {
        get
        {
            return _formView;
        }
        set
        {
            _formView = value;
            OnFormViewChanged(_formView);
        }
    }
    protected virtual void OnFormViewChanged(FormViews view)
    {
        //contentPanel is just a System.Windows.Forms.Panel docked to fill the form
        switch (view)
        {
            case FormViews.A:
                if (viewA != null) viewA = new MyViewA();
                //extension method, you could use a static function.
                this.contentPanel.DockControl(viewA); 
                break;
            case FormViews.B:
                if (viewB != null) viewB = new MyViewB();
                this.contentPanel.DockControl(viewB);
                break;
        }
    }

    public MainForm()
    {

        InitializeComponent();
        FormView = FormViews.A; //simply change views like this
    }
}

public static class PanelExtensions
{
    public static void DockControl(this Panel thisControl, Control controlToDock)
    {
        thisControl.Controls.Clear();
        thisControl.Controls.Add(controlToDock);
        controlToDock.Dock = DockStyle.Fill;
    }
}