2
Answers

how to prevent negative number input from user in textbox

Ask a question
Eric L

Eric L

11y
5k
1
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)