Q078. TextBoxの入力をカナや英数だけに制限したいが簡単な方法はないか

A.自分でロジックを組むこともできますが、GrapeCityWPFコンポーネント InputMan for WPF に含まれている GcTextBox を使えば、入力制限を簡単に設定できますInputMan for Windows Forms で長年培われた日本語入力技術がこのコンポーネントにも反映されてるので、安心して利用できます。



以下 GcTextBox と Livet を使ってサンプルを作ってみました。サンプルプログラムはこちらからダウンロードできます。

InputMan.zip 633KB


外観です。


コンボボックスで選択したフォーマットを GcTextBox にバインドします。


半角大文字アルファベットで入力制限します。入力時は IME の設定に左右されません。


全角カナ以外入力できません。


半角二進数のみ入力できます。(実際用途があるか不明ですが・・・)


全角カナと半角数字です。

ソースコード

以下はサンプルのソースです。まず Model。書式オブジェクトとコレクションを生成するファクトリクラスを用意します。

Option Explicit On
Option Strict On

Imports System.Collections.ObjectModel

Public Class FormatType
    Property Format As String
    Property Summary As String
End Class

Public Class FormatTypes
    Public Shared Function Create() As ObservableCollection(Of FormatType)
        Dim ret As New ObservableCollection(Of FormatType)
        ret.Add(New FormatType() With {.Format = "A", .Summary = "[全角]大文字のアルファベット(A〜Z)"})
        ret.Add(New FormatType() With {.Format = "A", .Summary = "[半角]大文字のアルファベット(A〜Z)"})
        ret.Add(New FormatType() With {.Format = "K", .Summary = "[全角]カタカナ(促音・拗音の小書き表記あり)"})
        ret.Add(New FormatType() With {.Format = "K", .Summary = "[半角]カタカナ(促音・拗音の小書き表記あり)"})
        ret.Add(New FormatType() With {.Format = "N", .Summary = "[全角]カタカナ(促音・拗音の小書き表記なし)"})
        ret.Add(New FormatType() With {.Format = "N", .Summary = "[半角]カタカナ(促音・拗音の小書き表記なし)"})
        ret.Add(New FormatType() With {.Format = "9", .Summary = "[全角]数字(0〜9)"})
        ret.Add(New FormatType() With {.Format = "9", .Summary = "[半角]数字(0〜9)"})
        ret.Add(New FormatType() With {.Format = "#", .Summary = "[全角]数字および数字関連記号(0〜9、+ - $ % \ , .)"})
        ret.Add(New FormatType() With {.Format = "#", .Summary = "[半角]数字および数字関連記号(0〜9、+ - $ % \ , .)"})
        ret.Add(New FormatType() With {.Format = "@", .Summary = "[全角]記号"})
        ret.Add(New FormatType() With {.Format = "@", .Summary = "[半角]記号"})
        ret.Add(New FormatType() With {.Format = "B", .Summary = "[全角]2進数(0または1)"})
        ret.Add(New FormatType() With {.Format = "B", .Summary = "[半角]2進数(0または1)"})
        ret.Add(New FormatType() With {.Format = "X", .Summary = "[全角]16進数(0〜9、A〜F、a-f)"})
        ret.Add(New FormatType() With {.Format = "X", .Summary = "[半角]16進数(0〜9、A〜F、a-f)"})
        ret.Add(New FormatType() With {.Format = "J", .Summary = "[全角]ひらがな(促音・拗音の小書き表記あり)"})
        ret.Add(New FormatType() With {.Format = "G", .Summary = "[全角]ひらがな(促音・拗音の小書き表記なし)"})
        ret.Add(New FormatType() With {.Format = "Z", .Summary = "[全角]すべての全角文字"})
        ret.Add(New FormatType() With {.Format = "T", .Summary = "[全角]サロゲート ペア文字"})
        ret.Add(New FormatType() With {.Format = "H", .Summary = "[半角]すべての半角文字"})
        ret.Add(New FormatType() With {.Format = "^", .Summary = "[半角]指定した書式に含まれないすべての文字"})
        ret.Add(New FormatType() With {.Format = "A9", .Summary = "[複合]全角アルファベットと半角数字"})
        Return ret
    End Function
End Class

次は ViewModel です。

Option Explicit On
Option Strict On

Imports System.Collections.ObjectModel
Imports Livet

Public Class MainViewModel
    Inherits ViewModel

    Public Sub New()
        Me.Types = FormatTypes.Create()
    End Sub

#Region "Format変更通知プロパティ"
    Private _Format As String

    Public Property Format() As String
        Get
            Return _Format
        End Get
        Set(ByVal value As String)
            If (_Format = value) Then Return
            _Format = value
            RaisePropertyChanged("Format")
        End Set
    End Property
#End Region

#Region "FormatType変更通知プロパティ"
    Private _FormatType As FormatType

    Public Property FormatType() As FormatType
        Get
            Return _FormatType
        End Get
        Set(ByVal value As FormatType)
            _FormatType = value
            RaisePropertyChanged("FormatType")
            If (_FormatType IsNot Nothing) Then
                Me.Format = _FormatType.Format
            End If
        End Set
    End Property
#End Region

#Region "Types変更通知プロパティ"
    Private _Types As ObservableCollection(Of FormatType)

    Public Property Types() As ObservableCollection(Of FormatType)
        Get
            Return _Types
        End Get
        Set(ByVal value As ObservableCollection(Of FormatType))
            _Types = value
            RaisePropertyChanged("Types")
        End Set
    End Property
#End Region

End Class


最後は View。何かの参考になれば幸いです。

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
        xmlns:local="clr-namespace:LivetWPFApplication1"
        xmlns:im="http://schemas.grapecity.com/windows/2010/inputman"
        Title="MainWindow" Height="150" Width="340" 
        WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <Grid>
        <ComboBox Margin="0,20,0,0" VerticalAlignment="Top" Width="280" 
                  ItemsSource="{Binding Types}"
                  SelectedItem="{Binding FormatType}"
                  MaxDropDownHeight="Auto"
                  SelectedIndex="0" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="28" Text="{Binding Path=Format}" />
                        <TextBlock Text="{Binding Path=Summary}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <im:GcTextBox Margin="0,65,0,0" VerticalAlignment="Top" Width="280"
                      Format="{Binding Format}" 
                      WatermarkDisplayNull="なにか入力してください" 
                      WatermarkDisplayNullForeground="Gray" />
    </Grid>
</Window>


WPF FAQ の目次に戻る