Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制数字文本框';s数值_C#_Wpf - Fatal编程技术网

C# 控制数字文本框';s数值

C# 控制数字文本框';s数值,c#,wpf,C#,Wpf,我有一个文本框,我只希望它是数字。因此,我在谷歌上搜索并找到了stackoverflow post,这对我已经有了很大帮助。但是,我希望现在只显示数字的文本框在我输入低于20的内容时显示警告。起初,我尝试了事件ondemideput,但没有成功,我尝试在与数字内容onpreviewtdemideput相同的事件中处理它。我对两者使用了相同的代码 if (!char.IsDigit(e.Text, e.Text.Length - 1)) { var text = e.Text; i

我有一个
文本框
,我只希望它是数字。因此,我在谷歌上搜索并找到了stackoverflow post,这对我已经有了很大帮助。但是,我希望现在只显示数字的文本框在我输入低于20的内容时显示警告。起初,我尝试了事件
ondemideput
,但没有成功,我尝试在与数字内容
onpreviewtdemideput
相同的事件中处理它。我对两者使用了相同的代码

if (!char.IsDigit(e.Text, e.Text.Length - 1))
{
    var text = e.Text;
    int num;

    var success = int.TryParse(text, out num);
    if (!success)
        return;

    if (num <= 20)
    {
        if (MessageBox.Show("Are you sure you want to go underneath 20?",
                            "... You sure?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) ==
                        MessageBoxResult.Yes)
                          e.Handled = true;

         else
         {
            num++;
            textBox.Text = num.ToString();
         }
    }

                e.Handled = true;
}
if(!char.IsDigit(e.Text,e.Text.Length-1))
{
var text=e.text;
int-num;
var success=int.TryParse(文本,out num);
如果(!成功)
返回;
如果(num使用
要捕获它的变化,请进行筛选

按照您的实现可以如下所示:

private void yourTextBox_TextChanged(object sender, EventArgs e)
{
    if (!char.IsDigit(e.Text, e.Text.Length - 1))
        {
            var text = e.Text;
            int num;

            var success = int.TryParse(text, out num);
            if (!success)
                return;

            if (num <= 20)
            {
                if (
                    MessageBox.Show("Are you sure you want to go underneath 20?",
                        "... You sure?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) ==
                    MessageBoxResult.Yes)
                    e.Handled = true;
                else
                {
                    num++;
                    textBox.Text = num.ToString();
                }
            }

            e.Handled = true;
        }
    }
private void yourTextBox\u TextChanged(对象发送者,事件参数e)
{
if(!char.IsDigit(e.Text,e.Text.Length-1))
{
var text=e.text;
int-num;
var success=int.TryParse(文本,out num);
如果(!成功)
返回;
如果(num使用
要捕获它的变化,请进行筛选

按照您的实现可以如下所示:

private void yourTextBox_TextChanged(object sender, EventArgs e)
{
    if (!char.IsDigit(e.Text, e.Text.Length - 1))
        {
            var text = e.Text;
            int num;

            var success = int.TryParse(text, out num);
            if (!success)
                return;

            if (num <= 20)
            {
                if (
                    MessageBox.Show("Are you sure you want to go underneath 20?",
                        "... You sure?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) ==
                    MessageBoxResult.Yes)
                    e.Handled = true;
                else
                {
                    num++;
                    textBox.Text = num.ToString();
                }
            }

            e.Handled = true;
        }
    }
private void yourTextBox\u TextChanged(对象发送者,事件参数e)
{
if(!char.IsDigit(e.Text,e.Text.Length-1))
{
var text=e.text;
int-num;
var success=int.TryParse(文本,out num);
如果(!成功)
返回;

如果(num为什么不处理
KeyDown
事件

private void txtNumeric_KeyDown(object sender, KeyEventArgs e)  
 {  
    e.Handled = !char.IsDigit(e.KeyChar); 
    if (Int32.Parse(sender.Text) <= 20)
        {
           if (
                MessageBox.Show("Are you sure you want to go underneath 20?",
                    "... You sure?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) ==
                MessageBoxResult.Yes)
                e.Handled = true;
            else
            {
                num++;
                textBox.Text = num.ToString();
            }

 } 
private void txtNumeric\u KeyDown(对象发送方,KeyEventArgs e)
{  
e、 Handled=!char.IsDigit(e.KeyChar);

如果(Int32.Parse(sender.Text)为什么不处理
KeyDown
事件呢

private void txtNumeric_KeyDown(object sender, KeyEventArgs e)  
 {  
    e.Handled = !char.IsDigit(e.KeyChar); 
    if (Int32.Parse(sender.Text) <= 20)
        {
           if (
                MessageBox.Show("Are you sure you want to go underneath 20?",
                    "... You sure?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) ==
                MessageBoxResult.Yes)
                e.Handled = true;
            else
            {
                num++;
                textBox.Text = num.ToString();
            }

 } 
private void txtNumeric\u KeyDown(对象发送方,KeyEventArgs e)
{  
e、 Handled=!char.IsDigit(e.KeyChar);


if(Int32.Parse(sender.Text)你说数字不能低于20,但你有一个比较:num是的,我希望20仍然是OK我相信这篇文章也会帮助你:它谈论TextBox的TextChanged事件,所以你可能可以做一些类似的事情-On TextChanged get TextBox value,确保它是一个数字,如果是的话。确保它大于或等于20(或如果小于20 do X)你为什么不使用NumericTextBox?你说数字不能低于20,但你有一个比较:num是的,我希望20仍然是OK我相信这篇文章也会帮助你:它谈论TextBox的TextChanged事件,所以你可能可以做一些类似于-On TextChanged get TextBox value的事情,确保它是一个数字,如果是的话。确保它是一个数字大于或等于20(或如果小于20 do X)为什么不使用NumericTextBox?我完全忽略了一个事实,即每当有人现在写75%时,7也会被处理,这意味着我不能在没有通知的情况下输入任何数字……有什么想法吗-O@John如果
yourTextBox.Text.Length>1
@Pikoh,那么我无法处理输入是否为3,这也是禁止的。是的,你可以。但无论如何,这对我来说似乎是一个糟糕的设计,很难实现。我建议你使用任何特定于数字的控件,而不是文本框,并在验证控件或formI时检查值。我同意@Pikoh。即使你想这样做,也要将其视为一个数字而不是字符。我完全忽略的一个事实是现在有人写了75%,7也被处理了,这意味着我不能在没有通知的情况下输入任何数字……有什么想法吗-O@John如果
yourTextBox.Text.Length>1
@Pikoh,那么我无法处理输入是否为3,这也是禁止的。是的,你可以。但无论如何,这对我来说似乎是一个糟糕的设计,很难实现。我建议gest您可以使用任何特定于数字的控件而不是文本框,并在验证控件或formI是否与@Pikoh一致时检查值。即使您想这样做,也可以将其视为数字而不是字符。因为显然没有。抱歉,没有看到WPF标记。更新答案以使用
键控
,因为显然没有ry没有看到WPF标记。已更新答案以使用
KeyDown