|
-
Jun 28th, 2020, 05:13 PM
#1
Thread Starter
Lively Member
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
-
Jun 28th, 2020, 05:21 PM
#2
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)
-
Jun 28th, 2020, 05:37 PM
#3
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)
-
Jun 28th, 2020, 06:07 PM
#4
Thread Starter
Lively Member
Re: Why can not select all text in textbox after focus to textbox ?
 Originally Posted by Delaney
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 ?
-
Jun 28th, 2020, 06:16 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 28th, 2020, 07:11 PM
#6
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
-
Jun 29th, 2020, 03:14 AM
#7
Thread Starter
Lively Member
Re: Why can not select all text in textbox after focus to textbox ?
 Originally Posted by passel
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.
-
Jun 29th, 2020, 11:48 AM
#8
Re: Why can not select all text in textbox after focus to textbox ?
@passel - you are the chosen one
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|