Solved

C# combobox drop down

Posted on 2004-08-03
8
685 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
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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

23 Experts available now in Live!

Get 1:1 Help Now