ComboBox - Typing vs List select

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Well,
This one is annoying. Given this setup:

VB.NET:
private cmb as ComboBox 
private bind as bindingsource

public sub Form_Shown() handles me.shown
  cmb.datasource = bind
  cmb.AutoCompleteMode = Append
  cmb.AutoCompleteSource = ListItems
  cmb.DropDownStyle = DropDown
  cmb.displaymember = "col1"
  cmb.valuemember = "col2"
end sub

public sub cmb_SelectedValue() handles cmb.selectedValue

end sub

public sub TextChanged() handles cmb.TextChanged

end sub

Let us assume that the binding source is set to a datatable, and the variables (components) are all built with the designer so they handle events and everything ( i just didn't feel like typeing all the params and such)

When the user selects via the arrow keys or mouse an item in the list the TextChanged, SelectedValue, and SelectedIndex events all fire in that order. (I've stepped it to be sure)

However, since the DataSource is set on the combobox AND the combobox style is DropDown i'm noticing a little problem with the currently selected value/item/index etc. The BindingSource and ComboBox Index just stay put, they don't shift when the user types in the combobox. Now a static list in the Items would normally cause the SelectedIndex to revert to -1, or at least it used to in other languages (Delphi, VB6, VBA), so how can I tell the system:

"OKay This item the user is typing is Not in the List, so blank all other bound fields, and wait for the user to exit the control."

When they exit the control continue with
"The User has exited is and the item is still not in the list, so prepare to add a new item to the datasource."

Frankily it seems a bit limited that they no longer have teh "Item Not In List" event like what existed in VB6/A, which was so helpful in trapping when a item was typed in that is not on the item list. and When using a BindingSource/DataTable as the DataSource for the List, the ComboBox.Items are DataRowViews, not Strings (or whatever value the ValueMember column would be), so I can't even do, ComboBox.Items.IndexOf(X) = -1.

Any suggestions?
 
You obviously haven't read the documentation for the ComboBox, or not properly at least, or you'd know about the GetItemText, FindString and FindStringExact methods. If you want to know anything about any class, its documentation is the first place you should look... EVERY time. So, you can, fairly easily, handle the TextChanged event and call the FindStringExact method to see whether the current Text matches an item.
 
You obviously haven't read the documentation for the ComboBox, or not properly at least, or you'd know about the GetItemText, FindString and FindStringExact methods. If you want to know anything about any class, its documentation is the first place you should look... EVERY time.
I always do, but sometimes one just can't find what they are looking for. I never saw those methods, but then again, "Seek and ye shall find" is more of a depiction of asking the right questions. I obviously asked the wrong ones in my search, primarily because I was looking for an event or property not an internal method. *sigh* nobody's perfect.
So, you can, fairly easily, handle the TextChanged event and call the FindStringExact method to see whether the current Text matches an item.
Yes I can, and thank you very much for pointing those methods out to me, as i've already seen half a dozen other uses for them in other elements of code, and the original problem has been resolved quite amicably.
:)
Thanks
 
I always do, but sometimes one just can't find what they are looking for. I never saw those methods, but then again, "Seek and ye shall find" is more of a depiction of asking the right questions. I obviously asked the wrong ones in my search, primarily because I was looking for an event or property not an internal method. *sigh* nobody's perfect.

Yes I can, and thank you very much for pointing those methods out to me, as i've already seen half a dozen other uses for them in other elements of code, and the original problem has been resolved quite amicably.
:)
Thanks
Why search at all? Open your local MSDN Library to the index, which you can do by selecting Help -> Index in VS, and then type combobox into the Look For box. Click on the ComboBox Members item and read it.

This is what I'm talking about when I say that you, and many other people, haven't read the documentation. EVERY type in the Framework has a topic dedicated to it and another dedicated to its member listing. If you haven't read those then you haven't read the documentation properly. Searching should NEVER be your first choice if you already know what class you're using.
 
As a side note: you may struggle to get a combo to behave both as a Navigator (when a list item is picked, it moves a bindingsource position) and as an Editor (when an item is picked, it alters an underlying model)
 
I'm saying I do that every time, F1 to the type help. Scan the members, reading and searching, and then I usually jump here or to msdn and do a search if I haven't found it, just on some of these situations, this to be precise, I thought "Well, it was an Event in VB6...") so i looked in events and properties, and only briefly scanned the members. When i searched, my thought process of typing vs list select was not very clear. After 30 minutes I had to move on and fix something else so i don't get behind.
As a side note: you may struggle to get a combo to behave both as a Navigator (when a list item is picked, it moves a bindingsource position) and as an Editor (when an item is picked, it alters an underlying model)
Ahh, not if you set the Binding Source mode to NEVER. I have two Bind sources on the form, one is the navigator, and is the datasource of the combobox. the second is the databind and it is set to the ComboBox and TextBox on the form, but both are set to DataSourceUpdateMode.Never.
on the Navigator Bind Source Position Update, it updates the databind position as well. Thanks to the point out on the FindStringExtact(), during the textchanged I could remove the databindings from both controls on a -1 (not found), and could reinstate the bindings if currently not bound and the findstringexact > -1.
After the MyBase.ShowDialog() returns, I have the dialog result and can compare the ComboBox.SelectValue to Nothing, if it is nothing that means it's a new entry, otherwise, i do a MyTextBox.DataBindings(0).WriteValue(). So Far the system works perfectly.

And hey, i understand a lot of people ask without looking first, but I never have. Sometimes I just don't see things, especially when it still is very difficult not to think of certain vb6 things that I can't seem to find in Vb.Net. *shrug* As well, a lot of the questions I've posted have been due to the F1 leading me to: "Information Not Found" in the contextual help.
Thanks
 
Back
Top