Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Updating a label's text automatically? (New programmer)

Updating a label's text automatically? (New programmer)

Scheduled Pinned Locked Moved Managed C++/CLI
questionjsonhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TabascoSauce
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • T TabascoSauce

      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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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


      T 1 Reply Last reply
      0
      • L Luc Pattyn

        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


        T Offline
        T Offline
        TabascoSauce
        wrote on last edited by
        #3

        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);
        	 }
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups