標籤

more tags...

最新回覆

[WinForm]利用C#做一個只可以輸入數值的Textbox(Only numbers Textbox)

小弟最近開始在寫WinForm的程式...想做一個只能輸入數值的Textbox

讓使用者只能輸入數值...小弟去找了一下資料...做了一個demo.介紹如何達到此功能...

WinForm(c#)

.cs

執行結果:

如果想把此textbox做成一個元件,可以繼承textbox,並且改寫OnKeyPress事件,以後就可以拉此元作重覆使用了

參考網址:

http://www.csharphelp.com/board2/read.html?f=1&i=58437&t=47961

推到 Twitter!
推到 Plurk!


 

2008/6/23 00:00| 閱讀數 : 5648 | 我要推薦 | 4 Comments | 文章分類 : C#(WinForm) 訂閱


回覆

# re: [WinForm]利用C#做一個只可以輸入數值的Textbox(Only numbers Textbox), Posted by 杜B on 2008/6/29 上午 03:43  回覆

似乎沒有做 copy and paste 的控制...

# re: [WinForm]利用C#做一個只可以輸入數值的Textbox(Only numbers Textbox), Posted by chhuang on 2008/6/29 下午 08:38  回覆

private bool IsBlocked;

 

to puma: private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

{

    if (IsBlocked) e.Handled = true;

 

    if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == 3 || e.KeyChar == 8 || e.KeyChar == 22)

    {

        return;

    }

    e.Handled = true;

}

 

private void textBox1_KeyDown(object sender, KeyEventArgs e)

{

    IsBlocked = false;

    if (e.Control && e.KeyCode == Keys.V)

    {

        string s = Clipboard.GetText();

        if ( string.IsNullOrEmpty(s) || Regex.Match(s, @"\D").Success)

        {

            IsBlocked = true;

        }

    }

}

# re: [WinForm]利用C#做一個只可以輸入數值的Textbox(Only numbers Textbox), Posted by Hei on 2008/7/6 下午 11:43  回覆

可以轉.NET 2.0的話, 可以試下masked textbox, 效果應該會比較好

# re: [WinForm]利用C#做一個只可以輸入數值的Textbox(Only numbers Textbox), Posted by Adolph on 2008/7/16 下午 06:14  回覆

應該不用那麼麻煩吧 好像用規則運算式就可以達到了

發表回覆

標題: *
姓名: *
Email: (將不會被顯示)
Url:
回覆: *
登入後使用進階評論
Please add 8 and 8 and type the answer here:
F6 Team logo


用BloggerAds 替自已加薪

每月文章

文章分類

推薦討論區

推薦部落格