0

I am attempting to give a winforms Textbox control placeholder text by programmatically setting the respective control's text during the GetFocus and LostFocus events. However, for whatever reason the control never reflects the updated text.

This is what I'm attempting:

Private Sub TextBoxEmail_GotFocus(ByVal sender As Object, ByVal e As EventArgs) Handles TextBoxEmail.GotFocus
    Dim this As TextBox = DirectCast(sender, TextBox)
    With this
        If .Text = "Email Address" Then
            .ForeColor = Bootstrap.Utilities.Color.TextBody
            .Text = String.Empty
        End If
    End With
End Sub

Private Sub TextBoxEmail_LostFocus(ByVal sender As Object, ByVal e As EventArgs) Handles TextBoxEmail.LostFocus
    Dim this As TextBox = DirectCast(sender, TextBox)
    With this
        .ForeColor = Bootstrap.Utilities.Color.TextLight
        If String.IsNullOrWhiteSpace(.Text) Then
            .Text = "Email Address"
        End If
    End With
End Sub

What is odd is that if I setup a breakpoint in the LostFocus event handler and step through the code using the F11 shortcut, it is constantly cycles through the GotFocus and LostFocus events.

David
  • 5,877
  • 3
  • 23
  • 40
  • 1
    That's really the wrong way to go about that. The ability to display prompt text in a `TextBox` is built into the OS so you should use that. https://www.bing.com/search?q=vb.net+textbox+cue+banner&PC=U316&cvid=8d4542dee6d1413ba515851d78cd6a77&&setLang=en-US&FORM=ANNTA1 – jmcilhinney May 31 '19 at 06:18
  • As the documentation says, an application should not be handling the `GotFocus` and `LostFocus` events. If you were going to go that way, you should handle `Enter` and `Leave`. – jmcilhinney May 31 '19 at 06:31
  • 1
    Using a debugger is part of the problem, a breakpoint switches focus to the IDE and that causes LostFocus. You should always use the Enter and Leave events in a Winforms app. Why you are otherwise having trouble is not obvious, "updated text" is murky. Smells like the usual trouble caused by vb.net's default instance feature for forms. Do favor the [built-in support](https://stackoverflow.com/a/4902969/17034) for a cue banner. – Hans Passant May 31 '19 at 08:11
  • Thank y'all. I wasn't aware of the PInvoke solution so I will go with that. I also would've never guessed the keyword was "cue banner" and I suppose that is why I didn't find much using the keyword "placeholder". – David May 31 '19 at 13:18

1 Answers1

1

It happens because you are debugging the code.

When your text box gets focus at that time GetFocus event will call unfortunately you have put a breakpoint in GetFocus as well as LostFocus event. so your form Lose the focus and that focus comes to the visual studio so LostFocus event will occur after that again after the debuging process is complete your text box get focused again so again GetFocus event will call and its process will create Cycle while you debug the code.

I hope this will help you.

Hiren Patel
  • 1,071
  • 11
  • 34