Updating a label's text automatically? (New programmer)
-
Thanks for looking at my question! I am a fairly new programmer, so please bear with me. I am trying to make a program that takes the user-inputted values from 3 text boxes, adds them together, then displays the total as a label that I placed next to the boxes in real time-(as in the number updates automatically every time the number in one of the text boxes is changed). Also, how do I make sure the value that is inputted is only numbers? Ok, here is what I have got so far. Obviously it doesn't work because I am asking this question. north1, north2, and north3 are the three text boxes and northT is the label next to them. The rest is pretty easy to understand from the code.
private: System::Void northT_Click(System::Object^ sender, System::EventArgs^ e)
{
//Variables
double nTotal = 0.0;
double n1 = 0.0;
double n2 = 0.0;
double n3 = 0.0;do { n1 = Convert::ToDouble(north1->Text); n2 = Convert::ToDouble(north2->Text); n3 = Convert::ToDouble(north3->Text); nTotal = n1 + n2 + n3; northT->Text = nTotal.ToString(); } while(nTotal > 0); }
Again, thanks for looking, and any help will be greatly appreciated.
-
Thanks for looking at my question! I am a fairly new programmer, so please bear with me. I am trying to make a program that takes the user-inputted values from 3 text boxes, adds them together, then displays the total as a label that I placed next to the boxes in real time-(as in the number updates automatically every time the number in one of the text boxes is changed). Also, how do I make sure the value that is inputted is only numbers? Ok, here is what I have got so far. Obviously it doesn't work because I am asking this question. north1, north2, and north3 are the three text boxes and northT is the label next to them. The rest is pretty easy to understand from the code.
private: System::Void northT_Click(System::Object^ sender, System::EventArgs^ e)
{
//Variables
double nTotal = 0.0;
double n1 = 0.0;
double n2 = 0.0;
double n3 = 0.0;do { n1 = Convert::ToDouble(north1->Text); n2 = Convert::ToDouble(north2->Text); n3 = Convert::ToDouble(north3->Text); nTotal = n1 + n2 + n3; northT->Text = nTotal.ToString(); } while(nTotal > 0); }
Again, thanks for looking, and any help will be greatly appreciated.
Hi, you're pretty close. Your question is clear, except for the "it doesn't work" which is too vague. You probably mean: I never got the sum to show at all, my program simply freezes. AFAIK your main problem is this: you have a do-while loop inside the handler, so for positive sums it will never exit, hence the new sum also does not become visible. Experiment: enter 0, 0, and -1 Throw away the do-while all together; the handler should calculate the sum only once each time your event fires (not sure what it is connected to). Further improvements: - either add a try-catch construct to deal with bad input, or start using
double.TryParse
- to restrict user input, you could wire TextChanged handlers to the TextBoxes, but it is rather hard to deal with all the details (minus signs, plus signs, exponents, periods, comma's, and editing, copy/pasting, etc). The TryParse method basically takes care of it all. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, you're pretty close. Your question is clear, except for the "it doesn't work" which is too vague. You probably mean: I never got the sum to show at all, my program simply freezes. AFAIK your main problem is this: you have a do-while loop inside the handler, so for positive sums it will never exit, hence the new sum also does not become visible. Experiment: enter 0, 0, and -1 Throw away the do-while all together; the handler should calculate the sum only once each time your event fires (not sure what it is connected to). Further improvements: - either add a try-catch construct to deal with bad input, or start using
double.TryParse
- to restrict user input, you could wire TextChanged handlers to the TextBoxes, but it is rather hard to deal with all the details (minus signs, plus signs, exponents, periods, comma's, and editing, copy/pasting, etc). The TryParse method basically takes care of it all. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Thanks for the help! I will experiment with the Double.TryParse() after I can get the intended effect from the rest of my program. I figured out how to stop my program from freezing and keep the do_while, (I also did it without the do_while, but my assignment is to make the program using do_while), but I still do not have the intended effect. northT (the label that displays the total) will update when I click on it, but I need it to update every time one of the values in the text boxes change. Here is the updated version of my code.
private: System::Void northT_Click(System::Object^ sender, System::EventArgs^ e)
{
//Variables
double nTotalIn = 0.0;
double nTotalFi = 0.0;
double n1 = 0.0;
double n2 = 0.0;
double n3 = 0.0;do { nTotalIn = Convert::ToDouble(northT->Text); n1 = Convert::ToDouble(north1->Text); n2 = Convert::ToDouble(north2->Text); n3 = Convert::ToDouble(north3->Text); nTotalFi = n1 + n2 + n3; northT->Text = nTotalFi.ToString(); } while(nTotalFi != nTotalIn); }