- View the source.
- Go to SampleCodes/DFL/TextBox/4.
Cur: 2012-01-26 (Thu) 02:46:26 SHOO ![]() ![]() 修正 |
|||
---|---|---|---|
Line 1: | Line 1: | ||
+ | TITLE:数値しか入力できないテキストボックス | ||
+ | // ページタイトルを記入しましょう | ||
+ | * 概要 [#bb23c545] | ||
+ | 数値しか入力することの出来ないテキストボックスを作成するには、onKeyPressとonTextChangedをオーバーライドする必要があります。 | ||
+ | 参考: http://dobon.net/vb/dotnet/control/numerictextbox.html | ||
+ | |||
+ | * ポイント [#b9040a98] | ||
+ | 文字列はUTF-8ですので、マルチバイトな文字列に対応するため、foreach (dchar c; text) を利用して、UTF-8からUTF-32に変換してswitch しています。逆に、 char[] な配列に dchar を連結させることで、UTF-8に変換しなおしています。 | ||
+ | |||
+ | KeyPressEventArgs や KeyEventArgs には handled というメンバがあり、これを true にすることで、入力を無視することなどが可能となります。 | ||
+ | |||
+ | * サンプルコード [#s9c53c71] | ||
+ | #code(d){{{ | ||
+ | import dfl.all; | ||
+ | pragma(lib, "dfl"); | ||
+ | |||
+ | class NumericTextBox: TextBox | ||
+ | { | ||
+ | void onKeyPress(KeyPressEventArgs ea) | ||
+ | { | ||
+ | switch (ea.keyChar) | ||
+ | { | ||
+ | case 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||
+ | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z': | ||
+ | ea.handled = true; | ||
+ | break; | ||
+ | default: | ||
+ | super.onKeyPress(ea); | ||
+ | } | ||
+ | } | ||
+ | void onTextChanged(EventArgs ea) | ||
+ | { | ||
+ | string str; | ||
+ | auto ss = selectionStart; | ||
+ | foreach (dchar c; text) | ||
+ | { | ||
+ | switch (c) | ||
+ | { | ||
+ | case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': | ||
+ | str ~= c; | ||
+ | break; | ||
+ | default: | ||
+ | ss--; | ||
+ | } | ||
+ | } | ||
+ | if (str != text) | ||
+ | { | ||
+ | text = str; | ||
+ | selectionStart = ss; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void main() | ||
+ | { | ||
+ | auto form = new Form; | ||
+ | auto tb = new NumericTextBox; | ||
+ | tb.parent = form; | ||
+ | tb.dock = DockStyle.TOP; | ||
+ | Application.run(form); | ||
+ | } | ||
+ | }}} | ||
+ | 実行結果はGUIで、挙動が問題なため省略 | ||
+ | |||
+ | * 投票とコメント [#df26d1de] | ||
+ | #vote(大変参考になった,参考になった,あまり参考にならなかった,まったく参考にならなかった,#notimestamp) | ||
+ | #pcomment |
- Backup diff of SampleCodes/DFL/TextBox/4(No. All)
- Cur: 2012-01-26 (Thu) 02:46:26 SHOO
- 修正
- Cur: 2012-01-26 (Thu) 02:46:26 SHOO
Page Info | |
---|---|
Page Name : | SampleCodes/DFL/TextBox/4 |
Page aliases : | None |
Page owner : | SHOO |
Can Read | |
Groups : | All visitors |
Users : | All visitors |
Can Edit | |
Groups : | All visitors |
Users : | All visitors |
Counter: 1813,
today: 2,
yesterday: 0