Textbox

Hi, I'm new to .NET framework. In the past I've used Borland and trying to learn visual c++.NET is a challenge. I'm not familar with MFC, so I'm diving straight into .NET. I have not found any good beginner/intermediate books.
Anyway, I'm trying to convert an entry into a Textbox (Textbox1->Text) to an int and want a simple way to check and see if the user entered a number or an alphabetic expression. I know I can convert using
          int x;                 
           x = System::Convert::ToInt32(Textbox1->Text); 
if the user enters a number. Problems though if you enter a letter.  


Answer this question

Textbox

  • Meylum

    I think they made the <b>Text</b>Box to store bytes, and display them in <b>Text</b> format.

    On Key Press would probably be the best choice to limit the number of bytes the <b>Text</b>Box can take as proper inputs.  Remember, Microsoft gives developers bare necessities to develop the applications they desire to or are required to.  

    This is probably what you're looking for.


    // in InitializeComponents, add this event handler.
    this.textBox1.KeyPress += 
         new System.Windows.Forms.KeyPressEventHandler(this.NumbericTextValidator);


    // now this is the event handler's method.
    private void NumbericTextValidator(
         object sender, 
         System.Windows.Forms.KeyPressEventArgs e) 
    {
    if ("0123456789".IndexOf(e.KeyChar) > 0)
    e.Handled = false;
    else
    e.Handled = true;
    }


    edited:
    by the way, for those that swear by the IsNumeric method of using int or double.parse.. this way is much faster and uses less resources when working with non decimal related validations. 

    ;)

  • phox

    I think that should be -1 and a dot and comma would help with decimals as in
    if (".0123456789".IndexOf(e.KeyChar) > -1)




  • ginee

    You're right.  Good looking out.  I wrote that late last night with only one eye open.  I wouldn't go with the commas since in financials, commas aren't used.  If you're going to validate for the period, than this way isn't the best route since you then have to ensure that it's not used more than once.  Anyways, good catch nonetheless.  Anyways, it's 3 am again.  hehe
  • Jim Vinsel

    I've been looking into this all day. I write code at work for number crunching and being able to input numeric data into a textbox seems basic to me. Why did Microsoft not allow numeric input into a textbox  Is there a numeric property for Textbox controls in visuall c++.net
    Is there any thrid party code for numeric input in visual c++.net  I have actually seen code where the string object is pasted to the clipboard just so the numeric data can be pulled back and used  Surely Microsoft did not intend this   This just doesn't that difficult of a problem to solve and yet I am no closer to solving it than when I started.

  • Textbox