简体   繁体   中英

Only allow integers in a winform combobox

I am currently working on programming a photo editor with c# and I am currently designing the ability to allow the pen tool to change sizes. It works flawlessly except for one problem. Here is some background information: So inside a combobox that I have, there are 10 Items, each are numbers 1 - 10. If I select one, or directly type some number into the combobox, it will set the pen size to that. The problem is, if I type a letter it gives me a

IndexOutOfRangeException

.

Is there a way I can make it so the combobox only accepts integers and floats? Basically what I mean is if I push 3 , the pen size will change to 3. But if I push H , it does nothing.

You can do either of the two options. The first option is restrict the user to type in the comboobx by Disable typing. This can be achieved by giving this code in the page_load

 comboBox1.DropDownStyle to ComboBoxStyle.DropDownList

or Access the value like the following:

       if (int.TryParse(comboBox1.Text, out BreshSize))
        {
            // Proceed
        }
        else 
        { 
        //Show errror message
        }  

This implementation should allow you see if the new value is an integer and act accordingly. You would place this inside the code when you start to check the value. "2" would be replaced with the string you are checking.

    int currInt = 0;
    int tryInt = 0;
    if(int.TryParse("2", out tryInt))
    {
        currInt = tryInt;         
    }
    else
    {
        //reset or display a warning
    }

Also, you could use the KeyPress handler to make sure just digits were typed.

private void txtPenToolSize_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}

One generic implementation for international users with different System decimal separator (locale), and texbox/combobox allowing not only Int numbers formats (double, float, decimal, etc..)

    private void comboTick_KeyPress(object sender, KeyPressEventArgs e)
    {
        //this allows only numbers and decimal separators
        if (!char.IsControl(e.KeyChar) 
            && !char.IsDigit(e.KeyChar) 
            && (e.KeyChar != '.') 
            && (e.KeyChar != ',') )
        {
            e.Handled = true; //ignore the KeyPress
        }
        
        //this converts either 'dot' or 'comma' into the system decimal separator
        if (e.KeyChar.Equals('.') || e.KeyChar.Equals(','))
        {
            e.KeyChar = ((System.Globalization.CultureInfo)System.Globalization.CultureInfo.CurrentCulture)
                .NumberFormat.NumberDecimalSeparator
                .ToCharArray()[0];
            e.Handled = false; //accept the KeyPress
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM