Results 1 to 8 of 8

Thread: Why can not select all text in textbox after focus to textbox ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Why can not select all text in textbox after focus to textbox ?

    In then form there is TextBox and a ListBox. When I Press Down or Up Key I want to Select ListBox item. Then After Selected item comes into the TextBox and Then after I want to select all Text in TextBox. But I couldn't do it.

    Where must I change into the code ?

    Advanced Thanks.

    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           ListBox1.Items.Add("Times New Roman")
           ListBox1.Items.Add("Arial")
           ListBox1.Items.Add("Arial Narrow")
           ListBox1.Items.Add("Monotype corsiva")
           ListBox1.Items.Add("Verdana")
           ListBox1.Items.Add("Andelip")
           ListBox1.Items.Add("Obit BT")
           ListBox1.Items.Add("LCD")
           ListBox1.Items.Add("Western")
           ListBox1.Items.Add("Roman")
    
           ListBox1.SelectedIndex = 1
           TextBox1.Text = ListBox1.SelectedItem.ToString
    
       End Sub
    
       Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
           If e.KeyCode = Keys.Down Then
               If ListBox1.SelectedIndex <= ListBox1.Items.Count - 2 Then
                   ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
                   TextBox1.Text = ListBox1.SelectedItem.ToString
    
                   ListBox1.Focus()
               End If
           End If
    
           If e.KeyCode = Keys.Up Then
               If ListBox1.SelectedIndex >= 1 Then
                   ListBox1.SelectedIndex = ListBox1.SelectedIndex - 1
                   TextBox1.Text = ListBox1.SelectedItem.ToString
    
                   ListBox1.Focus()
               End If
           End If
       End Sub
    
       Private Sub ListBox1_GotFocus(sender As Object, e As EventArgs) Handles ListBox1.GotFocus
           TextBox1.SelectAll()
           TextBox1.Focus()
       End Sub

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Why can not select all text in textbox after focus to textbox ?

    Hello,

    that's because you use the TextBox1.KeyDown event instead of using the listbox1.KeyDown event...

    regards
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: Why can not select all text in textbox after focus to textbox ?

    By the way , there were a few other errors :

    the index start at 0, not 1
    when you press the down key the listbox already go to next element so when you do that
    Code:
     ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
    you go to the second next element..

    the next element is selected after the end of the keydown event so you need
    Code:
    TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex + 1).ToString
    instead of
    Code:
    TextBox1.Text = ListBox1.SelectedItem.ToString
    here is a correction

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            ListBox1.Items.Add("Times New Roman")
            ListBox1.Items.Add("Arial")
            ListBox1.Items.Add("Arial Narrow")
            ListBox1.Items.Add("Monotype corsiva")
            ListBox1.Items.Add("Verdana")
            ListBox1.Items.Add("Andelip")
            ListBox1.Items.Add("Obit BT")
            ListBox1.Items.Add("LCD")
            ListBox1.Items.Add("Western")
            ListBox1.Items.Add("Roman")
    
            ListBox1.SelectedIndex = 0
            TextBox1.Text = ListBox1.SelectedItem.ToString
    
        End Sub
    
        Private Sub listbox_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
            If e.KeyCode = Keys.Down Then
                If ListBox1.SelectedIndex < ListBox1.Items.Count Then
                    TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex + 1).ToString
                End If
            End If
    
            If e.KeyCode = Keys.Up Then
                If ListBox1.SelectedIndex > 0 Then
                    TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex - 1).ToString
                End If
            End If
        End Sub
    
    
    End Class
    Last edited by Delaney; Jun 28th, 2020 at 05:51 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: Why can not select all text in textbox after focus to textbox ?

    Quote Originally Posted by Delaney View Post
    By the way , there were a few other errors :

    the index start at 0, not 1
    when you press the down key the listbox already go to next element so when you do that
    Code:
     ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1
    you go to the second next element..

    the next element is selected after the end of the keydown event so you need
    Code:
    TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex + 1).ToString
    instead of
    Code:
    TextBox1.Text = ListBox1.SelectedItem.ToString
    here is a correction

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            ListBox1.Items.Add("Times New Roman")
            ListBox1.Items.Add("Arial")
            ListBox1.Items.Add("Arial Narrow")
            ListBox1.Items.Add("Monotype corsiva")
            ListBox1.Items.Add("Verdana")
            ListBox1.Items.Add("Andelip")
            ListBox1.Items.Add("Obit BT")
            ListBox1.Items.Add("LCD")
            ListBox1.Items.Add("Western")
            ListBox1.Items.Add("Roman")
    
            ListBox1.SelectedIndex = 0
            TextBox1.Text = ListBox1.SelectedItem.ToString
    
        End Sub
    
        Private Sub listbox_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
            If e.KeyCode = Keys.Down Then
                If ListBox1.SelectedIndex < ListBox1.Items.Count Then
                    TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex + 1).ToString
                End If
            End If
    
            If e.KeyCode = Keys.Up Then
                If ListBox1.SelectedIndex > 0 Then
                    TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex - 1).ToString
                End If
            End If
        End Sub
    
    
    End Class
    And Where is the code that for Select All Text in TextBox ?

    This is not what I want. I want same as FontDialogBox. when in TextBox, keydown or keyup Move next or prev item in ListBox and TextBox fill ListBox item (toString) and Select All in TextBox.

    Where is this part in your Code ?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Why can not select all text in textbox after focus to textbox ?

    Code:
    Private Sub listbox_KeyDown(sender As Object, e As KeyEventArgs) Handles ListBox1.KeyDown
        If e.KeyCode = Keys.Down Then
            If ListBox1.SelectedIndex < ListBox1.Items.Count Then
                TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex + 1).ToString
                TextBox1.SelectAll()
            End If
        End If
    
        If e.KeyCode = Keys.Up Then
            If ListBox1.SelectedIndex > 0 Then
                TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex - 1).ToString
                TextBox1.SelectAll()
            End If
        End If
    End Sub

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Why can not select all text in textbox after focus to textbox ?

    He wants to Up and Down arrow in the Textbox.

    Paul's code doesn't work because of Event order, I believe.
    A slight change to Paul's code to Handle the event for the Textbox, and to delay the call to SelectAll() until pending events have been processed.
    Code:
      Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Down Then
          If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
            ListBox1.SelectedIndex += 1
            TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex).ToString
            Me.BeginInvoke(Sub() TextBox1.SelectAll())
          End If
        End If
    
        If e.KeyCode = Keys.Up Then
          If ListBox1.SelectedIndex > 0 Then
            ListBox1.SelectedIndex -= 1
            TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex).ToString
            Me.BeginInvoke(Sub() TextBox1.SelectAll())
          End If
        End If
      End Sub
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    Re: Why can not select all text in textbox after focus to textbox ?

    Quote Originally Posted by passel View Post
    He wants to Up and Down arrow in the Textbox.

    Paul's code doesn't work because of Event order, I believe.
    A slight change to Paul's code to Handle the event for the Textbox, and to delay the call to SelectAll() until pending events have been processed.
    Code:
      Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Down Then
          If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
            ListBox1.SelectedIndex += 1
            TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex).ToString
            Me.BeginInvoke(Sub() TextBox1.SelectAll())
          End If
        End If
    
        If e.KeyCode = Keys.Up Then
          If ListBox1.SelectedIndex > 0 Then
            ListBox1.SelectedIndex -= 1
            TextBox1.Text = ListBox1.Items(ListBox1.SelectedIndex).ToString
            Me.BeginInvoke(Sub() TextBox1.SelectAll())
          End If
        End If
      End Sub

    YES.... ABSOLUTELY YESSSS...

    THANK YOU soo much PASSEL. Only you understook what I want to do. And 100% CORRECT solution for me.

    GOD BLESS YOU.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Why can not select all text in textbox after focus to textbox ?

    @passel - you are the chosen one

Tags for this Thread

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