|
-
Jun 7th, 2007, 08:03 AM
#1
Thread Starter
Addicted Member
[2005] combobox question
Hi All,
I hope you are able to help me with this.
When you type text into a combo, I want to be able to hit "enter" and get a message box with the text I just entered into the combo box. Below is the code I have currently, but I can not seem to get it to work correctly.
Code:
Private Sub cbDisplay_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbDisplay.KeyDown
If cbDisplay.SelectedItem = Nothing Then
MsgBox(cbDisplay.Text)
end if
End Sub
-
Jun 7th, 2007, 08:07 AM
#2
Re: [2005] combobox question
2 Code:
Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter And Me.ComboBox1.Text <> String.Empty Then
MessageBox.Show(Me.ComboBox1.Text)
End If
End Sub
-
Jun 7th, 2007, 08:08 AM
#3
Fanatic Member
Re: [2005] combobox question
vb Code:
private sub comboBox1_KeyPress(sender as object, e as KeyEventArgs)
if e.KeyCode = Keys.Enter then messageBox.show(combobox1.text)
end sub
-
Jun 7th, 2007, 08:11 AM
#4
Frenzied Member
Re: [2005] combobox question
Code:
Private Sub cbDisplay_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cbDisplay.KeyDown
If e.KeyCode = Keys.Enter Then
MsgBox(cbDisplay.Text)
End If
End Sub
-
Jun 7th, 2007, 08:16 AM
#5
Thread Starter
Addicted Member
Re: [2005] combobox question
talkro and robertx,
When I use your code, I get a blank message box.
-
Jun 7th, 2007, 08:19 AM
#6
Frenzied Member
Re: [2005] combobox question
That's because there is no text in the combo box. Use stimbo's code which tests for an empty string and does not display a message box if there is no text displayed.
-
Jun 7th, 2007, 08:24 AM
#7
Thread Starter
Addicted Member
Re: [2005] combobox question
robertx,
stimbo's code returns nothing even when I have text in the combo box
-
Jun 7th, 2007, 08:30 AM
#8
Re: [2005] combobox question
Charming,
You are simply doing something wrong then.
I noticed in your code you have a keypress address but a key down handle which could be causing a problem.
Re-create the subroutine and try again. Everybody's code is correct, mine just had that extra check of not showing the messagebox if the combobox was empty.
-
Jun 7th, 2007, 08:50 AM
#9
Thread Starter
Addicted Member
Re: [2005] combobox question
stimbo,
My dropdownstyle is set to "simple". This is the monkey throwing the wrench. Can you think of a work around for this?
Thank you for you help
-
Jun 7th, 2007, 08:58 AM
#10
Re: [2005] combobox question
I would imagine that the event still fires no matter what the dropdown style is.
Although I seem to remember having a problem with events firing twice depending on menu style. If that's the case then you could try putting the code in the Key Up event instead.
-
Jun 7th, 2007, 09:00 AM
#11
Thread Starter
Addicted Member
Re: [2005] combobox question
the Key Up event does not work either. When I press "Enter", the all the text is cleared from the box.
-
Jun 7th, 2007, 09:01 AM
#12
Re: [2005] combobox question
Sigh. Could you show us exactly what code you have now.
-
Jun 7th, 2007, 09:05 AM
#13
Thread Starter
Addicted Member
Re: [2005] combobox question
Sure,
The below code works if the combo box is set to "DropDown". If it is set to "Simple" (the style that I need) then it does not work.
Code:
Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter And Me.ComboBox1.Text <> String.Empty Then
MessageBox.Show(Me.ComboBox1.Text)
End If
End Sub
-
Jun 7th, 2007, 09:13 AM
#14
Re: [2005] combobox question
It works for me when set to simple. Although it does fire twice as I thought it might. One workaround for that would be put it in the keyUp event as I said although you can't get it to work at all which is very strange.
I'm out of ideas. Anyone else have a problem with it firing the Dropdown style is set to Simple? Could it be a service pack issue? (Clutching at straws now)
-
Jun 7th, 2007, 11:22 AM
#15
Thread Starter
Addicted Member
Re: [2005] combobox question
bah, I am still trying to get this thing to work. Thank you for your help though stimbo, much appreciated.
Does anyone have any ideas about this?
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
|