从另一个函数更改窗体"visible"属性?

Change a forms "visible" property from another function?

本文关键字:visible 属性 窗体 另一个 函数      更新时间:2023-10-16

我需要从另一个函数更改testAppGUI的(testAppGUI是一种表单)的可见属性。该函数位于单独的文件中,不在类中。

如果我尝试做

testAppGUI::Visible = false;

我只是收到错误

C2597:非法引用非静态成员"系统::窗口::窗体::控件::可见"

如果我尝试像这样创建对象的实例

testAppGUI^ formProperty = gcnew testAppGUI;

然后做

formProperty->Visible = false; nothing happens?!

谁能解释一下如何做到这一点?

提前谢谢。

编辑:这是更多代码

在测试应用中.cpp

#include "stdafx.h"
#include "testAppGUI.h"
using namespace testApp;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 
    Application::Run(gcnew testAppGUI());
    return 0;
}

在测试应用图形用户界面中

#pragma once
#include "HideAndShowGUI.h"
namespace testApp {
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::IO;
    public ref class testAppGUI : public System::Windows::Forms::Form
    {
    public:
        testAppGUI(void)
        {
            InitializeComponent();
        }
    protected:
        ~testAppGUI()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    ...
#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            ...
        }
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             hideGUI();
         }
};
}

隐藏和显示GUI

.cpp
#include "stdafx.h"
#include "testAppGUI.h"
using namespace testApp;
void hideGUI(){
    //Hide the form, this function should be able to be called by all functions in the program. Not just from forms!
}
void showGUI(){
    //Unhide/Show the form, this function should be able to be called by all functions in the program. Not just from forms!
}

hideGUI 和 showGUI 在 HideAndShowGUI.h 中声明

如果您已经有一个要隐藏的表单实例,则必须将对要更改属性的函数传递对该表单的引用。

您可以通过直接提供表单作为函数的参数来执行此操作,或者如果函数是类的成员,则可以将表单传递给类(和类的实例)(并将其存储为成员变量)。 其中哪一个更适合您取决于您的特定上下文,如果没有您的更多代码,我们就无法访问该上下文。

注意:您的第一个狙击手与第二个狙击手冲突:在第一个狙击中,您将form1用作变量,在第二个狙击手中用作类型。 如果您已经有变量 form1 ,则可以设置其属性:

form1->Visible = false;