Reputation: 49
I have 3 text boxes, and I want the user to put any number into it to get an answer.
Here is the code I have so far:
int firstNumber;
int secondNumber;
int thirdNumber;
int answer;
firstNumber = 100;
secondNumber = 75;
thirdNumber = 50;
answer = firstNumber + secondNumber * thirdNumber;
MessageBox.Show(answer.ToString());
Upvotes: 0
Views: 729
Reputation: 3018
private void TxtBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar)) e.Handled = true; } //To allow for backspace: private void TxtBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!(Char.IsDigit(e.KeyChar) && (e.KeyChar == (char)Keys.Back))) e.Handled = true; }
If you want to add other allowable keys, look at the Keys enumeration and use the approach above.
3. To convert a string to int, use Int32.Parse.
4. Add & display the answer.
Upvotes: 0
Reputation: 125197
To convert a string to int, use Int32.Parse
or Int32.TryParse
or Convert.ToInt32
int firstNumber = Int32.Parse(firstNumberTextBox.Text);
//throws exception if not convertible
or
int firstNumber;
bool result = Int32.TryParse(firstNumberTextBox.Text, out firstNumber);
//return false if not convertible
or
int firstNumber;
result = Convert.ToInt32(firstNumberTextBox.Text);
//throws exception if not convertible
Using the
Convert.ToInt32(String)
method is equivalent to passing value to theInt32.Parse(String)
method. value is interpreted by using the formatting conventions of the current thread culture.If you prefer not to handle an exception if the conversion fails, you can call the
Int32.TryParse
method instead. It returns aBoolean
value that indicates whether the conversion succeeded or failed.
You can use int
instead of Int32
.
So in your case, it seems int.TryPars
better fits:
int firstNumber;
int secondNumber;
int thirdNumber;
int answer;
int.TryParse(firstNumberTextBox.Text, out firstNumber);
int.TryParse(secondNumberTextBox.Text, out secondNumber);
int.TryParse(thirdNumberTextBox.Text, out thirdNumber);
answer = firstNumber + secondNumber * thirdNumber;
MessageBox.Show(answer.ToString());
Upvotes: 2
Reputation: 14044
Create a Form or Window where you have 3 TextBoxes
naming firstNumber
, secondNumber
, thirdNumber
and than the calculations could be done something like this
MessageBox.Show((Convert.ToInt32(firstNumber.Text) +
Convert.ToInt32(secondNumber.Text) *
Convert.ToInt32(thirdNumber.Text)).ToString());
Less code to do the same thing (and possibly better).
Upvotes: 0
Reputation: 1626
Here how to use Convert.ToInt32() method,
int firstNumber;
int secondNumber;
int thirdNumber;
int answer;
firstNumber = Convert.ToInt32(textBox1.Text);
secondNumber = Convert.ToInt32(textBox2.Text);
thirdNumber = Convert.ToInt32(textBox2.Text);
answer = firstNumber + secondNumber* thirdNumber;
MessageBox.Show(answer.ToString());
but the problem in here is when the value of textbox
is not convertible to integer your application will give an error.
Upvotes: 0
Reputation: 8782
As pointed out already Int32.TryParse
will protect you from an exception if the user types in a value that's not a number.
But I would advise to prevent the user from typing in invalid values. You can achieve that by handling the KeyPress
event
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
Check this post for more details.
This way you will make sure the input is valid and it's safe to use Int32.Parse
or Convert.ToInt32
.
Upvotes: 0