Thread: checking input

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    85

    checking input

    had a quick google for this but no luck ( well not what i wanted).

    checking input is a textbox is valid i.e. is a character or numeric value.

    any way to do this without importing user32.dll for the IsChar etc functions ?

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    http://msdn.microsoft.com/en-us/libr....tryparse.aspx
    http://msdn.microsoft.com/en-us/libr....tryparse.aspx

    you can then use each classes' .Parse methods to capture a string from a field. Also you may want to look into the ErrorProvider class which executes validation logic in a control's validation events automatically.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) Use int.TryParse
    2) Traverse each character and test with char.IsNumber
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    or just Convert.ToX and catch exception(s)

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    ok expanding on this a little

    im am using Leave event from the textbox to check the input.
    Code:
     private void textBox1_Leave(object sender, EventArgs e) 
            {
             ......
            }
    sender is my textbox and should i beleive hold all the relevant information about the control. i have checked this and can see the values in the IDE.
    My question is how do i access them ?
    if i try sender.text.tostring(), it complains that text isnt valid
    if i try sender.tostring(), i get the object reference and the text it holds as a single string.
    i need to segregate the actual text in the textbox from sender args - is this possible ? if so how ?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Sender is an "object", you need to cast it to a textbox:
    Code:
    var MyTextBox = sender as System.Windows.Forms.TextBox;
    var MyText = MyTextBox.Text;
    //Do something with MyText here
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    ok i was playing around with this ( and kind of came up with Magos' suggestion)
    but want i want to know is this the right way to do it ? i dont want to use it if it is wrong and i end up with a bad habit

    Code:
     private void textBox1_Leave(object sender, EventArgs e) // trim leading and trailing spaces
            {
                test_input.Strip_Spaces((TextBox) sender); <-- call class handler
             }
    
     public TextBox Strip_Spaces(TextBox obj)  <--- class test_input
            {
                obj.Text = obj.Text.TrimStart();  << --- can these two lines be merged into 1 statement ??
                obj.Text = obj.Text.TrimEnd();    <<---
                return obj;
            }
    this appears to do what i want and automatically updates the text in the textbox which saves me a job

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use "Trim()" instead, it's semantically equivalent to "TrimStart().TrimEnd()".
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, it is fine. Would prefer not to use obj for a TextBox, since obj would be like for Object, but details details.

    You could use Trim() I guess instead of TrimStart()+TrimEnd(). In which case this would be simpler:
    Code:
     
    private void textBox1_Leave(object sender, EventArgs e) // trim leading and trailing spaces
    {
                ((TextBox)sender).Text.Trim();
               //or if the above is kind of ugly
               //TextBox box = (TextBox)sender;
               //box.Text.Trim();
    }

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    Beauty is in the eye of the beholder - and as long as it works and is right - im blind

    edit:

    if i want to use this in the class ( for re-using later) is this a suitable method ?

    i can see that ((TextBox) sender).Text.Trim(); does the job fine as it is so is it worth recreating the wheel here ?

    edit:

    Code:
    private void textBox1_Leave(object sender, EventArgs e) // trim leading and trailing spaces
            {
                ((TextBox)sender).Text.Trim();
                
                
            }
    doesnt appear to trim btw (oops sry it does it just needs to be assigned back to the .Text) sry lol

    Code:
    ((TextBox) sender).Text = ((TextBox)sender).Text.Trim();
    Last edited by deviousdexter; 12-19-2008 at 09:45 AM.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    1) read up on the Validating event.
    2) if you have a choice between Convert.ToXXX(...) and XXX.TryParse(...), use the latter because it's faster.
    3) if you're dealing with references, the 'as' keyword is faster than a (cast).

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The job is fine, you don't need anything more.
    I always forget also to assign back values. Which is kind of tricky some time...
    Especially for string.Insert()!

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by bling View Post
    1) read up on the Validating event.
    2) if you have a choice between Convert.ToXXX(...) and XXX.TryParse(...), use the latter because it's faster.
    3) if you're dealing with references, the 'as' keyword is faster than a (cast).
    So he/she should do?

    Code:
    private void textBox1_Leave(object sender, EventArgs e) // trim leading and trailing spaces
    {
                TextBox boxStr = sender as TextBox;
                boxStr.Trim();
    }
    Why is it faster, btw? The "as" also checks if the cast is compatible, but the cast just casts...

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    they are faster because they won't throw exceptions. if the cast fails, the result is null.

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by bling View Post
    they are faster because they won't throw exceptions. if the cast fails, the result is null.
    If the cast is completely invalid (like a TextBox to a PictureBox) you won't be able to compile. If it is "just" invalid then it will throw an exception when something goes wrong.

    The difference with as is that it will set the pointer to null instead of throwing an exception. Wouldn't as be the equivalent with this?
    Code:
    try
    {
       a = (type)b;
    }
    catch(InvalidCastException)
    {
       a = null;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM
  3. Checking input
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-10-2002, 09:00 AM
  4. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 09:07 PM