Hi there,
I'm currently writing a code for a calculator meant to work out any value of a physics equation given every other value. While this part appears to be working and I've managed to incorporate TryParse into the conversion so that it will set any non-numerical value to zero, I am having difficulty figuring out how to prevent the entry of symbols or letters.
What I really need is either a code that will send a message if a symbol or letter is inserted into the text box or a text box that will refuse to accept an input other than a numerical value.
Can anyone help me? Any advice would be greatly appreciated.
Below is a sample of my current code.
Thank you.
What I have tried:
public partial class Form1 : Form
{
double P1, P2, V1, V2, U1, U2, v1, v2, h1, h2, Q, W, Z, g;
public Form1()
{
InitializeComponent();
Z = 0;
P1 = P2 = V1 = V2 = U1 = U2 = v1 = v2 = h1 = h2 = Q = W = Z;
g = 9.81;
}
private void txtBox_P2In_TextChanged(object sender, EventArgs e)
{
double.TryParse(txtBox_P2In.Text, out P2);
}
private void txtBox_P1In_TextChanged(object sender, EventArgs e)
{
double.TryParse(txtBox_P1In.Text, out P1);
}
private void CalcButton1_Click(object sender, EventArgs e)
{
if (radioButtonP1.Checked == true)
{
P1 = (((P2 * V2) + U2 + ((v2 * v2) / 2) + (g * h2) + W - Q - (g * h1) - ((v1 * v1) / 2) - U1) / V1);
txtBox_P1Out.Text = P1.ToString();
}
else if (radioButtonP1.Checked == false)
{
txtBox_P1Out.Text = null;
}
}
}