Solved

vb.net selstart, sellenght

Posted on 2010-11-18
9
1,395 Views
Last Modified: 2012-05-10
Is there any way to activate the selstart/sellenght for all textbox in form automatically.
0
Comment
Question by:rflorencio
  • 4
  • 3
  • 2
9 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34166376
You would need to add a javascript function to each textbox's onfocus event to set the selStart and selLength values.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34166385
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34166393
Hmm...WinForms or WebForms?  =D
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34166438
Good point, which is it?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:rflorencio
ID: 34166545
Winforms
0
 

Author Comment

by:rflorencio
ID: 34166563
Instead of defining all GotFocus each textbox, I would do it automatically
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34166655
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34166683
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
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 34166686
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now