Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# combobox drop down

Posted on 2004-08-03
8
Medium Priority
?
695 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

721 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