在C#中从类A更新类B中的GUI组件, [英] Updating GUI components in class B from class A in c#

查看:379
本文介绍了在C#中从类A更新类B中的GUI组件,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我看到我在这篇文章中得到了很多糟糕的结果。我试图解释我试图做什么,我的错误和在哪个方向想去。我要求洞察我做错了什么。如果你downvote;请求告诉我为什么,所以我可以改善的问题。非常感谢。

I see I'm getting a lot of downvotes on this post. I've tried to explain what I try to do, where my errors and in which direction want to go. I'm asking for insight in what I'm doing wrong. If you downvote; pleas tell me why, so I can improve the question. Thanks.

我创建一个应用程序,其中有一个主窗体,以及用户操作的几个不同的用户控件。这有助于我拆分代码,管理程序的不同部分。

I'm creating an application where I have one main form, and several different User Controls which the user works on. This helps me splitting the code, managing the different parts of the program. And it would be easy to expand the application later on.

我试图创建一个类,其中我管理活动控件我想调用该类中的一个函数

I'm trying to create a class where I manage the active controls I want to call one function in that class with as argument the form that should become active.

图片可以说明我如何尝试设置此应用程序。 注意,控制管理器类不是下面显示的代码中的独立类,但是部分类主体。任何提示如何得到它像图像是非常欢迎:)

An image can illustrate how I try to setup this application. Note that the control manager class is not a seperate class in the code i show below, but a partial class of the mainform. Any tips on how to get it like in the image are very welcome :)

管理所有活动窗体的类如下所示:请注意,所有用户控件只是一个带有一些按钮/文本框等的用户控件。尚未添加代码。

The class to manage all active forms looks like this: Please note that all user controls are just a user control with some buttons/textboxes etc on it. No code is added at all yet.

    public partial class STP2Main 
        {
// I make each UserControl accessable for the whole class
            SetupDeviceControl.SetupDevice SetupDev = new SetupDeviceControl.SetupDevice();
            GenConfigFileControl.GenConfigFileControl GenConfFile = new GenConfigFileControl.GenConfigFileControl();
            Monitoring.MonitoringControl Monitor = new Monitoring.MonitoringControl();
            GenEncKeyControl.GenEncKeyControl GenEncKey = new GenEncKeyControl.GenEncKeyControl();
            MenuControl.MenuControl MenuControl = new MenuControl.MenuControl();        

            public void SelectActiveWindow()
            {
                // Any active control should be hidden thats what this function does:
                HideCurrentActiveControl();
                // Check whether the window is already created
                if (!WindowExists())
                { // if not created; create the windows: 
                    switch (STP_Design.ProgramParameters.C.NextActiveControl)
                    {
                        case STP_Data.Data.SetupDeviceControl: // control 1:
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.SetupDeviceControl;
                            STP_Design.ProgramParameters.C.SetupDeviceControlIsCreated = true;
                            SetupDev.Parent = this;
                            SetupDev.Location = new Point(3, 30);
                            SetupDev.Show();
                            SetupDev.BringToFront();
                            break;
                        case STP_Data.Data.MonitoringControl: //control 2:
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.MonitoringControl;
                            STP_Design.ProgramParameters.C.MonitoringControlIsCreated = true;
                            Monitor.Parent = this;
                            Monitor.Location = new Point(3, 125);
                            Monitor.Show();
                            Monitor.BringToFront();
                            break;
                        case STP_Data.Data.MenuControl: // control 3
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.MenuControl;
                            STP_Design.ProgramParameters.C.MenuControlIsCreated = true;  
                            MenuControl.Location = new Point(3, 30);
                            MenuControl.Parent = this;
                            MenuControl.Show();
                            MenuControl.BringToFront();
                            break;
                    }
                }
                else
                { // window is already created so needs to be called to front again:
                    switch (STP_Design.ProgramParameters.C.NextActiveControl)
                    {
                        case STP_Data.Data.SetupDeviceControl:
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.SetupDeviceControl;
                            SetupDev.BringToFront();
                            break;
                        case STP_Data.Data.MonitoringControl:
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.MonitoringControl;
                            Monitor.Visible = true;
                            Monitor.BringToFront();
                            break;
                        case STP_Data.Data.AdvancedMenu:
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.AdvancedMenu;
                            tabControl1.Visible = true;
                            tabControl1.BringToFront();
                            break;

                        case STP_Data.Data.MenuControl:
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.MenuControl;
                            MenuControl.Visible = true;
                            MenuControl.BringToFront();
                            break;
                    }

                }
                btnMenu.BringToFront();

            }
    // some functions which are called above are not shown; not relevant for this question
    }

