1

I want to create a function or sub that clears the text box when it gets focus. for example:

textbox1 contains: "Your name here."

and when the user clicks it. the "Your name here." would disappear. I've done it by placing textbox1.clear in the GotFocus event of a textbox.

Now I'm planning to put some more codes in it. but the thing is coding would be repetitive and long because I'm planning to do this in many text boxes. I want to minimize the coding so I want to create a function that clears textboxes when they got focused, so that I'll just call the function in GotFocus event and reduce the coding somehow.

I don't have an idea how to do that right now so If anyone has then I'm really thankful for the advice you can give me.

I'm using visual studio 2010, and creating a windows form application project.

Chris
  • 8,527
  • 10
  • 34
  • 51
response.write
  • 175
  • 5
  • 23

3 Answers3

5

Solution1

First know you can, In VB.Net, connect Multiple Events to a Single Event Handler in Windows Forms.

Private TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus
' Add event-handler code here.
End Sub

Solution2

Then, generic event handler that clears the text of a textbox (thanks @Miky Dinescu for original solution and C# code) is another possible solution to start to reduce code and share some methods .

Just put this code in your Form.vb, or in an new class HelperClass if you want to share methods between more that one form.

In Form.vb:

Private Sub ClearTextBox(ByVal sender As Object, ByVal e As EventArgs)
  If TypeOf sender Is TextBox Then
    (DirectCast(sender, TextBox)).Text = ""
  End If
End Sub

Or in HelperClass.vb:

Public Shared Sub ClearTextBox(ByVal sender As Object, ByVal e As EventArgs)
  If TypeOf sender Is TextBox Then
    (DirectCast(sender, TextBox)).Text = ""
  End If
End Sub

Then, you just attach this handler to all your text boxes, basically in constructor of the form, in FormLoad Event or in an "Initialize" method that you call before to show the form:

AddHandler textbox1.GotFocus, AddressOf Me.ClearTextBox
AddHandler textbox2.GotFocus, AddressOf Me.ClearTextBox

or

AddHandler textbox1.GotFocus, AddressOf HelperClass.ClearTextBox
AddHandler textbox2.GotFocus, AddressOf HelperClass.ClearTextBox

But it means you need to attach this handler to all your TextBox. If you have more than one handler, you need to apply each method to each TextBox...

And you should also remove all these event handler if you explictly call AddHandler when you close the form if you want to prevent memory leak...

RemoveHandler textbox1.GotFocus, AddressOf HelperClass.ClearTextBox
RemoveHandler textbox2.GotFocus, AddressOf HelperClass.ClearTextBox

So I would recommend this only in order to share methods between a bit of controls.

Edit

Of course code can be reduced again here by using a loop, like @DonA suggests. But do not forget RemoveHandler.

Solution3

Another solution would consist to create your own custom TextBox class that inherits from TextBox, and then use this Custom class in remplacement of TextBox, or replace existing TextBox if your have already created the project.

Public Class MyTextbox
  Inherits TextBox

Protected OverridesSub OnGotFocus(ByVal e As System.EventArgs)
    MyBase.OnGotFocus(e)
    Me.Text = String.Empty
End Sub    

End Class

Then Build project. You should now see MyTextbox in ToolBox. You can use it or replace your existing TextBox with MyTextBox

With this approach, you need to maintain only the methods of one Class. And no need to worry about handlers...

A great feature of oriented object programing is inheritance: Do not deprive yourself!

Finaly, I am not sure that clear the Text on GetFocus is a good approach for what you are trying to do. It seems to be a "Watermark TextBox" or "Cue Banner" in WinForms. So you may be interested by this:Watermark TextBox in WinForms or maybe consider using of Enter Event and prevent for deletion of user inputs.

Hope this helps.

Community
  • 1
  • 1
Chris
  • 8,527
  • 10
  • 34
  • 51
1

Find all textboxes and make array, loop thru array and add the handler for the got_Focus sub.

Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
  'array by type
  Dim tbs = Me.Controls.OfType(Of TextBox)()
  'loop thru and add the delegate 
  Array.ForEach(Of TextBox)(tbs.ToArray, Sub(tb) AddHandler tb.GotFocus, AddressOf got_Focus)
End Sub

Private Sub got_Focus(sender As Object, e As System.EventArgs)
  'cast our sender object to textbox and clear it's content
  DirectCast(sender, Textbox).Clear
End Sub
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
0

Ok, so I may be new to this community called StackOverflow.com but im not new with Desktop Programming nor programming in general because the same question would also most likely too apply with Web Forms as well. So here is an easier method which no one mentions at all, example below:

Public Class LoginForm1

      Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
        TextBox1.Text = ""

        End Sub

End Class

Now as you can see, when the EventArgs aka Event happends like, the mouse clicks on text box or TextBox1, it will automatically clear the box.