none
how to get access to Form components from .cpp?

    Question

  • Creating new project with  standart Windows Form Application Wizard I  got two files: .h and .cpp .
    If I have my function realizations in .cpp file how can I access my ListBox component on Form to print out results?
    I tried to create pointer to Form1 instance, but got error:
    'myForm' : cannot declare a global or static managed type object or a __gc pointer
    , but I want to have access to all components of my Form from my procedures and functions from .cpp file!
    already downloaded couple books, but no explanation.
    Please answer if you have an idea how!
    Thank you
    Wednesday, February 14, 2007 4:32 PM

Answers

  • Hi ,

     just keep all ur function realizations in side the namespace under which the Form created.Then u will be able to access all the components on Form.

    in .cpp file ,

    namespace projectname

    {

        //Keep ur function realizations here.

    }

    Hope it will work out for ur problem.

     

    Thanx & Regards,

    Ch.T.Gopi Kumar.

     

    Friday, February 23, 2007 8:32 AM

All replies

  • Hi ,

     just keep all ur function realizations in side the namespace under which the Form created.Then u will be able to access all the components on Form.

    in .cpp file ,

    namespace projectname

    {

        //Keep ur function realizations here.

    }

    Hope it will work out for ur problem.

     

    Thanx & Regards,

    Ch.T.Gopi Kumar.

     

    Friday, February 23, 2007 8:32 AM
  • Hello People

    I am struggeling also on this issue, and still not able to make it working from projectname.cpp file
    'richTextBox1' : undeclared identifier

    Thanks for the help


    projectname.cpp

    namespace projectname
    {
     Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        richTextBox1->AppendText("hello");
    }
    }

    Form1.h
    #pragma endregion
      private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
        }
      private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        }
    Monday, March 05, 2007 2:35 PM
  • Hi,

    1)Remove the code written by you in projectname.cpp file(ofcourse u can do it here also but it doesn't look fine,instead we'll create new file Form1.cpp and write the definitions in it as follows).

    2)Don't define the button1_click in the form1.h - remove ' {}' and put ';' like as follows- Just declare it.

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) ;

    2)Add a Form1.cpp(new file) and write code in it like

    #include "stdafx.h"

    #include "Form1.h"

    System::Void Test::Form1::button1_Click(System::Object^ sender, System::EventArgs^ e)

    {

    richTextBox1->Text = "RichTextBox1 accessed";

    }

    -------------------------------------------------------------------------

     

    Hope u got me.

    Thanx,

    Ch.T.Gopi Kumar.

    Monday, March 05, 2007 3:29 PM
  • Hiya Ch.T.Gopi Kumar,

    That did the "trick" VERY GOOD explination, (I searched yersterday all over the place but nothing found) I am familiar using borland c++ but visual c++ i am new. Now i understand how this syntax is working access to Form components.

    Thanks!!
    Tuesday, March 06, 2007 10:10 AM
  • Thank you all guys,
    the point was to get access from my own procedures in .cpp part, but not from event handlers as button1_onClick. Now I'm writing everything in .h file, and still have no idea about cpp.
    Everything I have in .cpp file :

    #include "stdafx.h"
    #include "Form1.h"
    #include <windows.h>

    using namespace myNameSpace;

    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
        Form1* myForm;
        System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
        Application::Run(myForm = new Form1());
        return 0;
    }

    Stas Zh. (aka distas)
    Tuesday, March 06, 2007 3:46 PM
  • Hi Stas,

     Create a WindowsForms Application in VS2005(VC++).

     Add a button and text box to it.

     Double click button to generate it's click event handler.

    Now make button handler definition to decleratio as shown below and add a prvate function in the form1.h

    -----------------------------------------------------------

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);

    private: System::Void SetText(String^ strText);

    -----------------------------------------------------------------------

    Now add a new file (sav it as form1.cpp add it to the project after saving it) and define the above functions in it as following

    ------------------------------------------------

    #include "stdafx.h"

    #include "Form1.h"

    namespace dffg {

    System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) {

    SetText("Hello, it's working");

    }

    System::Void Form1::SetText(String^ strText)

    {

    this->textBox1->Text = strText;

    }

    }

    ----------------------------------------------------------

    Build and Run the project.Click the button to see the text box containing the value 'Hello',it's working'.

    Am i done? Still not clear?

    Hope this time u'll get it.

     

    Thanx,

    Ch.T.Gopi Kumar.

     

     

    Wednesday, March 07, 2007 1:05 PM