Hi Guys.

I'm not a c++ dev but a VB.NET developer. I will first explain my scenario so that the question would be clear. Suppose I had a form which has a Label contro named label1 and the project name is Test which is done in a Visual Studio using the CLR template.

Files

I will just mention files which are related to the question only.

1) Form1.h
2) Test.cpp

Now I have functions (Tasks which the application should perform) on Test.cpp. My question is how do I update a label1 control within Test.cpp file inside the function.

Sample functions:

HRESULT Register(void)
{
//...
//Registration process here
//...
//Use the result to detemine the registration result and write the result in a label1 control
}

My question is how can I call the label1 within the cpp file. I've seen some posts says you will need to make another Form1 instance within the function or cpp file and yes doing so I am able to call the label but it seems as if I'm calling it to this new instance which doesn't really write/update the actual label on Form1.

Another question is with the HWND of a form. Can I be able to get the window handle before the window is created?

Thanks again as I've said I'm not a C++ dev.

@Mr.M. I'd also reveal what IDE and OS this is in. I can guess it's Visual Studio and Windows but don't leave that for all to guess. Either use the tags or just call it out.

I recall in Visual Studio on a C++ app I think there is no handle until the window is created. What article or app tried to do that?

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.load(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1 shows a C++ app changing the text of an object. No HWND there.

Hi rproffitt. As of IDE I've mentioned that above

Visual Studio using the CLR template

and Yes I'm using Windows 7 Home Basic (32bit). Also about the link. The post on the link update the label within the Form1.h file Please take note of the (.h) and the (.cpp) as what I'm asking is about (.cpp) just make sure that is cleared out.

I've seen some other theads on other forums which almost do what I want but the problem it is called by a button control which is oppose to what I want.

Basically the form first register to recieve system events and based on the registration it will then write back so that a user can see what happended at the back end.

@MrM Now I see it. Don't know why my eyes skipped to the next line.

Anyhow, in your code you can change the text of an object using the example I gave. It does not have to be in the button control.

I get the feeling you want someone to write the code for you. So I worry you are on a very old version of VS but in the line of code you call out the control or form name, property you want to change and change it. As in the example link shows.

Hi well my code is not inside the .h file but its inside the .cpp file and those are the functions that should access the control on the .h file from .cpp file. I've manage to get it to update on startup under the main function like this

Form1 ^form = gcnew Form1();
form->label1->Text = "Text has changed from the out side.";
Application::Run(form);

Now I have other functions which should also update this label control. How can I do that or is it safe to use the above code on each and every function that needs to update this label?

Let's see if anyone will write if it's safe. Here I didn't need that line 1 but called out Form1 as needed in similar code in past projects.

So you mean I can call

form->label1->Text = "Text has changed from the out side.";
Application::Run(form);

as much as need and where ever I want to? I will have to try it out and see then.

Thanks what I did was that I kept it on that main function a added more control which are for each and every function and its worked. I also think this isn't the good way of programming in C++ as its limits me. I think I should have wrote my Functions on the .h file not on the .cpp but if I do that errors occures.

I've found the solution to this problem and the problem was that I was writting the code in the wrong place, the correct place is inside the Form1.h and you will be able to do all the possible things to the control even on run time, you can update, change its colors, show, hide, etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.