How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
Hi all,
Can anyone help with this?
I've searched for suggestions in previous postings but what I found doesn't seem to work for me...
Thanks in advance for your help!
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
Modify the arguments to suit your needs...
Code:
Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Return Then
SelectNextControl(ComboBox1, True, False, False, True)
End If
End Sub
:)
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
Thanks, but unfortunately it doesn't seem to work :(
Any other suggestions?
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
Did you read up on the arguments for it? This is why I mentioned for you to modify it for your needs. ;)
Does your combo box have the name "ComboBox1"? Do you have the forms KeyPreview property set to True?
The code works for me.
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
yep, I did all the changes you mentioned but it still doesn't seem to work :(
Perhaps there's something missing? I just want the user to be able to press Enter instead of Tab and get the info related to the selected item in the ComboBox (this info is output in a RichTextBox). Here is the code I'm experimenting with (the programme is a sort of dictionary which is meant to show in the RichTextBox the selected item in the ComboBox - which is a Middle English word - its pronunciation and its translation into Modern English).
Any help greatly appreciated! :)
Code:
Imports Microsoft.Office.Interop
Imports System.IO
Public Class Form1
Dim k As Integer = 1000
Dim objExcel As Excel.Application
Dim data As Excel.Range
Dim i As Integer = 1
Private Sub OpenExcel()
objExcel = New Excel.Application
End Sub
Private Sub CloseExcel()
objExcel.Workbooks.Close()
objExcel.Quit()
objExcel = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim sheet As New Excel.Worksheet
Dim row As Integer
OpenExcel()
sheet = objExcel.Workbooks.Open _
("C:\Documents and Settings\Cristiano\Documenti\1_Canterbuty_Tales").Worksheets.Item(1)
objExcel.Range("A2:H" & k).Select()
data = objExcel.Selection
For row = 2 To k
cmbEntries.Items.Add(Data(row, 4).value)
Next
End Sub
Private Sub cmbEntries_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmbEntries.SelectedIndexChanged
rtbTrans.Clear()
Dim boldFont As New Font("Arial", 12, FontStyle.Bold)
Dim noBold As New Font("Arial", 8, FontStyle.Regular)
Dim transFont As New Font("Ipa-samd Uclphon1 SILDoulosL", 10, FontStyle.Italic)
rtbTrans.SelectionFont() = boldFont
rtbTrans.AppendText(data(cmbEntries.SelectedIndex + 2, 4).text)
rtbTrans.AppendText(vbCrLf)
rtbTrans.SelectionFont() = transFont
rtbTrans.AppendText(data(cmbEntries.SelectedIndex + 2, 6).text)
rtbTrans.AppendText(vbCrLf)
rtbTrans.SelectionFont() = noBold
rtbTrans.AppendText(data(cmbEntries.SelectedIndex + 2, 8).text)
rtbTrans.AppendText(vbCrLf)
End Sub
Private Sub cmbEntries_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles cmbEntries.KeyDown
If e.KeyCode = Keys.Enter Then
SelectNextControl(cmbEntries, True, False, False, True)
End If
End Sub
End Class
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
Did you set the forms KeyPreview property to True like I mentioned? Is your rich textbox control the next in the tab index order? Is the RTB in a groupbox or panel etc?
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
yep, I set the KeyPreview property to True and the RTB is the next in the TabIndex order. Both the ComboBox and the RichTextBox are directly on the form (I haven't used a GroupBox).
Btw, I also tried:
If e.KeyCode = Keys.Enter Then
SendKeys.Send("{TAB}")
End If
but it doesn't do the trick either.
:confused:
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
Place a breakpoint on the If statement and then run your app. When you press a key down it should fire the event but not go into the if block. Press enter and it should step into the if block and activate the next control. Sendkeys wont work unless the form is focused and active
Re: How do I make the Enter Key behave like the Tab key in a combobox in VB 2005?
I think I've found what causes the problem. It's the AutoCompleteMode for the ComboBox, which I have set to Append. If it is set to None, the Enter key works the same as the Tab key. But I can't understand how I can have it both ways (i.e. Append + Enter working as in the None mode)...