Results 1 to 15 of 15

Thread: [2005] combobox question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    [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

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] combobox question

    2 Code:
    1. Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
    2.     Handles ComboBox1.KeyDown
    3.  
    4.         If e.KeyCode = Keys.Enter And Me.ComboBox1.Text <> String.Empty Then
    5.             MessageBox.Show(Me.ComboBox1.Text)
    6.         End If
    7.  
    8. End Sub
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] combobox question

    vb Code:
    1. private sub comboBox1_KeyPress(sender as object, e as KeyEventArgs)
    2.   if e.KeyCode = Keys.Enter then messageBox.show(combobox1.text)
    3. end sub

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Resolved 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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: [2005] combobox question

    talkro and robertx,

    When I use your code, I get a blank message box.

  6. #6
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    Re: [2005] combobox question

    robertx,

    stimbo's code returns nothing even when I have text in the combo box

  8. #8
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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

  10. #10
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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.

  12. #12
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] combobox question

    Sigh. Could you show us exactly what code you have now.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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

  14. #14
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jun 2006
    Posts
    250

    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
  •  



Click Here to Expand Forum to Full Width