-1

I have one Text Box in windows application. This Text Box only allowed the integer value not string. Can anybody have solution ?

Sakthi
  • 21
  • 10

3 Answers3

0

Use this.

int value = Convert.ToInt32(textBox1.Text);

You use this code and get your integer value.Thanks

D.M Patel
  • 145
  • 1
  • 7
0

Convert it.

public int GetIntValue(TextBox tb)
{
    try
    {
        return Convert.toInt32(tb.Text);
    }
    catch (Exception ex)
    {
        //This is called if the converting failed for some reason
    }

    return 0; //This should only return 0 if the textbox does not contain a valid integer value
}

Use it like this:

int number = GetIntValue(textBox1);

Hope this helps!

0

I found a solution from C# How do I make a textbox that only accepts numbers

Hope it will help you.

    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;
        }
    }
Community
  • 1
  • 1
likewer
  • 216
  • 3
  • 16