Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1569
  • Last Modified:

vb.net selstart, sellenght

Is there any way to activate the selstart/sellenght for all textbox in form automatically.
0
rflorencio
Asked:
rflorencio
  • 4
  • 3
  • 2
1 Solution
 
Shaun KlineLead Software EngineerCommented:
You would need to add a javascript function to each textbox's onfocus event to set the selStart and selLength values.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmm...WinForms or WebForms?  =D
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Shaun KlineLead Software EngineerCommented:
Good point, which is it?
0
 
rflorencioAuthor Commented:
Winforms
0
 
rflorencioAuthor Commented:
Instead of defining all GotFocus each textbox, I would do it automatically
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Example of #2 using a recursive routine at Form Load():
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WireTextBoxFocusEvent(Me)
    End Sub

    Private Sub WireTextBoxFocusEvent(ByVal cont As Control)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                AddHandler ctl.GotFocus, AddressOf AllTextBoxes_GotFocus
            ElseIf ctl.HasChildren Then
                WireTextBoxFocusEvent(ctl)
            End If
        Next
    End Sub

    Private Sub AllTextBoxes_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tb As TextBox = CType(sender, TextBox)
        tb.SelectAll()
    End Sub

End Class

Open in new window

0
 
Shaun KlineLead Software EngineerCommented:
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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now