Solved

combobox help

Posted on 2009-04-01
10
406 Views
Last Modified: 2013-11-08
Using:
Visual Studio 2008
Windows Forms (not ASP)
C#
.NET 3.5

I have a few comboboxes in my application. They are not databound. All of the items are loaded in manually. I do not want the user to be able to type in any text that does not exist in my list of items so I have the dropdownstyle = dropdownlist. this works fine. My problem is that I want the combobox to allow the user to type in multiple letters in the combobox text area and the box will go directly to that item. This does not work. It won't accept multiple characters.

Example:
If I have a list of cities sorted alphabetically..
.
.
Dallas
Detroit
.
.
Evansville
.
.
.
Say the user is looking for Detroit. In the combobox he types in a "D" and it goes to Dallas, then he types in an "E" and instead of looking at that as "DE" and going to Detroit it looks at it as just "E" and drops to Evansville, disreagrding the previous "D".

I've trid changing the autocomplete property but since I am using the dropdownlist style I am very limited here.

Any ideas?

Thanks.
0
Comment
Question by:kalittaair
  • 5
  • 5
10 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24038980
0
 

Author Comment

by:kalittaair
ID: 24039450
Looking through this code I'm not sure how it will help. My combobox is style dropdownlist to prevent users from typing in text that is not in my list. As such, I cannot change the autocomplete property easily and when I do I get limited results. I tried implementing this from the example:
this.lstColors.AutoCompleteMode = ((System.Windows.Forms.AutoCompleteMode)((System.Windows.Forms.AutoCompleteMode.Suggest | System.Windows.Forms.AutoCompleteMode.Append)));
        this.lstColors.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;

And got the compile error:
Only the value AutoCompleteMode.None can be used when DropDownStyle is ComboBoxStyle.DropDownList and AutoCompleteSource is not AutoCompleteSource.ListItems.

Am I missing something?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24039561
i tried the sample as it is and its working as its explained
i typed in some wrong names and it didnt add the value to the combobox

change the dropdownstyle and give it a show
if you dont do it then only one keypress will be taken for a workd and the next key press will start searching for the new word in the combox (from the beginning)
0
 

Author Comment

by:kalittaair
ID: 24040209
Thanks for your response but it is not working.

Firstly you said: "i typed in some wrong names and it didnt add the value to the combobox" - I'm not sure what you mean here. My combobox is not adding any values to itself or anything else. It is designed to simply let the user chose one of the items to be used in calcualtions later in the code.

Secondly:
My combobox is called comboFilterCity.

I changed the dropdownstyle of comboFilterCity from dropdownlist to dropdown and added from your sample code:
comboFilterCity.AutoCompleteMode = ((System.Windows.Forms.AutoCompleteMode)((System.Windows.Forms.AutoCompleteMode.Suggest | System.Windows.Forms.AutoCompleteMode.Append)));
comboFilterCity.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;

Now the autocomplete works fine but I can type in values that do not exist in my list. In using my original example, if I type in "DE" it goes to Detroit. However, if I then type an "X" it shows "DEX" in my combo's text box which does not match any item in my list. This is why I have it set to style dropdownlist. I don't want them to do this.

Should I be using additional code from the example? The rest of the code just looks like its creating the combobox, loading it, and setting some display properties.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24040254
but when you type in something which does not match it will show to the user no posibilities of match and so the user will have to correct his/her errors
i think this is the way autocomplete works where if something which does not match is typed nothing is displayed in the shortlist for combo
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:kalittaair
ID: 24040395
Yes, you are correct. It does not show the user any matching items. However, this does not stop the user from typing it and then hitting the SUBMIT button. The code in the SUBMIT button then accepts the text that exists in the box, regardless of whether or not it matches one of the items. It would be nice if the combobox would restrict users from typing in items that does not exist but still allow them to use the autocomplete that accepts multiple characters. Is there anyway to configure the combobox to do this? If not, it looks as though I'll have to loop through all the items in the compbobox upon hitting SUBMIT to do my own error checking. I was hoping not to have to do this.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24040456
no you dont have to loop through the item list
combobox has findbytext and findbyvalue functions which you can use to see if the text exists in the list or not
http://www.obout.com/combobox/aspnet_options_find.aspx
0
 

Author Comment

by:kalittaair
ID: 24040509
I am not using ASP.NET. I am using Windows forms. Your example is ASP.NET. Does that matter?
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 24040546
0
 

Author Comment

by:kalittaair
ID: 24056671
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now