textBoxes Calculation issue
-
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!"); } }
-
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!"); } }
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
-
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
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
-
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
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
-
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
so any solution to help pleas if possible?
-
so any solution to help pleas if possible?
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 -
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 Kreskowiakhello, 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(); } } }
-
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(); } } }
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