Eric L

Eric L

  • NA
  • 1
  • 9.2k

how to prevent negative number input from user in textbox

Dec 9 2012 12:07 AM
This is what I have:

private void btnCompute_Click(object sender, EventArgs e)

       {

double width = 0;

     


double length = 0;


           


double height = 0;



           


if (!double.TryParse(this.tbWidth.Text.Trim(), out width))


            {


               


MessageBox.Show("The width is invalid.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error);


               


return;


            }



           


if (!double.TryParse(this.tbHeight.Text.Trim(), out height))


            {


               


MessageBox.Show("The height is invalid.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error);


               


return;


            }



           


if (!double.TryParse(this.tbLength.Text.Trim(), out length))


            {


               


MessageBox.Show("The length is invalid.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Error);


               


return;


            }

It accepts negative numbers, but I want to able to show an error message when a negative number is entered for the width, height, or length.


Any help is greatly appreciated, Thanks!



         


Answers (2)