Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 220323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:55:40+00:00 2026-05-11T18:55:40+00:00

I have a combobox that I don’t want users adding new data too, but

  • 0

I have a combobox that I don’t want users adding new data too, but I also want to let them type in the title of the object they want to select.

Currently I am using this code:

    protected virtual void comboBoxAutoComplete_KeyPress(object sender, KeyPressEventArgs e) {
        if (Char.IsControl(e.KeyChar)) {
            //let it go if it's a control char such as escape, tab, backspace, enter...
            return;
        }
        ComboBox box = ((ComboBox)sender);

        //must get the selected portion only. Otherwise, we append the e.KeyChar to the AutoSuggested value (i.e. we'd never get anywhere)
        string nonSelected = box.Text.Substring(0, box.Text.Length - box.SelectionLength);

        string text = nonSelected + e.KeyChar;
        bool matched = false;
        for (int i = 0; i < box.Items.Count; i++) {
            if (((DataRowView)box.Items[i])[box.DisplayMember].ToString().StartsWith(text, true, null)) {
                //box.SelectedItem = box.Items[i];
                matched = true;
                break;
            }
        }

        //toggle the matched bool because if we set handled to true, it precent's input, and we don't want to prevent
        //input if it's matched.
        e.Handled = !matched;
    }

It works well for any combobox that uses data bound to a database, and is case insensitive. However, if the user inputs something in the wrong case and then tabs out of the combobox the combobox’s selected value is still -1 (or whatever the previous value was). That’s not the behavior I want, I want it to set the value to what is currently the best guess at what the user is tying, i.e. the autocompleted option.

I have tried this, if you see the commented out line in the for loop. That doesn’t work.
It does something like this:
I have the field “Rent” with the value of 53
I type ‘r’
I get the result ‘rRent’
combobox.SelectedValue returns -1

What it currently does:
I have the field “Rent” with the value of 53
I type ‘r’
Autocomplete suggests “rent”
It’s the correct value so I move on and the combobox loses focus
Combobox displays “rent”
combobox.SelectedValue return -1

What I want:
I have the field “Rent” with the value of 53
I type ‘r’
The combobox loses focus, it fills in ‘rent’ (even though it’s not in the correct case [already does this])
combobox.SelectedValue should now return 53

I think setting box.SelectedValue might be better but I can’t figure out how to do that, at least in a high level abstracted way, if I knew how the combobox did it with ValueMemeber and Display member I would duplicate it but I don’t.

Does anyone have any suggestions on how to resolve this bug?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:55:41+00:00Added an answer on May 11, 2026 at 6:55 pm

    It looks like I have no option but to do it in the LeaveFocus event, this handles the problem:

        protected void autocomplete_LeaveFocus(object sender, EventArgs e) {
            ComboBox box = ((ComboBox)sender);
            String selectedValueText = box.Text;
    
            //search and locate the selected value case insensitivly and set it as the selected value
            for (int i = 0; i < box.Items.Count; i++) {
                if (((DataRowView)box.Items[i])[box.DisplayMember].ToString().Equals(selectedValueText, StringComparison.InvariantCultureIgnoreCase)) {
                    box.SelectedItem = box.Items[i];
                    break;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a select-only ComboBox that provides a list of items for
I have a listview with a DataTemplate that has a ComboBox. I want the
I have a ComboBox that I set up like this: this.cmbCustomerJob.DisplayMember = display; this.cmbCustomerJob.AutoCompleteMode
I have a Flex ComboBox that gets populated by a dataprovider all is well...
I have a combobox on my form that is bound to a generic list
I have a radajaxpanel that is populated with the selected item from a combobox
I have a ComboBox that is bound to an ObservableCollection of custom UserControls. Each
I have a ComboBox that uses a DataTemplate . The DataTemplate contains a binding
(I'm using Cairngorm) I have a combobox that displays items form an XMLListCollection that's
WinForms, .NET 3.5 I have a ComboBox that needs to display a growing list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.