^^what you are doing is fine for a beginner. you need to learn the basic things your self.. once you are a little comfortable with the language, you can go ahead learning advanced techniques.
don't get worried if you are getting errors. try to check the flow once or twice before posting here.. we can help you without any problem,but that's not the point.. you need to learn about the issues which come up when you are coding.
So here are few code suggestions-
1.Always have some good naming conventions.This helps
to improve code readability
Eg-chkFruits,nMangoes,etc
2.You can use String.Empty instead of "" for readability
3.I think as you want to validate four times using same
logic.A functions will make code tidy.
Alternate way as per OOPS ideology
is creating a validation class for you objects
which is pretty high level for now.
Like
Code:
public void ValidateControlandAssign(CheckBox targetChkbox,TextBox tValue,TextBox tAssign,int rate )
{
private const string senterqty="Please enter a quantity";
private const string scheck="Please check the respective items after entering the quantity");
private int tempval;
if (targetChkbox.Checked)
{
if (tValue.Text != String.Empty)
tempval = Convert.ToInt32(tvalue);
else
MessageBox.Show(senterqty);
}
else
{
if (Convert.ToInt32(tValue.Text) > 0)
MessageBox.Show(scheck);
}
tAssign.Text= = Convert.ToString(rate * tempval);
}
private void button1_Click(object sender, EventArgs e)
{
..
//as per your code call
//observe how names of control of your code doesnt make sense except mango
ValidateControlandAssign(checkBox1,textBox1,textBox5.Text, rate_mango);
..
}
4.Remove {} for single statements of if.
Code:
if (true) singlestatment;
else singlestatment;
5.Dont hardcode strings.Put it in either config file
or constants.
Code:
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (textBox1.Text != "")
{
qua1 = Convert.ToInt32(textBox1.Text);
}
else
{
MessageBox.Show("Please enter a quantity");
}
}
else
{
if (Convert.ToInt32(textBox1.Text) > 0)
{
MessageBox.Show("Please check the respective items after entering the quantity");
}
}
textBox5.Text = Convert.ToString(rate_mango * qua1);
if (checkBox2.Checked)
{
if (textBox2.Text != "")
{
qua1 = Convert.ToInt32(textBox2.Text);
}
else
{
MessageBox.Show("Please enter a quantity");
}
}
textBox6.Text = Convert.ToString(rate_pineapple * qua2);
if (checkBox3.Checked)
{
if (textBox3.Text != "")
{
qua1 = Convert.ToInt32(textBox3.Text);
}
else
{
MessageBox.Show("Please enter a quantity");
}
}
textBox7.Text = Convert.ToString(rate_apple * qua3);
if (checkBox4.Checked)
{
if (textBox4.Text != "")
{
qua1 = Convert.ToInt32(textBox4.Text);
}
else
{
MessageBox.Show("Please enter a quantity");
}
}
textBox8.Text = Convert.ToString(rate_banana * qua4);
textBox9.Text = Convert.ToString(Convert.ToInt32(textBox5.Text) + Convert.ToInt32(textBox6.Text) + Convert.ToInt32(textBox7.Text) + Convert.ToInt32(textBox8.Text));
}
There is absolutely no need for checking String to be empty, the reason being there are only 2 equivalence classes - Correct Input and Incorrect Input and an empty string is the same as an incorrect numeric string.
@ds_
As I have earlier demonstrated the use of int.TryParse(), I suggest you instead use int.Parse() and handle the exception.
This way the calculation will cease once it detects one of the inputs as Invalid, I'd show you a much, much better way of handling things like these (using foreach and Controls property) but it'd be an overkill for now.
P.S. There is no need for Trim() here since TryParse()/Parse() trims it anyway.
1) The warning messages are coming as many times as error is occurred. Like if the user enters the value for all 4 items but forgets to check the respective boxes, it gives error 4 times. How do I make it once?
2) I do a sample calculation for the first time, then press "Reset" button. Then I press "OK" button without entering any values. This should give grand total = 0 but its giving last grand total. I'm not able to catch what's wrong. It was not happening earlier, but in the modification process something went wrong.
This screenie of my app may help you overcoming my bad naming convention.
*imgur.com/sowmu.png
One more question:
I applied E.Handled restriction only to textBox1 but its working on all quantity textboxes (1-4). I can see it activated in properties, KeyPress event of all quantity textBoxes? How is it possible?
It's optimization at the cost of error checking. It won't throw errors on invalid inputs like "12s2". It uses the age-old technique of converting ASCII numbers to decimal.
If it's bothering you, you can turn it off either by setting the compiler options via properties to /nowarn 642 or using the good old #pragma directive #pragma warning disable 642. Alternatively you can exclude that optional else clause
1) The warning messages are coming as many times as error is occurred. Like if the user enters the value for all 4 items but forgets to check the respective boxes, it gives error 4 times. How do I make it once?
Try the try-catch approach. If you don't want to do that, then use a boolean flag to set it as true if some error occured. Then display the error only once.
2) I do a sample calculation for the first time, then press "Reset" button. Then I press "OK" button without entering any values. This should give grand total = 0 but its giving last grand total. I'm not able to catch what's wrong. It was not happening earlier, but in the modification process something went wrong.
I'm guessing some errand variables you failed to reset.
I applied E.Handled restriction only to textBox1 but its working on all quantity textboxes (1-4). I can see it activated in properties, KeyPress event of all quantity textBoxes? How is it possible?
If it's bothering you, you can turn it off either by setting the compiler options via properties to /nowarn 642 or using the good old #pragma directive #pragma warning disable 642. Alternatively you can exclude that optional else clause
Do I really need that fuss? I just included the braces.
Try the try-catch approach. If you don't want to do that, then use a boolean flag to set it as true if some error occured. Then display the error only once.
There are total 8 variables. 4 for inputting quantity & 4 for rate. I already gave "rest" code in above post in which I reset all the textboxes and quantity variables. Rate is of course not required.
Possible if you copied-pasted those text-boxes from the first one.
There are total 8 variables. 4 for inputting quantity & 4 for rate. I already gave "rest" code in above post in which I reset all the textboxes and quantity variables. Rate is of course not required.
Actually I wanted to work it on all boxes and its working without my effort lol
I experienced same case in a program written by one of my friend where he made a sample calci to input two nos and print +,-,*,/ on clicking buttons. But when I saw the code, there was only "sum", nothing else.
Hi Guest we just wanted to alert you to a major change in the forum. We will no longer be allowing the posting of outgoing links. Please use the attachment feature to attach media to your posts.