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("请输入一个整数");
            }
            
        }
    }
}
相关推荐
明耀14 分钟前
WPF RadioButton 绑定boolean值
c#·wpf
暮雪倾风1 小时前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
芝麻科技1 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退2 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆2 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退2 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu2 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘
笑非不退5 天前
WPF 设计属性 设计页面时实时显示 页面涉及集合时不显示处理 设计页面时显示集合样式 显示ItemSource TabControl等集合样式
wpf
△曉風殘月〆6 天前
WPF中的XAML详解
wpf·xaml
ithouse6 天前
使用WPF实现一个快速切换JDK版本的客户端工具
java·开发语言·wpf