I need to change testAppGUI’s (testAppGUI is a form) visible property from another function. The function is in a separate file and it’s not in a class.
If I try to do
testAppGUI::Visible = false;
I just get error
C2597: illegal reference to non-static member ‘System::Windows::Forms::Control::Visible’
And if I try create a instance of the object like this
testAppGUI^ formProperty = gcnew testAppGUI;
and then do
formProperty->Visible = false; nothing happens?!
Can anybody explain how to do this?
Thanks in advance.
EDIT: Here is some more code
In 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;
}
In 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 and showGUI is declared in HideAndShowGUI.h
If you already have an instance of the form which you want to hide, you will have to pass a reference to that form to the function where you want to change the property.
You can do this either by directly supplying the form as a parameter of the function or if the function is a member of a class, you can pass the form to (and instance of) the class (and store it as a member variable). Which of these is more appropriate for you depends on your specific context, which we do not have access to without some more of your code.
Note: Your first snipet is conflicting with your second: in the first you are using
form1as a variable, in the second as a type. If you already have the variableform1, you can just set its property: