0
0

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 1 year has passed since last update.

C# Windowsアプリ作成時 備忘録

Last updated at Posted at 2023-05-08

テキストボックスを数値のみ入力に制限する方法

  • ImeModeプロパティをDisableに変更
  • ShortcutsEnabledプロパティを(コピペ不可)Falseに変更
  • ([カミナリマーク]をクリックして)KeyPressイベント入力欄に[txtNumOnly_KeyPress]と入力

KeyPressイベント

private void txtNumOnly_KeyPress(object sender, KeyPressEventArgs e)
{
    //バックスペースが押された時は有効(Deleteキーも有効)
    if (e.KeyChar == '\b'){
        return;
    }

    //数値0~9以外が押された時はイベントをキャンセル
    if ((e.KeyChar < '0' || '9' < e.KeyChar)){
        e.Handled = true;
    }
}
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?