CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2012
    Posts
    4

    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...

  2. #2
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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.
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  3. #3
    Join Date
    May 2012
    Posts
    4

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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Problem with class and form

    .Net is off topic in this forum. Look for the managed C++ forum.

  5. #5
    Join Date
    May 2012
    Posts
    1

    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.

  6. #6
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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();
    */
    }
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  7. #7
    Join Date
    May 2012
    Posts
    4

    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..

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Problem with class and form

    Quote Originally Posted by Dawiss View Post
    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.

  9. #9
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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.
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with class and form

    Quote Originally Posted by Dawiss View Post
    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

  11. #11
    Join Date
    May 2012
    Posts
    4

    Re: Problem with class and form

    Then where should I post this question?
    Attached Files Attached Files

  12. #12
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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--;
    	}
    }
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Problem with class and form

    Quote Originally Posted by Dawiss View Post
    Then where should I post this question?
    http://forums.codeguru.com/forumdisplay.php?f=17

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured