从子窗体更改父窗体中的值

本文关键字:窗体 | 更新日期: 2023-09-27 18:05:56

我尝试了不同的解决方案,但没有运气…我不知道该怎么处理。以下问题:

我有一个主表单(Form1)和一个子表单(splashScreen)。

我的splashScreen中的代码:

    public splashScreen()
    {
        InitializeComponent();
    }
    public splashScreen(Form1 frm1)
    {
        form1 = frm1;
        InitializeComponent();
    }
    private static splashScreen m_instance = null;
    private static object m_instanceLock = new object();
    public static splashScreen GetInstance()
    {
        lock (m_instanceLock)
        {
            if (m_instance == null)
            {
                m_instance = new splashScreen();
            }
        }
        return m_instance;
    }

在我的Form1中,我正在创建一个新线程并开始我的splashScreen。我在splashScreen中调用控件的方式如下:

    splashScreen splashObj = splashScreen.GetInstance();
                if (splashObj.InvokeRequired)
                   {
                       splashObj.Invoke((MethodInvoker)delegate()
                       {
                           splashObj.Show();
                       }
                       );
                   }
                   else
                   {
                       splashObj.Show();
                   }

现在,当我的Form1正在工作时,splashScreen开始显示当前进程。在splash屏幕上我有一个"取消"按钮。当我点击那个按钮时,我想改变一个变量"killProc"-这是在我的Form1-为"真",这样在Form1的工作可以通过返回语句停止,当在某些时候"if(killProc)"返回真。

我如何改变变量在我的Form1通过我的溅屏或甚至有一个更好的方式?

从子窗体更改父窗体中的值

GetInstance方法中使用splashScreen(Form1 frm1)构造函数来构造实例。如果您有来自SplashScreen的对父节点的引用,则可以使用set属性。

public static splashScreen GetInstance(Form1 frm1)
    {
        lock (m_instanceLock)
        {
            if (m_instance == null)
            {
                m_instance = new splashScreen(frm1);
            }
        }
        return m_instance;
    }

所以,从SplashScreen

form1.killProc = true;