Solved

C# combobox drop down

Posted on 2004-08-03
8
693 Views
Last Modified: 2010-04-15
Hello Expert,
I was am programming a c# windows form and on it i have a comobox populated with Cities. The combo box is populated from a database I set the type of box to dropdown because I want to be able to as the user types in letters for it to narrow down to the city in the combobox and if it is not there I want to be able to add the newly typed city to the database. Is this possible?
Thanks
0
Comment
Question by:jvoros1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 11706132
On ComboBox Leave event, run a query in your database and see if the typed city exist. If it doesnt add it. You know how to add and run queries in the databse right?

-Nauman
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11706148
sure ... make it a combobox (which allows typing) then when they say ok check to see if the value they enterred is in the list, if not insert it ...
0
 

Author Comment

by:jvoros1
ID: 11706513
Is there a way to do it on the fly check as they type in each letter??
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11706530
yeah there is ... you can trap the OnKeyPress event and check your list for matches on the string they have typed so far
0
 
LVL 8

Accepted Solution

by:
rajaloysious earned 500 total points
ID: 11707966
           private void combobox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
            {
                  int nCmbIdx = 0;
                  int nlength = 0;
                  string sprevval = "";
                  ComboBox cmbBox = (ComboBox)sender;

                  if (cmbBox.DroppedDown == false)
                        cmbBox.DroppedDown = true;
                  
                  nlength = cmbBox.SelectionStart;
                  sprevval = cmbBox.Text.Substring(0,nlength);
                  nCmbIdx = cmbBox.FindString(sprevval + e.KeyChar.ToString());

                  //if the item exists
                  if (nCmbIdx >= 0)
                  {
                        e.Handled = true;
                        cmbBox.SelectedIndex = nCmbIdx;
                        cmbBox.SelectedItem = cmbBox.Items[nCmbIdx];
                        cmbBox.SelectionStart = nlength + 1;
                        cmbBox.SelectionLength = cmbBox.Text.Length - sprevval.Length;
                  }
                  else
                  {
                        //item is new one
                        //Have a track and add it on combobox1_Leave
                        Set a flag = true and a varaible to remember the new item
                        bNewItem = true;
                        sNewItem = sprevval + e.KeyChar.ToString()
                  }

            private void combobox1_Leave(object sender, System.EventArgs e)
            {
                  combobox1.Items.Add sNewItem
                  nCmbIdx = cmbBox.FindString(sNewItem);
                  cmbBox.SelectedIndex = nCmbIdx;
            }

Cheers...
0
 

Author Comment

by:jvoros1
ID: 11709278
Thanks I tried on keypress
 cmbCity.SelectedIndex = cmbCity.FindString(e.KeyChar.ToString());
but this only gets first letter is there a way to get them to narrow down so they can type Toro and as they type it narrows it down.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11709297
why not use the .Text property to access the text they have typed in so far ? or you could just maintain a string of the data they have enterred so far.
0
 

Author Comment

by:jvoros1
ID: 11709397
Thanks rajaloysious  I got it working that was great help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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