one super easy question !!! urgent !!!  
Author Message
gon_no1





PostPosted: Visual C++ General, one super easy question !!! urgent !!! Top

hi guys !

in VS2005 (C++), I have a form named Form1 (all default names here) and a label named label1 on it.

and in my source file, named say code1.cpp, i am trying to modify the Text of label1 (which is on Form1) in a function called myfoo(void).

My code1.cpp is:
----------------------------------------------------
#include "stdafx.h"
#include "Form1.h"
using namespace myFirstProject;

void myfoo(void)
{
label1->Text = "hello !!";
}
--------------------------------------------------
and obviously it is not right when i try to run it.

Can anyone please tell me (step by step) how I can do this simple thing

i tried to change label1 to "public", and still failed.

Clear step by step answer is much appreciated as I am quite new to this business.

Thank you very much ! Cheers!




Visual C++2  
 
 
nobugz





PostPosted: Visual C++ General, one super easy question !!! urgent !!! Top

The label1 control is a member of Form1. You need to reference it with a instance pointer to Form1. First off, go the form designer and change the Modifiers property to Public (you didn't edit Form1.h did you ) That makes the control accessible from outside the class.

Next, we need to find an instance pointer to Form1. One way to do that is to use the Application.OpenForms property. With a little luck, Form1 is the first form you'll display and it is actually being shown while you execute the code. If you're not lucky, it will not work. Here's the code:

void myfoo() {
Form1^ main = dynamic_cast<Form1^>(Application::OpenForms[0]);
if (main) main->label1->Text = "Hello world";
}



 
 
gon_no1





PostPosted: Visual C++ General, one super easy question !!! urgent !!! Top

hi thax alot for that.

I will try it out very soon.

however, if later I have decided Form1 is not gonna be the first form to display, is there another good way of doing it

thank you very much !