zoukankan      html  css  js  c++  java
  • wpf怎么让Textbox只能输入数字?

    只需要两步:

    1.禁掉输入法:

    <Window x:Class="WpfModelViewApplication1.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
        Title="Main Window" Height="400" Width="800">
            <Grid x:Name="grid1">
                <TextBox x:Name="tb" Width="100" HorizontalAlignment="Right" Margin="0,164,122,128"  input:InputMethod.IsInputMethodEnabled="False"/>
            </Grid>
    </Window>
    第二步 采用正则表达式:

    <Window x:Class="WpfModelViewApplication1.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"
        Title="Main Window" Height="400" Width="800">

     <Grid x:Name="grid1">
                <TextBox x:Name="tb" Width="100" HorizontalAlignment="Right" Margin="0,164,122,128" PreviewTextInput="tb_PreviewTextInput" input:InputMethod.IsInputMethodEnabled="False"/>
            </Grid>

    </Window>

    cs后台代码:

            

    //using System.Text.RegularExpressions;
    
            private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
    
            {
    
                Regex re = new Regex("[^0-9.-]+");
    
                e.Handled = re.IsMatch(e.Text);
    
            }
  • 相关阅读:
    小程序-小菊花loading
    小程序-跳转
    小程序-拨打电话
    Pluto
    Android外部存储
    iOS微信内存监控
    iOS UITableView左滑操作功能的实现(iOS8-11)
    Hi,腾讯 WeTest 限免开放 Android Oreo 云真机,Android 8.1 可开测!
    揭密微信跳一跳小游戏那些外挂
    腾讯WeTest发布《2017中国移动游戏质量白皮书》,专注手游品质提升
  • 原文地址:https://www.cnblogs.com/lijinping321/p/4154968.html
Copyright © 2011-2022 走看看