Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
Hello,
I am transitioning from console applications and have a small question. I know that values such as the text of a label can be changed by "this->label1->Text = L"Some New Text";" as a response to a return value, button click etc.

I am trying to learn how I can change the values of controls from within a function before the function returns. As an example suppose I have:
C#
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             System::Void myfunc();
         }


This in turn leads to the following code just after main:
MIDL
void myfunc()
{
    //We are doing something
    //lets change Label1.Text = L"New Text";
    //now we will do some more stuff

}


I have tried specifying the label as "System::Windows::Forms::Label...." but have been unsuccessful in getting it to work. Is there a caveat here or am I just not identifying the member of the class correctly.

Cheers,Sawyer1
Posted
Comments
Sergey Alexandrovich Kryukov 30-May-11 15:55pm    
Not clear. From a normal point of view, all that issues are not issues, the notion of something "before function returns" looks irrelevant. Do you clearly understand classes and instances. Instance method or property vs static method or property? The role and mechanism of "this"? What exactly "unsuccessful in getting it to work"? Show some code.
--SA

1 solution

Assuming that myfunc() is a member of the same class as the button1_Click() event handler, you can just use the same code in your function as you would in your event handler:

void myfunc()
{
    this->label1->Text = L"Some New Text";
}


I'm hoping that I've understood your question correctly :)

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

Ah, it seems I did misunderstand the question, sorry :)

You want to access a control in a specific class from a function that's not in the same class. In that case, the outside function (myfunc) needs to receive a reference to the label, so that it can access it (otherwise it would be very hard for the myfunc function to find an instance of label1)

Unfortunately my C++ is extremely rusty, so I'm not sure what the syntax is for passing references, but in C# I'd do the following:

void myfunc(Label lbl)
{
  lbl.Text = "Some New Text"; // In C++: lbl->Text = L"Some New Text";
}


And then calling it like this from inside the button1_Click event handler (the "caller"):

myfunc(this.label1);


So in order to get this to work, you'll need to figure out the correct syntax for the label1 argument in both the myfunc function and the caller.

I hope this is of more use...
 
Share this answer
 
v3
Comments
sawyer1 31-May-11 15:46pm    
R.Hoffman,
Thanks for the interest. I use that method of changing the properties most often and referenced doing so in my post. In trying to state the problem in the briefest manner it might not have been clear from the context.
This is a learning/educational problem for me. I am exploring classes, and how controls are manipulated, and how Visual Studio organizes therein.
Specifically I am trying to do the same thing as the code you wrote would do, but within a function that is not a member of the same class. FYI: In my test program I have made the member of the class public and the function has been placed after main.
Basically my questions are is it possible to do this, and if so how do I reference the member of the class/value correctly in code.
In a practical sense I could just put the guts of the function directly in the event handler but that would evade the educational value.
Cheers,
Shane
R. Hoffmann 31-May-11 16:01pm    
Thanks for your explanation, I've updated my answer with some hints that may be useful. My C++ is too rusty to do much more :)
sawyer1 1-Jun-11 11:00am    
I had not thought of passing it as an argument.. That raises some interesting possibilities. I will twiddle with it and see what I can do.
I have been attempting to learn how to access it with something like System::Windows::Forms::Label::label1->Text = L"some text"; from inside the function but the syntax off somehow. I don't use classes in my embedded applications so it's very new.
Much appreciation,
Shane

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900