Results 1 to 6 of 6

Thread: [RESOLVED] AutoSelect when moving to textboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    15

    Resolved [RESOLVED] AutoSelect when moving to textboxes

    I use the code below for moving to textboxes in a form, with enter and backward with shift+tab. I want to be able to autoselect the value when moving to textboxes…

    Thank you

    Code:
      Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If e.KeyCode = Keys.Enter AndAlso TypeOf Me.ActiveControl Is TextBox Then
                Me.SelectNextControl(Me.ActiveControl, Not e.Shift, True, True, True)
            End If
        End Sub

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: AutoSelect when moving to textboxes

    Do you want to select all the text in the active textbox?

    vb.net Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.     If e.KeyCode = Keys.Enter AndAlso TypeOf Me.ActiveControl Is TextBox Then
    3.         Me.SelectNextControl(Me.ActiveControl, Not e.Shift, True, True, True)
    4.         If TypeOf Me.ActiveControl Is TextBox Then DirectCast(Me.ActiveControl, TextBox).SelectAll()
    5.     End If
    6. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    15

    Re: AutoSelect when moving to textboxes

    Thanks for your reply.. It is almost working…

    The textboxes when I open my form are blank. I type a value in the first textbox and I move to next by pressing the enter. If I go back with shift tab it is not selecting all of my textbox. I have to compete a full “cyrcle” around my textboxes to start working.. Is there something else can be done..; I am trying to cath the Shift+Tab for e.keycode.. Can you provide some help..;

    Thank you

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: AutoSelect when moving to textboxes

    Why dont you just set the TabIndex and TabStop property of each control, to be in your preferred order?, no code is needed to setup the Tab and Shift-Tab actions.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    15

    Re: AutoSelect when moving to textboxes

    The properties you are suggesting are in preffered order. The problem is, when I move to previous textbox with shift+tab it is not autoselecting the value of the textbox to start overwiting. I must select it with mouse to start overwiting. It happens only when I press shift+tab. The edited code provided before was very helpful and I want to catch and shift+tab keystroke.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    15

    Re: AutoSelect when moving to textboxes

    I think i found it...

    Code:
        Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
            If Control.ModifierKeys = Keys.Shift And e.KeyCode = Keys.Tab Then
                If TypeOf Me.ActiveControl Is TextBox Then DirectCast(Me.ActiveControl, TextBox).SelectAll()
            End If
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width