|
-
Jan 27th, 2012, 12:14 PM
#1
Thread Starter
New Member
[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.
-
Jan 27th, 2012, 05:15 PM
#2
Addicted Member
Re: Simple Combobox Question
What about LostFocus event???
I often use that to validate text entered in one combobox.
-
Jan 27th, 2012, 05:21 PM
#3
Addicted Member
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
-
Jan 28th, 2012, 06:00 AM
#4
Addicted Member
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.
-
Jan 28th, 2012, 06:23 AM
#5
Addicted Member
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.
-
Jan 28th, 2012, 08:25 AM
#6
Addicted Member
Re: Simple Combobox Question
 Originally Posted by ikdekker
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?
-
Jan 28th, 2012, 09:23 AM
#7
Thread Starter
New Member
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
-
Jan 28th, 2012, 09:49 AM
#8
Addicted Member
Re: Simple Combobox Question
How can your app know when did the user finish typing?
-
Jan 28th, 2012, 10:10 AM
#9
Thread Starter
New Member
Re: Simple Combobox Question
when the user presses the Enter key
-
Jan 28th, 2012, 10:13 AM
#10
Addicted Member
Re: Simple Combobox Question
So you can check if enter was pressed with ComboBox KeyPress event and then remove focus from combo.
-
Jan 28th, 2012, 10:19 AM
#11
Thread Starter
New Member
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.
-
Jan 28th, 2012, 10:26 AM
#12
Addicted Member
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
-
Jan 28th, 2012, 10:35 AM
#13
Thread Starter
New Member
Re: Simple Combobox Question
ευχαριστήριο !
I will give it a try
-
Jan 28th, 2012, 10:36 AM
#14
Addicted Member
Re: Simple Combobox Question
 Originally Posted by profray
ευχαριστήριο !
I will give it a try

I hope it works
-
Jan 28th, 2012, 10:37 AM
#15
Addicted Member
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
-
Jan 28th, 2012, 10:41 AM
#16
Addicted Member
Re: Simple Combobox Question
 Originally Posted by ikdekker
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
-
Jan 28th, 2012, 10:49 AM
#17
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|