MaskedTextBox クラスとは? わかりやすく解説

MaskedTextBox クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

適切なユーザー入力不適切ユーザー入力区別するには、マスク使用します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public Class MaskedTextBox
    Inherits TextBoxBase
Dim instance As MaskedTextBox
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public class MaskedTextBox : TextBoxBase
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
[ComVisibleAttribute(true)] 
public ref class MaskedTextBox : public
 TextBoxBase
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public class MaskedTextBox extends TextBoxBase
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public class MaskedTextBox extends
 TextBoxBase
解説解説

MaskedTextBox クラスは、ユーザー入力受け入れまたは拒否するための宣言構文サポートする拡張されTextBox コントロールです。Mask プロパティ使用すると、アプリケーションカスタム検証ロジック作成しなくても、次の入力指定できます

実行時MaskedTextBox コントロール表示される場合マスク一連のプロンプト文字およびオプションリテラル文字として表されます。編集可能なマスク位置は、必須またはオプション入力表し単一プロンプト文字使用して表示されます。たとえば、シャープ記号 (#) は、数字入力のプレースホルダとしてよく使用されます。PromptChar プロパティ使用すると、カスタム プロンプト文字指定できます。HidePromptOnLeave プロパティは、コントロール入力フォーカス失ったときにプロンプト文字表示されるかどうか決定します

ユーザーマスクされたテキスト ボックス入力すると、有効な入力文字それぞれのプロンプト文字に順に置換されます。ユーザー無効な文字入力すると、置換行われません。BeepOnError プロパティtrue設定されている場合ビープ音出され、MaskInputRejected イベント発生します。このイベント処理すると、独自のカスタム エラー ロジック指定できます

現在の挿入位置リテラル文字場合複数オプション使用できます

MaskFull プロパティ使用すると、ユーザー必須の入力をすべて入力したかどうか検証できますText プロパティは、マスクおよび TextMaskFormat プロパティに従って書式設定されたユーザー入力を常に取得します

実際には、MaskedTextBox コントロールは、MaskedTextProvider プロパティ指定された System.ComponentModel.MaskedTextProvider クラス対すすべてのマスク処理遅延します。この標準プロバイダは、サロゲートおよび垂直方向の組み合わせ文字を除くすべてUnicode 文字サポートします。ただし、AsciiOnly プロパティ使用すると、入力文字セット a ~ z、A ~ Z、および 0 ~ 9 に制限できます

マスクは、ユーザー入力特定の型に対す有効な値を表すことを必ずしも保証していません。たとえば、年齢に対して -9 が入力されることがあります。その値の型のインスタンスを ValidatingType プロパティ割り当てると、ユーザー入力有効な値を表すかどうか検証できます。TypeValidationCompleted イベント監視すると、無効な値が含まれる場合ユーザーMaskedTextBox からフォーカス解除するかどうか検出できます。型の検証正常に行われると、TypeValidationEventArgs パラメータReturnValue プロパティ通じて値を表すオブジェクト使用できます

TextBox コントロールと同様、MaskedTextBox では、いくつかの一般的なショートカット キー使用できません。たとえば、Ctrl + R (右揃え)、Ctrl + L (左揃え)、Ctrl + L (中央揃え) の各キー使用できません。

Visual Basic 6.0 との互換性

MaskedTextBox は、Visual Basic 6.0マスク エディット コントロール のほとんどの機能保持するようにデザインされています。マスク エディット コントロール一般的なプロパティと、MaskedTextBox での等価プロパティの一覧を次の表に示します

マスク エディット コントロール (Visual Basic 6.0) プロパティ

等価の MaskedTextBox (.NET Framework) プロパティ

AllowPrompt プロパティ

AllowPromptAsInput

AutoTab プロパティ

なし

ClipMode プロパティ

CutCopyMaskFormat

ClipText プロパティ

Text (TextMaskFormat が ExcludePromptAndLiterals に設定されている場合)

Format プロパティ

なし

FormattedText プロパティ

Text (TextMaskFormat が IncludePromptAndLiterals に設定されている場合)

Mask プロパティ

Mask

PromptChar プロパティ

PromptChar

PromptInclude プロパティ

ResetOnPrompt

ValidationError イベント

MaskInputRejected

注意に関するメモ注意

MaskedTextBox コントロールは、複数行の構成元に戻す機能サポートしていません。ただし、これらの機能関連付けられたメンバが、TextBoxBase 基本クラスとの互換性のために保持されていますが、これらのメンバ実装アクション実行することはありません。

使用例使用例

日付受け入れるために MaskedTextBox初期化しMaskInputRejected イベントTypeValidationCompleted イベント両方使用してユーザー無効な入力であることを通知するコード例次に示します

Private Sub MaskedTextBox1_MaskInputRejected(ByVal
 sender As System.Object, ByVal e As
 System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
    If (Me.MaskedTextBox1.MaskFull) Then
        ToolTip1.ToolTipTitle = "Input Rejected - Too Much Data"
        ToolTip1.Show("You cannot enter any more data into the
 date field. Delete some characters in order to insert more data.",
 Me.MaskedTextBox1, Me.MaskedTextBox1.Location.X, Me.MaskedTextBox1.Location.Y, 5000)
    ElseIf (e.Position = Me.MaskedTextBox1.Mask.Length)
 Then
        ToolTip1.ToolTipTitle = "Input Rejected - End of Field"
        ToolTip1.Show("You cannot add extra characters to the
 end of this date field.", Me.MaskedTextBox1, Me.MaskedTextBox1.Location.X,
 Me.MaskedTextBox1.Location.Y, 5000)
    Else
        ToolTip1.ToolTipTitle = "Input Rejected"
        ToolTip1.Show("You can only add numeric characters (0-9)
 into this date field.", Me.MaskedTextBox1, Me.MaskedTextBox1.Location.X,
 Me.MaskedTextBox1.Location.Y, 5000)
    End If
End Sub

Private Sub Form1_Load(ByVal
 sender As System.Object, ByVal e As
 System.EventArgs) Handles MyBase.Load
    Me.ToolTip1.IsBalloon = True
    Me.MaskedTextBox1.Mask = "00/00/0000"
End Sub

Private Sub MaskedTextBox1_KeyDown(ByVal
 sender As Object, ByVal
 e As KeyEventArgs) Handles MaskedTextBox1.KeyDown
    ' The balloon tip is visible for five seconds; if the user types
 any data before it disappears, collapse it ourselves.
    Me.ToolTip1.Hide(Me.MaskedTextBox1)
End Sub
private void Form1_Load(object sender, EventArgs
 e)
{
    maskedTextBox1.Mask = "00/00/0000";

    maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
    maskedTextBox1.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);
}

void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs
 e)
{
    if (maskedTextBox1.MaskFull)
    {
        toolTip1.ToolTipTitle = "Input Rejected - Too Much Data";
        toolTip1.Show("You cannot enter any more data into the date field. Delete
 some characters in order to insert more data.", maskedTextBox1,
 maskedTextBox1.Location.X, maskedTextBox1.Location.Y, 5000);
    }
    else if (e.Position == maskedTextBox1.Mask.Length)
    {
        toolTip1.ToolTipTitle = "Input Rejected - End of Field";
        toolTip1.Show("You cannot add extra characters to the end of this
 date field.", maskedTextBox1, maskedTextBox1.Location.X, maskedTextBox1.Location.Y,
 5000);
    }
    else
    {
        toolTip1.ToolTipTitle = "Input Rejected";
        toolTip1.Show("You can only add numeric characters (0-9) into this
 date field.", maskedTextBox1, maskedTextBox1.Location.X, maskedTextBox1.Location.Y,
 5000);
    }
}

void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    // The balloon tip is visible for five seconds; if the user types
 any data before it disappears, collapse it ourselves.
    toolTip1.Hide(maskedTextBox1);
}
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.TextBoxBase
          System.Windows.Forms.MaskedTextBox
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「MaskedTextBox クラス」の関連用語

MaskedTextBox クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



MaskedTextBox クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS