Results 1 to 8 of 8

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

Threaded View

  1. #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)

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