Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've asked 2 multiple-question posts and one of those questions has been "how do I display a variables value in a label?", but I never got an answer to that.
Now that is my question, how do I display a variables name in a label?
For example if I want to display something like this:
"Im showing the number 10 because I want to"
where the number 10 is a variables value.
In a DOS window I would write it like this:

C++
int main()
{
int Number = 10;
cout << "Im showing the number " << Number << " because I want to";
return 0;
}


But how can I do it in a label (inside a Windows Application)?

Thank You! :)
Posted
Updated 25-Oct-11 0:15am
v4
Comments
Nuri Ismail 7-Jan-11 12:48pm    
From your previous questions I suspect that you are using C++\CLI, but your question is tagged as C++, which refers to native C++. Please clarify this, because setting the label text is quite different in Native C++ Windows applications than C++\CLI WinForms appapplications.

C++
label.Text = sprintf("I'm showing the number %d because I want to.", Number);
 
Share this answer
 
v2
If I understood your problem properly than first you have to convert the integer value to string and using SetWindowText you display that string to label.

:)
 
Share this answer
 
Comments
Karsten Malmquist 7-Jan-11 14:28pm    
Sorry if I sound like a total noob, but how exactely do I convert a integer value to a string with the SetWindowText thing?
By the way, by using SetWindowText can I set the string to whatever the value was from the variable without having to use if statements to check the value?
ShilpiP 9-Jan-11 23:54pm    
Err ?? You can convert the int to string using
CString str;
int x;

x =6;
str.Format("%d", x);

and using SetWindowText you can display the string in Label. I mean to say that if you are using Static control as label.
Karsten Malmquist 10-Jan-11 14:33pm    
I still don't understand, if I were to display text to a label I would use the library "windows.h" and write it like this:

this->label1->Text = L"Text";

but do I need to write it in a completely different way to display a string (would prefer if you could write it in code if you dont mind).
And what do I need to include for the "str.Format", because it says it's a undeclared identifier.

Thanks for your help! :)
Karsten Malmquist 18-Jan-11 15:03pm    
I still don't understand, if I were to display text to a label I would use the library "windows.h" and write it like this: this->label1->Text = L"Text"; but do I need to write it in a completely different way to display a string (would prefer if you could write it in code if you dont mind). And what do I need to include for the "str.Format", because it says it's a undeclared identifier. Thanks for your help! :)

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