Results 1 to 9 of 9

Thread: textbox.Clear does not clear :(

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    15

    Exclamation 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

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Select Case e.KeyChar.ToLower
    would have to be this:
    vb.net Code:
    1. Select Case Char.ToLower(e.KeyChar)
    That said, I'd just do this:
    vb.net Code:
    1. Select Case e.KeyChar
    2.     Case "m"c, "M"c
    3.         '...
    4.     Case "f"c, "F"c
    5.         '...
    6. End Select
    Note the comparing to Char literals instead of String literals too, which is more efficient as it avoids needless widening conversions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    15

    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


  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: textbox.Clear does not clear :(

    e.Handled = True is important here.

    vb.net Code:
    1. Private Sub txt_Quick_Gender_KeyPress( _
    2.     ByVal sender As Object, _
    3.     ByVal e As System.Windows.Forms.KeyPressEventArgs _
    4. ) Handles txt_Quick_Gender.KeyPress
    5.  
    6.     Select Case e.KeyChar
    7.         Case "m"c, "M"c
    8.             txt_Quick_Gender.Text = "Male"
    9.         Case "f"c, "F"c
    10.                 txt_Quick_Gender.Text = "Female"
    11.     End Select
    12.     e.Handled = True
    13. End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    15

    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

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    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:
    1. Select Case e.KeyChar.ToLower
    would have to be this:
    vb.net Code:
    1. Select Case Char.ToLower(e.KeyChar)
    That said, I'd just do this:
    vb.net Code:
    1. Select Case e.KeyChar
    2.     Case "m"c, "M"c
    3.         '...
    4.     Case "f"c, "F"c
    5.         '...
    6. 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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width