Solved

C# combobox drop down

Posted on 2004-08-03
8
686 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WKHTMLTOPDF - --disable-smart-shrinking not working 10 37
Stream.BeginRead and Stream.EndRead in .NET Core 5 37
C# Json POSt to Rest API 4 37
C# HTTP GET method sample code 3 41
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

19 Experts available now in Live!

Get 1:1 Help Now