自动在文本框中设置最小值和最大值 [英] Auto set minimum and maximum value on textbox

查看:54
本文介绍了自动在文本框中设置最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让文本框在用户将值放在最大值之上时自动设置最大值.例如,最小值为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.

它只是检查文本框的 Text 是否为数字,然后检查范围并应用适当的值

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";
    }
}

这篇关于自动在文本框中设置最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