从另一个函数更改表单“可见”属性?

时间:2012-06-16 11:49:42

标签: c++ .net winforms forms properties

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

如果我尝试

testAppGUI::Visible = false;

我刚收到错误

  

C2597:对非静态成员的非法引用'System :: Windows :: Forms :: Control :: Visible'

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

testAppGUI^ formProperty = gcnew testAppGUI;

然后再做

formProperty->Visible = false; nothing happens?!

任何人都可以解释如何做到这一点吗?

提前致谢。

编辑:这是一些更多的代码

在testApp.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;
}

在testAppGUI.h中

#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();
         }
};
}

HideAndShowGUI.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中声明

1 个答案:

答案 0 :(得分:2)

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

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

注意:您的第一个snipet与您的第二个snipet冲突:第一个是使用form1作为变量,第二个是类型。如果您已经拥有变量form1,则可以设置其属性:

form1->Visible = false;