入力マスクの使い方

MaskedTextBoxを使って電話番号を入力する例です。
あらかじめ入力して欲しい書式をMaskプロパティーに設定しておき
未入力部分があることを示すためにPromptCharプロパティーに_文字を設定しています。

maskedtextbox

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 入力マスク書式を設定(携帯などの電話番号を仮定)
            maskedTextBox1.Mask = "999-9999-9999";

            // 未入力部分に表示される文字を設定(不要であれば''を設定)
            maskedTextBox1.PromptChar = '_';
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(maskedTextBox1.Text);
        }
    }
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)