自动设置的最小和文本框最高可值文本框、最小、高可

2023-09-04 23:07:07 作者:沵的暖冷了俄的怀▂

我想使文本自动设定的最大值,当用户把上述最大值的值。示例min为0,最大为255。当用户把999在文本框中,它会自动设置为255为最大值。当用户把-11的文本框,它会自动设置为0的最小值。你可以看到GIF动画低于它应该是如何工作的。

I want to make the textbox automatically set the maximum value when the user put the value above the maximum value. Example min is 0 and max is 255. When user put 999 in the textbox, it automatically set to 255 as the max value. When user put -11 on textbox, it automatically set to 0 as the min value. You can see the gif animation below how it should work

我试过的if else 声明,但它不能将字符串转换为 INT

I had tried if else statement but it could not convert string to int.

推荐答案

您应该设置这个你想要这个功能对于每一个文本框。

you should set this on every text box you want this functionality for.

它只是检查文本文本框,如果它是数字那么它会检查范围和应用适当的值

it simply check for Text of the textbox if it is numerical then it checks for ranges and apply appropriate value

yourtextbox.TextChanged+= (s, e) =>
{
    var textbox = s as TextBox;
    int value;
    if (int.TryParse(textbox.Text, out value))
    {
        if (value > 255)
            textbox.Text = "255";
        else if (value < 0)
            textbox.Text = "0";
    }
}
 
精彩推荐