Change a forms "visible" property from another function? - c++

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 form1 as a variable, in the second as a type. If you already have the variable form1, you can just set its property:
form1->Visible = false;

Related

MSB4236 The SDK Microsoft.NET.Sdk specified could not be found

I've created a CLR (.NET Core) project and added a Windows Form there. I didn't change any code and tried to run the program. Then the error MSB4236 The SDK Microsoft.NET.Sdk specified could not be found comes in.
enter image description here
The header file code:
`#pragma once
namespace Project21 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
}
protected:
~MyForm()
{
if (components)
{
delete components;
}
}
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->components = gcnew System::ComponentModel::Container();
this->Size = System::Drawing::Size(300,300);
this->Text = L"MyForm";
this->Padding = System::Windows::Forms::Padding(0);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
}
#pragma endregion
};
} `

Accessing a class object from outside

I created a basic project on the TouchGFX Designer and wrote a function in my own cpp file using touchgfx library. I want that when the button is clicked ,the function is called to ScreenView.cpp or ScreenViewBase.cpp from my own cpp file and change the color of the box.
This is my cpp file.
#include <touchgfx/Color.hpp>
#include <touchgfx/widgets/Box.hpp>
#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
#include <touchgfx/widgets/Box.hpp>
ChangeColor::ChangeColor()
{
Screen1View::box1;
box1.setColor(touchgfx::Color::getColorFrom24BitRGB(51, 168, 35));
box1.invalidate();
}
This is the Screen1View.cpp where I want to call my function.
#include<gui/ChangeColor.hpp>
#include <touchgfx/Color.hpp>
Screen1View::Screen1View():
buttonCallback(this, &Screen1View::buttonCallbackHandler)
{
}
void Screen1View::setupScreen()
{
Screen1ViewBase::setupScreen();
button1.setAction(buttonCallback);
}
void Screen1View::tearDownScreen()
{
Screen1ViewBase::tearDownScreen();
}
void Screen1View::buttonCallbackHandler(const touchgfx::AbstractButton& src)
{
if (&src == &button1)
{
//Interaction1
//When button1 clicked execute C++ code
//Execute C++ code
//ChangeColor();
ChangeColor();
}
}
and this is the Screen1BaseView.hpp where the box is declared
#define SCREEN1VIEWBASE_HPP
#include <gui/common/FrontendApplication.hpp>
#include <mvp/View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
#include <touchgfx/widgets/Box.hpp>
#include <touchgfx/widgets/Button.hpp>
class Screen1ViewBase : public touchgfx::View<Screen1Presenter>
{
public:
Screen1ViewBase();
virtual ~Screen1ViewBase() {}
virtual void setupScreen();
protected:
FrontendApplication& application() {
return *static_cast<FrontendApplication*>(touchgfx::Application::getInstance());
}
/*
* Member Declarations
*/
touchgfx::Box __background;
touchgfx::Box box1;
touchgfx::Button button1;
private:
};
I can't access the box. Is there way to do it?
TouchGFX uses MPV architecture, i.e. Model-Presenter-View. The point is Model and View can not access each other.
In your case, your cpp file play role as a model, so it's not allowed to access element in View. That's the reason why you can't access. You need a Presenter to handle the connection.

How to invoke/call performClick of menuitem of an MDI parent from another form?

