Problem with class and form
Hello, I'm new to the forum and also kinda new to VC++ (but familiar with C++)..
Im making ths application which uses Form. I have form with many buttons and I have a class with methods.
Where and how should I create my object so I could access it through form elements(like clicking on button)? Use seters and geters with buttons etc..
Please help me, I'm searching for this for two days now...
Re: Problem with class and form
You can create a member variable of your method class in your form/dialog class. You can also create a variable of method class in onClick function of your button. see following example.
Code:
void CTestingDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
MethodClass mClass;
mClass.DoProcess();
OnOK();
}
void CTestingDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
MethodClass *pMClass = new MethodClass();
pMClass->DoProcess();
delete pMClass;
OnOK();
}
Can you please post your code sample so I can answer more accurately.
Re: Problem with class and form
I'm totally new to Visual C++ (I made a new widows form project with Visual Studio). I added two buttons on form(dragged on form).
Example would be something like this:
Form1.h:
private: System::Void PlayBtn_Click(System::Object^ sender, System::EventArgs^ e) {
/*
call class method that increments value
*/
}
private: System::Void StopBtn_Click(System::Object^ sender, System::EventArgs^ e) {
/*
call class method that decrement value
*/
}
And in class MyClass.h:
#pragma once
ref class MyClass
{
private:
int val;
public:
MyClass(void);
void addVal();
void delVal();
};
MyClass.cpp:
#include "StdAfx.h"
#include "Field.h"
void addVal()
{
val++;
}
void delVal()
{
val--;
}
Form1.h is Visual Studio generated code with all the information about the form etc. I include the "MyClass.h" in "Form1.h" at the top of it.
#pragma once
#include "MyClass.h"
My question is where should I create class MyClass object in Form1.h so I could access it form PlayBtn_Click() and StopBtn_Click()?
And for now I'm creating object like:
MyClass^ myc = gcnew MyClass();
Re: Problem with class and form
.Net is off topic in this forum. Look for the managed C++ forum.
CString to be displayed in a single line in the display grid.
i am trying to add multiple strings in a resultant cstring variable , but in case some of these strings are of length greater than 1000 , the resultant string is stored and displayed (in a grid/text visualizer) in multiple lines. how to display/store the string in a single line.
Re: Problem with class and form
Code:
Form1.h:
private:
MyClass m_MyClass; // Member variable of Form1 class
Form1() // Constructor
{
m_MyClass = gcnew MyClass();
}
private: System::Void PlayBtn_Click(System::Object^ sender, System::EventArgs^ e) {
/*
call class method that increments value
m_MyClass->addVal();
*/
}
private: System::Void StopBtn_Click(System::Object^ sender, System::EventArgs^ e) {
/*
call class method that decrement value
m_MyClass->delVal();
*/
}
Re: Problem with class and form
Thank you for your answer, I created the object in Form1() constructor:
MyClass^ myc = gcnew MyClass();
but then I get error:
" left of '->addVal' must point to class/struct/union/generic type "
and even Intelisense dosen't show me anything when I start typing..
Re: Problem with class and form
Quote:
Originally Posted by
Dawiss
Thank you for your answer, I created the object in Form1() constructor:
MyClass^ myc = gcnew MyClass();
but then I get error:
" left of '->addVal' must point to class/struct/union/generic type "
and even Intelisense dosen't show me anything when I start typing..
Again, you're using a language that is off topic for this forum. Managed C++ and .Net is quite different from the Visual C++ discussed here.
Re: Problem with class and form
did you #include MyClass.h in Form1.h file. Also please make sure your are using same namespaces in both files, Other wise use the namespace statement in form1.h file.
Please move this thread to .Net forum or re-post over there.
Re: Problem with class and form
Quote:
Originally Posted by
Dawiss
Thank you for your answer, I created the object in Form1() constructor:
MyClass^ myc = gcnew MyClass();
1) What is the exclusive-or operator "^" doing on the left side of the = sign?
2) What is this "gcnew"? Is it a macro.
My point begin that this code is off-topic to this forum. As far as this forum goes, the code above is gibberish. Any .NET questions should go to the Managed C++ forum.
Regards,
Paul McKenzie
1 Attachment(s)
Re: Problem with class and form
Then where should I post this question?
Re: Problem with class and form
You need to mension same namespace for MyClass as follows.
MyClass.h file
Code:
#pragma once
namespace MyApp
{
public ref class MyClass
{
private:
int val;
public:
MyClass(void);
void addVal();
void delVal();
};
}
MyClass.cpp file
Code:
#include "StdAfx.h"
#include "MyClass.h"
namespace MyApp {
MyClass::MyClass(void)
{
}
void MyClass::addVal()
{
val++;
}
void MyClass::delVal()
{
val--;
}
}
Re: Problem with class and form
Quote:
Originally Posted by
Dawiss
Then where should I post this question?
http://forums.codeguru.com/forumdisplay.php?f=17