form label string to integer?
Im making a form with a label and I want the label to change into a number that counts upwards until I tell it to stop....I have a button I click to make it go already but it seems that I cant use numbers as the label...whats going on?
Here is my buttons code:
private: System::Void btnGo_Click(System::Object^ sender, System::EventArgs^ e)
{
int x = x + 1;
while ( x == x )
lblText->Text = x; // I know this is infinite loop thats what I want
}
Thanks Syx
Re: form label string to integer?
Try:
lblText->Text = x.ToString();
Re: form label string to integer?
got a spam of errors from that
the errors I originally got were
1>------ Build started: Project: counter, Configuration: Debug Win32 ------
1> counter.cpp
1>c:\users\ameriwannica\desktop\desktop\c++\counter\counter\Form1.h(117): error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'int' to 'System::String ^'
1> No user-defined-conversion operator available, or
1> No standard conversion exists from the boxed form of the arithmetic type to the target type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Re: form label string to integer?
Strange, with the statement lblText->Text = x.ToString(); I don't get any compile errors!, So I have no idea why you do!
And you said you want to count upwards, must the while loop then not be something like this:
int x = 0;
while ( x == x )
{
lblText->Text = x.ToString();
// I know this is infinite loop thats what I want
x++;
}
And how do you want to stop this infinite loop? You calling this with a new Thread?
Re: form label string to integer?
Ill be stopping it with a timer after it counts to one minute...
I thought about changing the loop to the way you wrote it as well (Which I have now done)
These are the new errors with the new code:
1>------ Build started: Project: counter, Configuration: Debug Win32 ------
1> counter.cpp
1>c:\users\ameriwannica\desktop\desktop\c++\counter\counter\Form1.h(115): error C2059: syntax error : 'while'
1>c:\users\ameriwannica\desktop\desktop\c++\counter\counter\Form1.h(116): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Re: form label string to integer?
But the code as you wrote it in the forum, will not show the counter in the label, you program will just jam. To edit label text in the way you want it, you need to use a new thread and a delegate to change the label value.
And what about your compiler errors, I have no idea, did your forgot a ; somewhere ( its the most common mistake to forget a ; at end of a statement or to have too many or too short opening { or closing } braces)
Re: form label string to integer?
Re: form label string to integer?
Quote:
Originally Posted by
syxdowns
// I know this is infinite loop thats what I want
Quote:
Originally Posted by
syxdowns
Ill be stopping it with a timer after it counts to one minute...
And how would you imagine your timer tick handler would look like to be able to break this infinite loop? ;)
As already hinted by SabNary, your never-returning button click handler would block your GUI thread, not only causing your program to become irresponsive, but also preventing the label from being repainted, so you can't observe the counting process anyway. Moreover, if your timer is a System::Windows::Forms::Timer, due to the blocked GUI thread the timer tick handler wouldn't ever get called either.
You may force the label to update by placing this call in your loop:
Code:
lblText->Refresh();
but that still wouldn't save the day since it wouldn't prevent the GUI thread from getting blocked.
Quote:
Originally Posted by
SabNary
But the code as you wrote it in the forum, will not show the counter in the label, you program will just jam. To edit label text in the way you want it, you need to use a new thread and a delegate to change the label value.
This is a possibility, but I would rather do the counting and updating of the label in the tick handler of a System::Windows::Forms::Timer. This handler would run in the context of the GUI thread, so there would be no need to marshal the updating call across a thread boundary. Also, that way the counting and updating process would happen in a controlled manner, and not eat up all the CPU cycles it can grab as an uncontrolled infinite loop in a worker thread would do as well.
The timing for the counting "loop" termination could then be done by counting invocations of the counting timer or by means of a second timer.
Please use code tags when posting code.
Re: form label string to integer?
thanks for the help, I took a break and tried a different angle didnt get anywhere so Ill try this stuff :D