I have a problem, sorry for this newbie question (just a 2 week visual c++ programming experience). I am currently working with MDI in Visual C++2010, here is how the program works, once you execute the application, the MDIParent loads in maximize state, then the MDIParent has MenuStrip and one of its subMenu is named noviceToolStripMenuItem. If noviceToolStripMenuItem is clicked using the mouse button, it will open a child form called frmNovice, this works out fine, now the frmNovice contains a simple game which works out according to its intended function, once the game is over a new form named frmRetry will be shown but this time frmRetry is not a childform and the frmNovice will close. frmRetry has two buttons, Yes and No, if the user clicks the No the frmRetry will close and the MDIParent will then again holds the focus of the application, but if the user clicks the Yes button, the application should perform the click event/function of noviceToolStripMenuItem, to show up again the frmNovice, but this is the problem, i can't get this done. frmRetry (not a child form) to invoke or call the click event noviceToolStripMenuItem of the MDIParent. Is there any way to do this, or maybe other solution? thanks in advance.
This are the codes (some are deleted to minimize space):
**FILE: mdiMain.h**
#pragma once
#include "frmNovice.h"
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form {
public:
Form1(void)
{
InitializeComponent();
//TODO: Add the constructor code here
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::MenuStrip^ menuStrip1;
protected:
private: System::Windows::Forms::ToolStripMenuItem^ noviceToolStripMenuItem;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->selectLevelToolStripMenuItem = (gcnew
System::Windows::Forms::ToolStripMenuItem());
this->noviceToolStripMenuItem = (gcnew
this->selectLevelToolStripMenuItem->Name = L"selectLevelToolStripMenuItem";
this->selectLevelToolStripMenuItem->Size = System::Drawing::Size(70, 20);
this->selectLevelToolStripMenuItem->Text = L"New &Game";
//
// noviceToolStripMenuItem
//
}
#pragma endregion
private: System::Void noviceToolStripMenuItem_Click(System::Object^ sen....) {
//OPENING NEW FORM AS MDI CHILD
frmNovice^ newMDIChild = gcnew frmNovice();
// Set the Parent Form of the Child window.
newMDIChild->MdiParent = this;
// Display the new form.
newMDIChild->Show();
}
};
}
FILE: mdiMain.cpp
#include "stdafx.h"
#include "mdiMain.h"
using namespace MemoryGame;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
**FILE: frmNovice.h**
#pragma once
#include "frmRetry.h"
#include "algorithm"
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class frmNovice : public System::Windows::Forms::Form
{
public:
frmNovice(void)
{
InitializeComponent();
//TODO: Add the constructor code here
}
protected:
~frmNovice()
{
if (components)
{
delete components;
}
}
//codes for the UI of the game goes here.....
#pragma region Windows Form Designer generated code
//MY USER DEFINED FUNCTION START
void gamefunction() //this function is where calls the frmRetry to show
{
frmRetry^ form = gcnew frmRetry();
form->Show(); //This time not a CHILD FORM
this->Close();
}
void InitializeComponent(void)
{
//initialize UI components GOES HERE
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//DIFFERENT PROCEDURE FUNCTIONS GOES HERE Also invokes/calls the game function that
//show the frmRetry (this is working properly)
};
}
FILE: frmNovice.cpp
#include "StdAfx.h"
#include "frmNovice.h"
//there are variables used for gameplay
int Choice1;
int Choice2;
int Mistakes;
int TimeRecord;
int AllItems;
and LASTLY
FILE: frmRetry.h
#pragma once
namespace MemoryGame {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class frmRetry : public System::Windows::Forms::Form
{
`enter code here`public:
frmRetry(void)
{
InitializeComponent();
}
protected:
~frmRetry()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ btnYes;
private: System::Windows::Forms::Button^ btnNo;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
CODES for UI inialization goes here
}
#pragma endregion
private: System::Void btnNo_Click(System::Object^ .....e) {
this->Close();
}
private: System::Void btnYes_Click(System::Object.....^ e) {
**THIS IS THE PART IM STuck WITH, i CANT GET THIS DONE**
**EVErything is working fine, except for this part where i have to
call again the frmNovice as an child form of the mdiParent**
THE following are my attempts but didnt work.
// frmRetry^ form = gcnew frmRetry();
// frmNovice^ frmNovice=gcnew frmNovice();
// noviceToolStripMenuItem->PerformClick();
//frmNovice->Show();
}
};
}
FILE: frmRetry.cpp
#include "StdAfx.h"
#include "frmRetry.h"
THANKS IN ADVANCE
You should pass in a reference to the main window when you create the Novice form, and pass this reference again to the Retry form. Then, have the main window implement a NoviceParent interface, which includes a public method for the Retry form to call. The noviceToolstripMenuItem_Click method on the main window will also delegate to this new public method.
Novice Parent Interface
public interface class INoviceParent
{
public:
void ShowNovice();
}
Main Window
#include "INoviceParent.h"
public ref class Form1 : public System::Windows::Forms::Form, INoviceParent {
public:
// ... existing public methods
void ShowNovice() {
//OPENING NEW FORM AS MDI CHILD
frmNovice^ newMDIChild = gcnew frmNovice(this);
// Set the Parent Form of the Child window.
newMDIChild->MdiParent = this;
// Display the new form.
newMDIChild->Show();
}
private:
void noviceTooltipMenuItem_Click( /* args */) {
ShowNovice();
}
// ...
};
Novice Form
#include "INoviceParent.h"
public ref class frmNovice : public System::Windows::Forms::Form
{
public:
frmNovice(INoviceParent ^ const parent)
{
this->parent = parent;
// ...
}
// ...
private:
void gamefunction() //this function is where calls the frmRetry to show
{
frmRetry^ form = gcnew frmRetry(parent);
form->Show(); //This time not a CHILD FORM
this->Close();
}
INoviceParent ^parent;
}
Retry Form
#include "INoviceParent.h"
public ref class frmRetry : public System::Windows::Forms::Form
{
public:
frmRetry(INoviceParent ^ const noviceParent)
{
this->noviceParent = noviceParent;
// ...
}
// ...
private: System::Void btnYes_Click(System::Object.....^ e) {
noviceParent->ShowNovice();
}
INoviceParent ^noviceParent;
};
The important thing here is that the sub-forms don't need to be strongly coupled to the main form or to each other. All they know is that the object passed in their constructor has a public method called ShowNovice(). The Retry form calls it when the user clicks the "Yes" button, and the main window does whatever it does when the user clicks its noviceTooltipMenuItem.

How to access a variable from the parent class

How can I access a variable from a parent class? I thought the below code would do this, but when I try to print out the value of name in Controller.cpp I get the error:
Member access into incomplete type 'TestApp'
TestApp.cpp
#include "cinder/app/AppNative.h"
#include "Controller.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class TestApp : public AppNative
{
public:
void setup();
void update();
string name = "Parent";
Controller controller;
};
void TestApp::setup()
{
controller.setup(this);
}
void TestApp::update()
{
controller.update();
}
CINDER_APP_NATIVE( TestApp, RendererGl )
Controller.h
#pragma once
class TestApp;
class Controller
{
public:
void setup(TestApp* parent);
void update();
TestApp* p;
};
Controller.cpp
#include "Controller.h"
void Controller::setup(TestApp* parent)
{
p = parent;
}
void Controller::update()
{
std::cout << p->name << std::endl;
}
This has nothing to do with parent classes. TestApp is defined in TestApp.cpp, it's not visible to the code in Controller.cpp. You need to move the definition of TestApp to a header file (called TestApp.h say) and then #include "TestApp.h" in Controller.cpp.
You need to put the TestApp class definition in a header, and include that in TestApp.cpp and Controller.cpp.
Class Controller knows nothing about what dara members class TestApp has. It knows only that there is class TestApp that is defined somewhere else.

'Error: expected a type' creating a new instance

I have a problem and I hope you can help me.
I'm creating a C++ program which is running on linux.
I have defined two classes, where the main one is called Downloader and looks like this:
#ifndef __DOWNLOADER_H__
#define __DOWNLOADER_H__
#include "configDownloader.h"
#include "mailServer.h"
#include <logger.h>
using namespace ObjectModel;
namespace Downloader
{
class Downloader
{
private:
...
MailServer *m_mailServer;
public:
Downloader(char* configFileName);
...
};
}
#endif
In the constructor of this class, I have tried to create a new instance of the class MailServer, which I defined into the same Namespace. The code of MailServer looks this way:
#ifndef __MAILSERVER_H__
#define __MAILSERVER_H__
#include <stdio.h>
#include <list>
#include <mail.h>
using namespace std;
using namespace ObjectModel;
namespace Downloader
{
class MailServer
{
private:
list<Mail> m_mails;
char *m_username;
char *m_password;
public:
MailServer();
~MailServer();
void Open(char *username,char *password);
bool SaveEmails(char *pathFiles);
void Close();
};
}
#endif
The constructor of this class is defined correctly into the .cpp file and everything seems correct.
The problem is that when I try to create a new instance of MailServer inside the constructor of Downloader, the compiler says "error: expected a type"
#include <stdio.h>
#include "downloader.h"
#include <unistd.h>
namespace Downloader
{
Downloader::Downloader(char* fileName)
{
this->m_config = new ConfigDownloader(fileName);
this->m_log = new Logger("Log",LOG_LEVEL_INFO,0);
this->m_mailServer = new MailServer();//the compiler shows the error right here
}
...
any ideas? I read somewhere that it could be that the compiler is too old, but I don't feel really comfortably coding inside the makefile.
I found the solution! I'm sorry because i didn't see it. The problem was that i defined a public getter to return the private field like this:
class Downloader
{
private:
ConfigDownloader* m_config;
Logger* m_log;
MailServer *m_mailServer;
public:
MailServer *MailServer();
being that both was define into the same scope, the compiler could be confused about the constructor of the class and the method, because both were called with the same name. The problem was mine! But everyone should take care about this, because intellisense doesn't tells you anything about it