我遇到的是以下情况:在所有。但控制根本不改变。如果我调用一个窗口,它只创建一次,因为我做了它作为我的Mainform的部分类。 (我试过一个完整的单独的类,这导致错误的线程,由于我不是一个有经验的c#程序员,我试图避免使用部分类。)

What I experience is the following: I get no errors at all. But the controls simply not change at all. If I call a window, it is created only once, because I did make it as partial class of my Mainform. (I've tried a complete seperate class, which did result in errors with threading, As I am not an experienced c# programmer, I tried to avoid that using a partial class.)

我将添加另一个函数;它不会做任何事情:

I'll add another function; which does not do anything at all:

    private void HideCurrentActiveControl()
    {
        switch (STP_Design.ProgramParameters.C.CurrentActiveControl)
        {
            case STP_Data.Data.SetupDeviceControl:
                SetupDev.Visible = false;
                break;
            case STP_Data.Data.MonitoringControl:
                tabControl1.Visible = false;
                Monitor.Visible = false;
                break;
            case STP_Data.Data.GenConfFileControl:
                GenConfFile.Visible = false;
                break;
            case STP_Data.Data.GenEncKeyControl:
                GenEncKey.Visible = false;
                break;
            case STP_Data.Data.MenuControl:
                MenuControl.Visible = false;
                break;
            case STP_Data.Data.AdvancedMenu:
                tabControl1.Visible = false;
                break;
            default:
                tabControl1.Visible = false;
                break;

        }
    }

的代码,它执行的语句,但我看不到任何更改。

I've tried debugging this part of code and it executes the statements, but I see no changes at all.

我想我已经显示了我想做的;和我如何尝试这样做。我的问题是:我如何访问这些形式,所以我可以从一个单独的类(或在这种情况下,主要形式的部分类)管理它们。

I think I've shown what I am trying to do; and how I try to do that. My question is: How do I acces those forms so I can manage them from a seperate class (or in this case partial class of the main form).

这最后的功能,做一些奇怪的东西。在我调用SelectActiveWindow()函数之前,我将变量STP_Design.ProgramParameters.C.NextActiveControl更新为例如:... AdvancedMenu。 (这是之前... MenuControl)但它总是显示它仍然是MenuControl。在我的代码中没有地方,我改变这个值,除了在我开始之前的功能。 (我也试图使nextcontrol作为函数SelectActiveWindow()的参数,但是这样做)

Then I have this last function, which does some wierd things. Before I call the SelectActiveWindow() function I update the variable STP_Design.ProgramParameters.C.NextActiveControl to for example: ...AdvancedMenu. (this was before that ...MenuControl) But it does always show that it is still MenuControl. Nowhere in my code is something where I change that value besides right before I start the function. (I've also tried to make the nextcontrol as an argument of the function SelectActiveWindow() but this did the same)

    private bool WindowExists()
    {
        switch (STP_Design.ProgramParameters.C.NextActiveControl)
        {
            case STP_Data.Data.SetupDeviceControl:
                if (STP_Design.ProgramParameters.C.SetupDeviceControlIsCreated)
                    return true;
                else
                    return false;
            case STP_Data.Data.MonitoringControl:
                if (STP_Design.ProgramParameters.C.MonitoringControlIsCreated)
                    return true;
                else
                return false;
            case STP_Data.Data.GenConfFileControl:
                if (STP_Design.ProgramParameters.C.GenConfFileIsCreated)
                    return true;
                else
                    return false;
            case STP_Data.Data.GenEncKeyControl:
                if (STP_Design.ProgramParameters.C.GenEncKeyControlIsCreated)
                    return true;
                else
                    return false;
            case STP_Data.Data.AdvancedMenu:
                return true;
            case STP_Data.Data.MenuControl:
                if (STP_Design.ProgramParameters.C.MenuControlIsCreated)
                    return true;
                else
                    return false;

            default:
                return false;
        }
    }

我正在寻找的地方:
我有一个主要的形式,显示不同的用户控制。我试图创建一个单独的类,可访问从我的项目中的每个控件/窗体。这个类应该管理显示的控件。

Summery of what I am looking for: I am having a main form where display different user controlls in. I am trying to create a seperate class which is accessable from each control/form in my project. This class should manage the controls which are shown. In the code above I illustrated how I tried to do this, but this does not result in the expected result.

推荐答案

确定,在这个例子中,现在我明白所需的上下文。我们实际上在我的程序中做了非常相似的事情。下面是我们如何做的基本概要...

Ok, Now I understand the context needed. We actually do something very similar in my program. Here is a basic outline of how we do it...

布局

在主窗体上,我们有一个 Panel 容器,我们称之为 pnlMain 。正是这个控件,我们添加和删除活动用户控件。我们还在表示 curActiveControl 的表单上在全局级别上有一个 UserControl 对象。

On the main form we have a Panel container that we call pnlMain. It is this control that we add and remove active user controls from. We also have a UserControl object at a global level on the form representing curActiveControl.

代码

当用户通过其中一个菜单选择窗口时,我们运行一个类似于

When the user selects a window via one of the menu's, we run a function that looks like this

switch (UserSelection)
{
    case "Page 1":
        if(curActiveControl.GetType() != typeOf(Page1Control))
        {
            pnlMain.Controls.Remove(curActiveControl);
            curActiveControl = new Page1Control();
            //do setup and configuration things
            pnlMain.Controls.Add(curActiveControl);
        }
        //do some post processing things
        break;
    //other pages/specific page controls
}

Refresh();

这种特定方法的缺点是页面本身不是持久性的,变量,您希望在一个会话而不是只在一个页面上有活动,你必须将它们存储在一些其他全局对象,并从用户控件的Load或Constructor方法重新加载它们。

The downside to this specific method is that the pages themselves are not persistent, so if there are entries or variables you want to have active across a session rather than only while on a page, you have to store them in some other global object and reload them from the user control's Load or Constructor methods.

你可以做同样的事情,而不是每次创建一个新的控制实例 curActiveControl 它与新控件的备用实例。注意参考和覆盖虽然,它不是我个人以前尝试过的。

You could do this same thing but instead of creating a new control instance each time for curActiveControl you could simply replace it with the standby instance of the new control. Be careful with referencing and overwriting though, its not something I personally have tried before.

我们使用的方法的关键是面板,用户控件。而不是调整大量用户控件的可见性和Z顺序,我们只是更改主面板中显示的控件,其他控件在任何给定的时间点都不存在。

The key in the method we use is the Panel that holds the user controls. Rather than adjusting the visibility and Z-Order of a large number of user controls, we simply change the displayed control in the main panel and the other control don't even exist at any given point in time.

另一个皱纹是这个功能直接在我们的主窗体上。我不知道这将如何工作作为另一个部分类。它绝对值得一试。

The other wrinkle is that this functionality is directly on our Main Form. I Don't know how well this will work as another partial class. Its definitely worth a try though.

这篇关于在C#中从类A更新类B中的GUI组件,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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