Solved

C# combobox drop down

Posted on 2004-08-03
8
691 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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