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. C#
  4. textBoxes Calculation issue

textBoxes Calculation issue

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 3 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.
  • H Offline
    H Offline
    haroon1980
    wrote on last edited by
    #1

    Hello, When i use below code - App launches and when entering values it loops two times through my if(tb != null) function - Any idea please? And then somehow my total text box same value twice.

    public Form()
    {
    InitializeComponent();
    TextBox[] tbs = new TextBox[] { monTxtBox, tuesTxtBox, wedTxtBox, thurTxtBox, frdtxtBox };
    foreach (var c in tbs)
    c.TextChanged += new EventHandler(textBoxes_TextChanged); //create a common event for all textBoxes
    }

        private void textBoxes\_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if(tb != null)
            {
                decimal temp =0;
                if(decimal.TryParse(tb.Text,out temp))
                {
                    if(temp < 1)
               //     totalTimetxtBox.Text = Convert.ToDecimal(tb.Text.Replace(",",".").ToString())+temp;
                     totalTimetxtBox.Text = tb.Text+temp;
            else
                MessageBox.Show("Please enter the number between 0 and 1.");
                }
        else
            MessageBox.Show("Please enter a decimal value only!");
            }
        }
    
    L 1 Reply Last reply
    0
    • H haroon1980

      Hello, When i use below code - App launches and when entering values it loops two times through my if(tb != null) function - Any idea please? And then somehow my total text box same value twice.

      public Form()
      {
      InitializeComponent();
      TextBox[] tbs = new TextBox[] { monTxtBox, tuesTxtBox, wedTxtBox, thurTxtBox, frdtxtBox };
      foreach (var c in tbs)
      c.TextChanged += new EventHandler(textBoxes_TextChanged); //create a common event for all textBoxes
      }

          private void textBoxes\_TextChanged(object sender, EventArgs e)
          {
              TextBox tb = sender as TextBox;
              if(tb != null)
              {
                  decimal temp =0;
                  if(decimal.TryParse(tb.Text,out temp))
                  {
                      if(temp < 1)
                 //     totalTimetxtBox.Text = Convert.ToDecimal(tb.Text.Replace(",",".").ToString())+temp;
                       totalTimetxtBox.Text = tb.Text+temp;
              else
                  MessageBox.Show("Please enter the number between 0 and 1.");
                  }
          else
              MessageBox.Show("Please enter a decimal value only!");
              }
          }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      shah123 wrote:

      it loops two times through

      You mean like your requirement is that the value be between 0 and 1 so your event fires once for the "." and then fires again for the numerical value entered?

      Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

      H 1 Reply Last reply
      0
      • L Lost User

        shah123 wrote:

        it loops two times through

        You mean like your requirement is that the value be between 0 and 1 so your event fires once for the "." and then fires again for the numerical value entered?

        Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

        H Offline
        H Offline
        haroon1980
        wrote on last edited by
        #3

        yes somehow it fires 2 times - dont know why? i just want to put 0.1 in one textbox,0.2 in 2nd textbox etc and then getting same time my totaltimetxtbox calculates as well

        L 1 Reply Last reply
        0
        • H haroon1980

          yes somehow it fires 2 times - dont know why? i just want to put 0.1 in one textbox,0.2 in 2nd textbox etc and then getting same time my totaltimetxtbox calculates as well

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          It does that because that is how you set it up to be. That event is going to fire every time you change the text in anyone of those textboxes. When you enter a "." it's going to fire, when you enter "1" it's going to fire, etc.

          Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

          H 1 Reply Last reply
          0
          • L Lost User

            It does that because that is how you set it up to be. That event is going to fire every time you change the text in anyone of those textboxes. When you enter a "." it's going to fire, when you enter "1" it's going to fire, etc.

            Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012

            H Offline
            H Offline
            haroon1980
            wrote on last edited by
            #5

            so any solution to help pleas if possible?

            D 1 Reply Last reply
            0
            • H haroon1980

              so any solution to help pleas if possible?

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              That depends on your requirements. You could handle one of the other events of the TextBox, such as the Leave event, or LostFocus, or even the Validating event.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              H 1 Reply Last reply
              0
              • D Dave Kreskowiak

                That depends on your requirements. You could handle one of the other events of the TextBox, such as the Leave event, or LostFocus, or even the Validating event.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                H Offline
                H Offline
                haroon1980
                wrote on last edited by
                #7

                hello, thanks - Iam trying to get the values of all textboxes and then adding it in totaltextbox but currently it takes only current textbox and when it leaves to 2nd it forgets the first one. Any help please?

                private void textBoxes_TextChanged(object sender, EventArgs e)
                {
                TextBox tb = sender as TextBox;
                if (tb != null)
                {
                decimal temp = 0;
                if (decimal.TryParse(tb.Text, out temp))
                {
                if (temp > 1)
                MessageBox.Show("Please enter the nnumber between 0 and 1.");
                }
                }
                }

                    private void textBoxes\_Leave(object sender, System.EventArgs e)
                    {
                        decimal total = 0;
                        TextBox tb = sender as TextBox;
                        if (tb != null)
                        {
                            decimal temp = 0;
                            if (decimal.TryParse(tb.Text, out temp))
                            {
                                total += temp;
                                totalTimetxtBox.Text = total.ToString();
                                
                            }
                        }
                    }
                
                D 1 Reply Last reply
                0
                • H haroon1980

                  hello, thanks - Iam trying to get the values of all textboxes and then adding it in totaltextbox but currently it takes only current textbox and when it leaves to 2nd it forgets the first one. Any help please?

                  private void textBoxes_TextChanged(object sender, EventArgs e)
                  {
                  TextBox tb = sender as TextBox;
                  if (tb != null)
                  {
                  decimal temp = 0;
                  if (decimal.TryParse(tb.Text, out temp))
                  {
                  if (temp > 1)
                  MessageBox.Show("Please enter the nnumber between 0 and 1.");
                  }
                  }
                  }

                      private void textBoxes\_Leave(object sender, System.EventArgs e)
                      {
                          decimal total = 0;
                          TextBox tb = sender as TextBox;
                          if (tb != null)
                          {
                              decimal temp = 0;
                              if (decimal.TryParse(tb.Text, out temp))
                              {
                                  total += temp;
                                  totalTimetxtBox.Text = total.ToString();
                                  
                              }
                          }
                      }
                  
                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  None of this code should be in the event handlers. This stuff should be in its own method, called by the events handlers so you're not duplicating code all over the place. Your addition code should be getting the values of all the textboxes it needs to and adding them together, not just the one where the focus left the textbox or had the textbox value changed. It's "forgetting" the previous value because your total value is being reset to 0 every time the event handlers are called. Variables declared in a method only exist in that method and are destroyed when execution leaves the method.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  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