WPF中TextBox失去焦点事件

限制TextBox只能输入整数,而且整数的数值范围为0-100。如果输入101后,弹窗提示输入超限


MainWindow.xaml

xml 复制代码
 <TextBox x:Name="textBox1" TextWrapping="Wrap" Text="TextBox" Width="120" Height="50" LostFocus="myTextBox_LostFocus"/>

MainWindow.xaml.cs

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NumRange2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void myTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            // 在这里添加你的处理代码
            string textVal = textBox1.Text;
            try
            {
                int numVal = int.Parse(textVal);
                if (numVal > 100 || numVal < 0)
                {
                    MessageBox.Show("输入超限");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("请输入一个整数");
            }
            
        }
    }
}
相关推荐
FuckPatience4 小时前
WPF Telerik.Windows.Controls.Data.PropertyGrid 自定义属性编辑器
wpf
almighty2710 小时前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
军训猫猫头18 小时前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
我要打打代码18 小时前
在WPF项目中使用阿里图标库iconfont
wpf
拾忆,想起2 天前
Redisson 分布式锁的实现原理
java·开发语言·分布式·后端·性能优化·wpf
weixin_464078072 天前
wpf依赖注入驱动的 MVVM实现(含免费源代码demo)
wpf
beyond谚语2 天前
一、WPF入门介绍+Grid和StackPanel布局介绍+实战模拟Notepad++页面布局
wpf
CPU不够了2 天前
WPF常见问题清单
wpf·自适应
beyond谚语2 天前
二、WPF——Style样式玩法(通过资源字典将Style独立,全局调用)
wpf
光辉岁月~2 天前
使用CalcBinding实现复杂逻辑绑定
wpf