Solved

combobox help

Posted on 2009-04-01
10
418 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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
 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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