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.