|
-
Apr 22nd, 2008, 10:35 PM
#1
Thread Starter
New Member
-
Apr 22nd, 2008, 10:42 PM
#2
Re: textbox.Clear does not clear :(
Try this:
Code:
Private Sub txt_Quick_Gender_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_Quick_Gender.KeyPress
Select Case e.KeyChar.ToLower
Case "m" : txt_Quick_Gender.Text = "Male"
Case "f" : txt_Quick_Gender.Text = "Female"
End Select
e.Handled = True
End Sub
-
Apr 22nd, 2008, 10:52 PM
#3
Re: textbox.Clear does not clear :(
Just a suggestion to JuggaloBrotha's code.. Avoid : whenever possible, it's not a good programming practice. And also not using it simplifies your code to read.
-
Apr 22nd, 2008, 11:02 PM
#4
Re: textbox.Clear does not clear :(
While I would not use it myself, I see no issue with using the colons in JB's code. In cases like that it actually can improve readability.
That's not what I posted for though. JB's code actually won't compile. Char.ToLower is Shared, so this:
vb.net Code:
Select Case e.KeyChar.ToLower
would have to be this:
vb.net Code:
Select Case Char.ToLower(e.KeyChar)
That said, I'd just do this:
vb.net Code:
Select Case e.KeyChar Case "m"c, "M"c '... Case "f"c, "F"c '... End Select
Note the comparing to Char literals instead of String literals too, which is more efficient as it avoids needless widening conversions.
-
Apr 22nd, 2008, 11:24 PM
#5
Thread Starter
New Member
Re: textbox.Clear does not clear :(
Thank you both 
Using the following (as the other doesn't compile) I still have the same result:
Code:
Private Sub txt_Quick_Gender_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_Quick_Gender.KeyPress
Select Case e.KeyChar
Case "m"c, "M"c
txt_Quick_Gender.Text = "Male"
Case "f"c, "F"c
txt_Quick_Gender.Text = "Female"
End Select
End Sub
result:
mMale
or
fFemale
-
Apr 22nd, 2008, 11:49 PM
#6
Re: textbox.Clear does not clear :(
Did you read JB's code? I was only talking about the Select Case statement. That's not all there is to the code JB posted.
-
Apr 23rd, 2008, 12:04 AM
#7
Re: textbox.Clear does not clear :(
e.Handled = True is important here.
vb.net Code:
Private Sub txt_Quick_Gender_KeyPress( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs _ ) Handles txt_Quick_Gender.KeyPress Select Case e.KeyChar Case "m"c, "M"c txt_Quick_Gender.Text = "Male" Case "f"c, "F"c txt_Quick_Gender.Text = "Female" End Select e.Handled = True End Sub
-
Apr 23rd, 2008, 12:29 AM
#8
Thread Starter
New Member
Re: textbox.Clear does not clear :(
ahem oops!
For those in the future here's the full working code (Thank you all!)
Code:
Private Sub txt_Quick_Gender_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_Quick_Gender.KeyPress
Select Case e.KeyChar
Case "m"c, "M"c
txt_Quick_Gender.Text = "Male"
Case "f"c, "F"c
txt_Quick_Gender.Text = "Female"
End Select
e.Handled = True
End Sub
-
Apr 23rd, 2008, 10:09 AM
#9
Re: textbox.Clear does not clear :(
 Originally Posted by jmcilhinney
While I would not use it myself, I see no issue with using the colons in JB's code. In cases like that it actually can improve readability.
That's not what I posted for though. JB's code actually won't compile. Char.ToLower is Shared, so this:
vb.net Code:
Select Case e.KeyChar.ToLower
would have to be this:
vb.net Code:
Select Case Char.ToLower(e.KeyChar)
That said, I'd just do this:
vb.net Code:
Select Case e.KeyChar
Case "m"c, "M"c
'...
Case "f"c, "F"c
'...
End Select
Note the comparing to Char literals instead of String literals too, which is more efficient as it avoids needless widening conversions.
Good call, I'd typed all of that code in Windows Notepad and couldn't remember if it would take the .ToLower() as is, in which case I would simply check for "m"c, "M"c like you're post here shows
As for the colon thing, I only use colon's ":" in very small cases like my earlier post. It simplifies the simple tasks can improves readability.
If there's more than 1 line of code following the Case statement then I would not use a colon all of the lines of code would be on their own line.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|