Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I need when the user Write for Example : 10.10
I need to Show Error Message :
This textbox Only Integer :
Just Accept Only Integer Number Like 10


private void txtQty_TextChanged(object sender, EventArgs e)
{

}
Posted

For Decimal you use Decimal.TryParse and for integers use Int32.TryParse.
C#
private void txtQty_TextChanged(object sender, EventArgs e)
{
	decimal txtbox1;
	if (Decimal.TryParse(txtBoxDecimal.Text, out txtbox1))
	{
		//do something with txtbox1
	}
	else
	{
		lblMessage.Text = "* Not a decimal.";
		return;
	}

	int32 txtbox2;
	bool result = Int32.TryParse(txtBoxInteger.Text, out txtbox2);
	if (result)
	{
		//do something with txtbox2        
	}
	else
	{
		lblMessage.Text = "* Not a number.";
		return;         
	}
}
 
Share this answer
 
Comments
Member 11280947 24-Apr-15 15:56pm    
Thank You
C#
private void txtQty_TextChanged(object sender, EventArgs e)
        {
            int number;
            bool result = Int32.TryParse(txtQty.Text, out number);
            if (!result)
            {
                MessageBox.Show("Invalid Input");
                txtQty.Text = "";
            }

        }
 
Share this answer
 
Comments
Member 11280947 24-Apr-15 15:57pm    
Thank You
Use NumericUpDown control, this will force the user to enter either int or decimal.

To force user enter int value:
C#
NumericUpDown.DecimalPlaces = 0;

To force user enter decimal value:
C#
NumericUpDown.DecimalPlaces = 2;


This will save your effort to validate, verify and filter the user input.
 
Share this answer
 
Comments
Alan N 24-Apr-15 8:34am    
The DecimalPlaces property doesn't do quite what we expect. It only controls the display format. Try setting DecimalPlaces = 0 and Increment = 1 and then type 1.5 into the control and press enter. The display will show 2 but the Value is 1.5. The arrows now increment by 1 on the actual value, i.e. 2.5, 3.5, 4.5 and the display shows 3, 4, 5. I spent a long time trying to figure out why the decimal value 3 cast to an int was 2!
adriancs 24-Apr-15 9:23am    
If you need 2.5, then use NumericUpDown.DecimalPlaces = 1; NumericUpDown.Increment = 0.5;
Alan N 24-Apr-15 9:49am    
My comment was just to make the point that the displayed and actual values can be different and settings DecimalPlaces to zero does not enforce any validation of the actual value. Visually it appears to restrict to integral values but that is just an illusion. For NumericUpDowns where integral values should be enforced I add a ValueChanged event handler and truncate the Value.
Member 11280947 24-Apr-15 15:56pm    
Thank You

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900