.net控制技巧:c# textbox數(shù)字的輸入
大家知道,在C#中,TextBox控件有keypress、keyup、和keydown事件等對輸入字符來控制。下面簡單談下在.net中c# textbox數(shù)字輸入的實現(xiàn)以及代碼示例。
當界面上要用戶輸入只有數(shù)字的字符時,默認的c# textbox數(shù)字是不能勝任的,網(wǎng)上有很多網(wǎng)友們提供了很多的做法,我總結(jié)了一下寫了一個在C#下的實現(xiàn),做到了如下的幾點:
1:只能輸入類似這樣的字符:-123456.789;1234.789;
2:在輸入的字符串中不能存在兩個點符:12456.78//正確;12.456.78//不正確;
3:如果表示負數(shù)可以在字符串的最前面加一個減號“-”,也只能加到弟一個字符的位置;
4:可以用復(fù)制粘帖功能和菜單功能,但是只對能正確格式的字符串有效,比如:12.34可以,Abc不可以;
5:只是得到一個字符串,還可以在這個基礎(chǔ)上再改進自己所需的,經(jīng)如添加對十六進制的支持等。
代碼如下在.NET下用C#寫的:
- using System;
- using System.Windows.Forms;
- namespace NumTextBox
- {
- ///
- /// NumTextBox 的摘要說明。
- ///
- public class TextBoxNumEx:System.Windows.Forms.TextBox
- {
- public const int WM_CONTEXTMENU = 0x007b;//右鍵菜單消息
- public const int WM_CHAR = 0x0102; //輸入字符消息(鍵盤輸入的,輸入法輸入的好像不是這個消息)
- public const int WM_CUT = 0x0300; //程序發(fā)送此消息給一個編輯框或combobox來刪除當前選擇的文本
- public const int WM_COPY = 0x0301; //程序發(fā)送此消息給一個編輯框或combobox來復(fù)制當前選擇的文本到剪貼板
- public const int WM_PASTE = 0x0302; //程序發(fā)送此消息給editcontrol或combobox從剪貼板中得到數(shù)據(jù)
- public const int WM_CLEAR = 0x0303; //程序發(fā)送此消息給editcontrol或combobox清除當前選擇的內(nèi)容;
- public const int WM_UNDO = 0x0304; //程序發(fā)送此消息給editcontrol或combobox撤消***一次操作
- public TextBoxNumEx()
- {
- //
- // TODO: 在此處添加構(gòu)造函數(shù)邏輯
- //
- }
- protected override void WndProc(ref Message m)
- {
- switch(m.Msg)
- {
- case WM_CHAR:
- System.Console.WriteLine(m.WParam);
- bool isSign = ((int)m.WParam == 45);
- bool isNum = ((int)m.WParam >= 48) && ((int)m.WParam <= 57);
- bool isBack = (int)m.WParam == (int)Keys.Back;
- bool isDelete = (int)m.WParam == (int)Keys.Delete;//實際上這是一個"."鍵
- bool isCtr = ((int)m.WParam == 24) || ((int)m.WParam == 22) || ((int)m.WParam == 26) ||((int)m.WParam == 3);
- if( isNum || isBack || isCtr)
- {
- base.WndProc (ref m);
- }
- if (isSign)
- {
- if (this.SelectionStart!=0)
- {
- break;
- }
- base.WndProc (ref m);
- break;
- }
- if (isDelete)
- {
- if (this.Text.IndexOf(".")<0)
- {
- base.WndProc (ref m);
- }
- }
- if ((int)m.WParam == 1)
- {
- this.SelectAll();
- }
- break;
- case WM_PASTE:
- IDataObject iData = Clipboard.GetDataObject();//取剪貼板對象
- if(iData.GetDataPresent(DataFormats.Text)) //判斷是否是Text
- {
- string str = (string)iData.GetData(DataFormats.Text);//取數(shù)據(jù)
- if (MatchNumber(str))
- {
- base.WndProc (ref m);
- break;
- }
- }
- m.Result = (IntPtr)0;//不可以粘貼
- break;
- default:
- base.WndProc (ref m);
- break;
- }
- }
- private bool MatchNumber(string ClipboardText)
- {
- int index=0;
- string strNum = "-0.123456789";
- index = ClipboardText.IndexOf(strNum[0]);
- if (index>=0)
- {
- if (index>0)
- {
- return false;
- }
- index = this.SelectionStart;
- if (index>0)
- {
- return false;
- }
- }
- index = ClipboardText.IndexOf(strNum[2]);
- if (index!=-1)
- {
- index = this.Text.IndexOf(strNum[2]);
- if (index!=-1)
- {
- return false;
- }
- }
- for(int i=0; i
- {
- index = strNum.IndexOf(ClipboardText[i]);
- if (index <0)
- {
- return false;
- }
- }
- return true;
- }
- }
- }
本文來自:編程論壇 作者:佚名
【編輯推薦】