Solved

combobox help

Posted on 2009-04-01
10
414 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Device Location 4 29
Video Player 11 23
Code works but it's slow 28 70
Latest thoughts on C# utilizing Mono on Linux vs .NET on Windows? 5 19
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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