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 ?
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 ?
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.
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.
ok expanding on this a little
im am using Leave event from the textbox to check the input.
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.Code:private void textBox1_Leave(object sender, EventArgs e) { ...... }
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 ?
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.
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
this appears to do what i want and automatically updates the text in the textbox which saves me a jobCode: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; }
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.
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(); }
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:
doesnt appear to trim btw (oops sry it does it just needs to be assigned back to the .Text) sry lolCode:private void textBox1_Leave(object sender, EventArgs e) // trim leading and trailing spaces { ((TextBox)sender).Text.Trim(); }
Code:((TextBox) sender).Text = ((TextBox)sender).Text.Trim();
Last edited by deviousdexter; 12-19-2008 at 09:45 AM.
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).
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()!
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; }