how to create a pointer to a form's object, and use it in a .cpp source file ??  
Author Message
gon_no1





PostPosted: Visual C++ Language, how to create a pointer to a form's object, and use it in a .cpp source file ?? Top

hi there!

I want to create a pointer to a form's object, ie, a pointer called "myptr" that points to "label1" on "Form1".

Then, inside Form1's button1_click event handling routine, I want to call a function and pass this label1's pointer, "myptr", into that function.

say the name of the function is "myFunc". I must write this function in a new cpp source file in the same project (ie, i cannot write this function inside Form1.h).

so in my cpp source file, i will have something like:

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

void myFunc ( myptr)

{

myptr->Text = "ha ha ha !!!";

}

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

and in my Form1's button1_click event handling routine (inside Form1.h), i will have something like:

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

....................

create a pointer called "myptr" that points to label1;

myFunc( myptr );

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

Bcoz what I want to do is when a user clicks on button1 on Form1, I wanna call a function called myFunc and let that function change the text of label1.

Please help. I am sure this is a very easy question for you guys, Thank you very much !!!

Best Regards



Visual C++16  
 
 
Frank Boyne





PostPosted: Visual C++ Language, how to create a pointer to a form's object, and use it in a .cpp source file ?? Top

I want to create a pointer to a form's object, ie, a pointer called "myptr" that points to "label1" on "Form1".
Not to be overly pedantic but, what you really want to create is a 'handle' not a pointer. The difference being that a pointer points to unmanaged (or 'native') memory and a handle (or at least this interpretation of 'handle') points to a managed object.

In earlier versions of Visual Studio, __gc * was used to declare pointers to objects on the garbage collected heap but in Visual Studio 2005 the ^ (handle to managed object) syntax is used instead

So, what you want to declare is a variable of type "handle to System.Windows.Forms.Label"...


System::Windows::Forms::Label^ foo;

You'd probably declare that variable somewhere in form1.h and then, somewhere appropriate (perhaps in Form1_Load ) you would need to initialise the variable like so...


foo = this->label1;

Now, let's say you create a completely new set of files 'stuff.h' and 'stuff.cpp'. In the header file 'stuff.h' you would declare your function prototype


void MyFunc (System::Windows::Forms::Label^ myLabelHandle);

and in the actual .cpp file you would put the function implementation


void MyFunc (System::Windows::Forms::Label^ myLabelHandle)
{
myLabelHandle->Text = "Ha Ha Ha!!!";
}

Finally, in Form1.h you would #include "stuff.h" to get the myFunc prototype declared and then invoke the function from within your button handler something like this...


private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MyFunc (foo);
}

Bcoz what I want to do is when a user clicks on button1 on Form1, I wanna call a function called myFunc and let that function change the text of label1.

You may not need to declare a separate variable. If you know you are in a click handler for a button on Form1 and if you know there is a label called label1 on that same form, you could access the label directly within the click handler...


private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MyFunc (this->label1);
}


 
 
gon_no1





PostPosted: Visual C++ Language, how to create a pointer to a form's object, and use it in a .cpp source file ?? Top

Thank you very much !! u the man Frank !!