CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2007
    Posts
    116

    Exclamation Numeric Textbox!

    Hi,
    I am trying to create a numeric textbox in C#.
    I got few useful links and used them but when I am using special characters then its crashes.

    Please advice.

    Thanks

  2. #2
    Join Date
    Dec 2009
    Posts
    596

    Re: Numeric Textbox!

    Textbox can only support text/string. You need to cast or convert or .tostring your value so it's appropriate before assigning to a textbox.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Numeric Textbox!

    Quote Originally Posted by viperbyte View Post
    Textbox can only support text/string. You need to cast or convert or .tostring your value so it's appropriate before assigning to a textbox.
    Well, "1" is a string, and so is "1.2345". I think that is what the OP means.

    However, I don't get how you expect us to psychically debug your code. I would bet that your validation routine is just poor and you are trying to convert a string of an incorrect format to an integer, floating point number, whatever.

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    Re: Numeric Textbox!

    I figured he was trying to do something like this: textbox1.text = 40; since he said it was crashing. But yeah you're probably right with what you said about what he was probably trying to do; escpecialy since he did mention special characters.

  5. #5
    Join Date
    Dec 2009
    Posts
    596

    Re: Numeric Textbox!

    I meant I thought he was trying to do something like: textbox1.text = IntegerVariable;. Man lately I can't express a thought accurately in writing on the first shot.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Numeric Textbox!

    I got the point the first time

  7. #7
    Join Date
    Sep 2010
    Posts
    1

    Re: Numeric Textbox!


  8. #8
    Join Date
    Jun 2009
    Posts
    144

    Re: Numeric Textbox!

    select your textbox and then from events double click on Keypress and paste this code

    Code:
    const char Delete = (char)8;
    e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete);
    this code will disable all characters on textbox except backspase and delete. if you want to use "," then just paste after != Delete this code

    Code:
    && (e.KeyChar != Convert.ToChar(",")

  9. #9
    Join Date
    Mar 2007
    Posts
    116

    Smile Re: Numeric Textbox!

    Thanks John & invador

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Numeric Textbox!

    What about a decimal? Do you need to be able to handle decimal numbers or only integers.

    Also invader, backspace is 8, delete is 46. Why don't you just use the Keys enumeration?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured