Results 1 to 17 of 17

Thread: [RESOLVED] Simple Combobox Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    10

    Resolved [RESOLVED] Simple Combobox Question

    I want to be able to enter (type in) data into a combobox as well as picking from the list. In earlier VB there was AfterUpdate event so that I could enter in the data I wanted. Now I have tried Keypress, Enter, and others and they all want just one character or number. If I try to enter a two-digit number it doesn't wait.
    Thanks in advance.

  2. #2
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    What about LostFocus event???

    I often use that to validate text entered in one combobox.

  3. #3
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: Simple Combobox Question

    you mean like this>

    Code:
       
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ComboBox1.Items.Add(ComboBox1.Text)
        End Sub

  4. #4
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    I mean

    Code:
        
    Private Sub ComboBox1_LostFocus(sender As Object, e As  System.EventArgs) Handles ComboBox1.LostFocus
            ComboBox1.Items.Add(ComboBox1.Text)
    End Sub
    You should check if sth has to be added of course.

  5. #5
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: Simple Combobox Question

    and you have to clear like this
    Code:
    Combobox1.text = ""
    otherwise it keeps adding after clicking on it and clicking on something else.

  6. #6
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    Quote Originally Posted by ikdekker View Post
    and you have to clear like this
    Code:
    Combobox1.text = ""
    otherwise it keeps adding after clicking on it and clicking on something else.
    Well it depends on what you want to achieve.

    You may check if the text has changed with the TextChanged event or even check if the text enterred already exists in list of the combobox.

    What exactly do you want to do?

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    10

    Re: Simple Combobox Question

    Thank-you both for your suggestions! I will explain further. I simply want to do what the combo box was invented for: allow the user to type in a non-listed option or answer.
    The lost-focus option does work, but I don't like the idea that I have to click somewhere else to get my input sent into my program. That makes it sort of a negative action to get something done.
    I find it ironic that MS lists over 230 methods with this object, but none that explicitly does what the object was originally intended for.
    If you have any other thoughts I would be glad to try them
    Thanks again
    Rich

  8. #8
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    How can your app know when did the user finish typing?

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    10

    Re: Simple Combobox Question

    when the user presses the Enter key

  10. #10
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    So you can check if enter was pressed with ComboBox KeyPress event and then remove focus from combo.

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    10

    Re: Simple Combobox Question

    You are correct. I think that means I must check what has been entered with every keypress. So if I want to enter the number 20, my routine would use the keypress event to first see the "2" and not lose focus, then see the "0" and not lose focus, then see the "Enter" , verify the input, then lose focus and go do what it needs to do.
    Is this what you suggest? Or is there a more direct method?
    I had hoped there would be a more direct implementation than this, given the 20 years of software development history.

  12. #12
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    You should just check if Enter was pressed with combo active.Then with LostFocus you can do whatever you want.

    You can check if Enter was pressed in Combo with this code too

    Code:
        
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
        If (Me.ActiveControl Is ComboBox1) AndAlso (keyData = Keys.[Return]) Then
            MsgBox("Combo Enter")
            Return True
        Else
            Return MyBase.ProcessCmdKey(msg, keyData)
        End If
    End Function

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    10

    Re: Simple Combobox Question

    ευχαριστήριο !
    I will give it a try

  14. #14
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    Quote Originally Posted by profray View Post
    ευχαριστήριο !
    I will give it a try


    I hope it works

  15. #15
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    Re: Simple Combobox Question

    You don't need the lose focus at all just use this code:
    Code:
    Private Sub ComboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
                ComboBox1.Items.Add(ComboBox1.Text)
            End If
        End Sub

  16. #16
    Addicted Member
    Join Date
    Jan 2012
    Location
    Athens, Greece
    Posts
    143

    Re: Simple Combobox Question

    Quote Originally Posted by ikdekker View Post
    You don't need the lose focus at all just use this code:
    Code:
    Private Sub ComboBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
                ComboBox1.Items.Add(ComboBox1.Text)
            End If
        End Sub
    Yeap, that's true...it's just like lose focus though

  17. #17

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    10

    Re: Simple Combobox Question

    Thanks to you both...lets call this resolved
    Rich

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