views:

44

answers:

2

I have found a similar question to mine in http://stackoverflow.com/questions/97459/automatically-select-all-text-on-focus-in-winforms-textbox

Now i am trying to modify or make it some more different by making it general. I want to apply same action to all the textboxes in form without write code for each one... how many i dun know. As soon as i add a textbox in my form it should behave with similar action of selecting.

So wondering how to do it?

+1  A: 

Assuming you're going to use the accepted solution from the question you link to, all you'd need to do would be that whenever you create a new textbox, you use AddHandler to add the same 3 eventhandlers to each new textbox.

Then you need to change the event handlers to instead of referencing the textbox as this.textBox1 they'll reference it as CType(sender, TextBox) which means that they'll use the textbox that generated the event.

Edit: I'll add that line of code here as well since it's easier to read then

Private Sub TextBox_GotFocus (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
ho1
hm... quite interesting... i tried it... it worked even... but can we do something on form so that we dun need to add eventhandler to them
KoolKabin
@KoolKabin: I thought you meant that you add the textboxes dynamically? Otherwise if they're created in the designer you can just link them all to the same event handler like this: `Private Sub TextBox_GotFocus (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus`
ho1
thnx it worked. can we explore one more step ahead on form level event
KoolKabin
@KoolKabin: You mean that you want to reuse the same event for multiple forms? I think that since most form events are handled by "themselves" rather than by the owner (like the form handling a button click event) I'm not sure if it would make sense in the same way. But you could of course do something like inherit two forms from the same base and have the base handle the event, though it can get a little messy.
ho1
+1  A: 

The following code inherits from TextBox and implements the code you mentioned in http://stackoverflow.com/questions/97459/automatically-select-all-text-on-focus-in-winforms-textbox.

Once you've added the MyTextBox class to your project you can do a global search for System.Windows.Forms.Text and replace with MyTextBox.

The advantage of using this class is you can't forget to wire all the events for every textbox. Also if you decide on another tweak for all textboxes you have one place to add the feature.

Imports System
Imports System.Windows.Forms

Public Class MyTextBox
    Inherits TextBox

    Private alreadyFocused As Boolean

    Protected Overrides Sub OnLeave(ByVal e As EventArgs)
        MyBase.OnLeave(e)

        Me.alreadyFocused = False

    End Sub

    Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
        MyBase.OnGotFocus(e)

        ' Select all text only if the mouse isn't down.
        ' This makes tabbing to the textbox give focus.
        If MouseButtons = MouseButtons.None Then

            Me.SelectAll()
            Me.alreadyFocused = True

        End If

    End Sub

    Protected Overrides Sub OnMouseUp(ByVal mevent As MouseEventArgs)
        MyBase.OnMouseUp(mevent)

        ' Web browsers like Google Chrome select the text on mouse up.
        ' They only do it if the textbox isn't already focused,
        ' and if the user hasn't selected all text.
        If Not Me.alreadyFocused AndAlso Me.SelectionLength = 0 Then

            Me.alreadyFocused = True
            Me.SelectAll()

        End If

    End Sub

End Class
Tim Murphy
well this trick will do my task more efficiently thxn tim
KoolKabin