Link to home
Start Free TrialLog in
Avatar of rp
rpFlag for Portugal

asked on

vb.net selstart, sellenght

Is there any way to activate the selstart/sellenght for all textbox in form automatically.
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

You would need to add a javascript function to each textbox's onfocus event to set the selStart and selLength values.
Can you give more details?

If you want all text to be selected upon entry try something like:
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
        TextBox1.SelectAll()
    End Sub

Open in new window

Hmm...WinForms or WebForms?  =D
Good point, which is it?
Avatar of rp

ASKER

Winforms
Avatar of rp

ASKER

Instead of defining all GotFocus each textbox, I would do it automatically
Two ways:
(1) Add all of them after the "Handles" clause.
(2) Wire them up at run-time using AddHandler().

In both cases, use the "sender" parameter to determine which box fired the event.

Example of #1 with three TextBoxes:
*You can select all of them on the form, hit the lightning bolt, then select the GotFocus() event to add them all at once*
Private Sub AllTextBoxex_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
        Dim tb As TextBox = CType(sender, TextBox)
        tb.SelectAll()
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can generalize the function so you can use it for all GotFocus functions for all of your textboxes.
Private Sub GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus ...
        CType(sender, TextBox).SelectAll()
    End Sub

Open in new window