textbox.Clear does not clear :(
Hi all, just wondering what I'm doing wrong here? I'm a bit stumped :)
Code:
Private Sub txt_Quick_Gender_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_Quick_Gender.KeyPress
If e.KeyChar = "m" Then
txt_Quick_Gender.SelectAll()
txt_Quick_Gender.Text = ""
txt_Quick_Gender.Clear()
txt_Quick_Gender.Text = "Male"
ElseIf e.KeyChar = "f" Then
txt_Quick_Gender.SelectAll()
txt_Quick_Gender.Text = ""
txt_Quick_Gender.Clear()
txt_Quick_Gender.Text = "Female"
End If
End Sub
You can see I'm trying to change a "m" to "Male" and "f" to "Female".
I've even put in .text = "" and the .SelectAll() and still it comes out like this:
OUTPUT:
mMale
or
fFemale
I'm a bit stumped :(
Any help really appreciated...
Rob
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
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.
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.
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
:(
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.
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
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
Re: textbox.Clear does not clear :(
Quote:
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.