Reputation: 35
SO it may seem simple to a lot of people and I usually learn most of these things on my own but I need some help. I need to add the value of 2 or more numbers from different textboxes such as if someone types in "5" in one box and "5" in the other then hits a button that will do the math. So all in all what I need is math to be done in a string when it needs to. I might just need to know the code to do it. From what I researched and tried to find out was I think something to do with Convert.ToInt32())
or something a long those lines. It has to be in a string like this:
if (textBox1.Text == string.Empty && textBox2.Text == string.Empty && textBox3.Text == string.Empty && textBox4.Text == string.Empty)
{
MessageBox.Show("All fields are empty, try again!");
}
else if (textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox3.Text != string.Empty && textBox4.Text != string.Empty)
{
string a = textBox1.Text;
string b = textBox2.Text;
string c = textBox3.Text;
string d = textBox4.Text;
if (txtmessagechanged != null)
txtmessagechanged("Your total is ", null);
}
So that is some example text from my program and my issue is that all I really need is to know what I type to do pretty much basic addition and subtraction and some percentages that the user can select from another menu that are static such as 25% increase or 5% increase. Let me know if clarification is needed, thank you.
Upvotes: 1
Views: 1365
Reputation: 286
Basicly you need to have at least one valid decimal input textbox others can be empty, but can not be invalid. if it is validated-do the math. Then i assume, you will have a buttons add or substract. And a combobox with default selected value 0. If pressed is add, then set bool add to true, if substract then set it to false.
public decimal Caulculate(decimal number1=0, decimal number2=0, decimal number3=0, decimal number4=0, bool add=true, decimal percentage=0)
{
decimal result=0;
if(add)
{
result = number1 + number2 + number3 + number4;
}
if(!add)
{
result = number1 - number2 - number3 - number4;
}
if(percentage != 0)
{
result = Percentage(result,percentage);
}
return result;
}
public decimal Percentage(decimal number1,decimal percentage)
{
return number1+(number1 * (percentage / 100));
}
Validation check if it is empty, return 0, if invalid return null
public decimal? Validate (string input)
{
decimal number;
if (string.IsNullOrEmpty(input))
return 0;
if (decimal.TryParse(input, out number))
return number;
else return null;
}
handle add or substract buttons
private void Start(bool add)
{
DoMath math = new DoMath();
decimal? number1 = math.Validate(textBox1.Text);
decimal? number2 = math.Validate(textBox2.Text);
decimal? number3 = math.Validate(textBox3.Text);
decimal? number4 = math.Validate(textBox4.Text);
decimal? number5 = math.Validate(comboBox1.SelectedItem.ToString());
if (number1 != null &&
number2 != null &&
number3 != null &&
number4 != null)
{
label1.Text = math.Caulculate(number1.Value, number2.Value, number3.Value, number4.Value, add, number5.Value).ToString();
}
else
{
label1.Text = "There is validation error in texbox(es)";
}
}
private void button1_Click(object sender, EventArgs e)
{
Start(true);
}
private void button2_Click(object sender, EventArgs e)
{
Start(false);
}
Upvotes: 0
Reputation: 8079
Mathematical calculation always needs a numerical type (like int, flaot, ---). So you need to cast the string to one of those types. To which type you cast is up to the expected input. If the user only enters whole numbers, integer is perfect. Of the user enters floating point numbers you should use float (or similar). The parsing itself can be done in a lot of different ways. But in this scenario, it could very well be, that the user does not enter a number that can be parsed, therefore you should try if you can parse the string (which will also produce the right result):
if (textBox1.Text == string.Empty && textBox2.Text == string.Empty && textBox3.Text == string.Empty && textBox4.Text == string.Empty)
{
MessageBox.Show("All fields are empty, try again!");
}
else if (textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox3.Text != string.Empty && textBox4.Text != string.Empty)
{
string a = textBox1.Text;
string b = textBox2.Text;
string c = textBox3.Text;
string d = textBox4.Text;
if (txtmessagechanged != null)
{
int ai = 0;
int bi = 0;
int ci = 0;
int di = 0;
if(int.TryParse(a, out ai) && int.TryParse(b, out bi) && int.TryParse(c, out ci) && int.TryParse(d, out di))
{
int result = ai + bi + ci + di;
txtmessagechanged("Your total is " + result.ToString(), null);
}
}
}
In C# version 7 you don't have to declare the int variables befor using them in the TryParse
:
if (txtmessagechanged != null)
{
if(int.TryParse(a, out int ai) && int.TryParse(b, out int bi) && int.TryParse(c, out int ci) && int.TryParse(d, out int di))
{
int result = ai + bi + ci + di;
txtmessagechanged("Your total is " + result.ToString(), null);
}
}
Upvotes: 1
Reputation: 1383
Assuming the values entered are numeric, and don't contain invalid characters, you can convert them to Decimal using
decimal val_a = Convert.ToDecimal(a);
decimal val_b = Convert.ToDecimal(b);
To add or subtract simply:
decimal result = val_a + val_b
decimal result = val_a - val_b
Multiply or divide:
decimal result = val_a * val_b
decimal result = val_a / val_b
For percentage growth i guess you could do:
decimal result = (val_b - val_a) / val_a * 100
(Then obviously round it, convert .ToString() and concatenate a '%' on to the end.)
Upvotes: 0
Reputation: 3693
If you know what kind of value beforehand you can use appropriate one int.TryParse
long.TryParse
double.TryParse
etc. If you are unsure about the type of the number go with the safe one (double.TryParse
). If you are interested in monetary stuff go with decimal
SO what you maybe looking for is the following:
int parsedInt;
if(int.TryParse(textBox.Text, out parsedInt)==true){
//Now parsedInt is a number you can do math
}
else{
//Entered text can not be parsed :(
//Warn user??
}
Upvotes: 2