8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio / WPF > link > TextBoxに数値しか入力できなくする > PreviewTextInput使用 | e.Handled = true;

Last updated at Posted at 2017-06-12
動作環境
Windows 7 Pro (32bit)
Microsoft Visual Studio 2017 Community

TextBoxに数値しか入力できなくするにはどうするか。

link1

https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf

answered Aug 12 '09 at 20:46
Ray
が参考になった。

TextBoxにはPreviewTextInputという指定が可能で、そこで、e.Handledへtrue/falseを代入するようだ。
e.Handled=true;だと処理がキャンセルされる。

MainWindow.xaml.cs
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;
//
using System.Text.RegularExpressions;

namespace _170612_t1610_onlyNumeric
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void uxMynumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9.-]+");
            var text = uxMynumber.Text + e.Text;
            e.Handled = regex.IsMatch(text);
        }
    }
}
MainWindow.xaml
<Window x:Class="_170612_t1610_onlyNumeric.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_170612_t1610_onlyNumeric"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Margin="20">
            <TextBlock Text="AlphaNumeric"/>
            <TextBox x:Name="uxAnyText" Width="150" Height="25"/>
            <TextBlock Text="Numeric"/>
            <TextBox x:Name="uxMynumber" 
                     PreviewTextInput="uxMynumber_PreviewTextInput"
                     Width="150" Height="25"/>
        </StackPanel>
    </Grid>
</Window>

数値しか受け付けないようにできた。
2017-06-12_15h55_49.png

後ろの---は入力できてしまう。

link2: 別案

nursの日記
WPF:数値しか受け付けないTextBox
http://d.hatena.ne.jp/nurs/20150427/1430150521

こちらの実装では、先頭に+も入力可能。

link1のv0.2

数値の後ろの"-"を入力不可にしてみた。

MainWindow.xaml.cs
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;
//
using System.Text.RegularExpressions;

namespace _170612_t1610_onlyNumeric
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void uxMynumber_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9.-]+");
            if (uxMynumber.Text.Length > 0 && e.Text == "-")
            {
                e.Handled = true;
                return;
            }

            var text = uxMynumber.Text + e.Text;
            e.Handled = regex.IsMatch(text);
        }
    }
}

コメント

(追記: 2019-07-16)

@nqdiorさんのコメントにてハイフンも制限する方法を教えていただきました。

情報感謝です。

8
8
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?