简体   繁体   中英

How to filter a Combo box matching string pattern c#

my Combo box contains values like DBaaaaa, DBbbbbb, Dbccccc, FBaaaaa, FBcccc which is coming from database. i want when user select a value from the combo box it should match the Prefix("DB","FB") and length of the selected value. and it must filter the combo box according to the matching pattern.

Regex filter = new Regex("^[a-zA-z][a-zA-z][a-zA-Z0-9]*");

for example if user selected DBaaaaa. now combo box should contain all values starting From "DB" and its matching length. where like DBbbbbb, Dbccccc.

if i simply say its kind of filtering data if there are thousands of records in Database.

Because of low reputation I can't add a comment, so I have to comment as answer. Why dont you create two combo boxes, and fill out the second one only if field from first one was selected. So then you can do a simple if else statement in your code.

Assuming you have the full list of strings somewhere you could handle this in the ComboBox SelectedIndexChanged/SelectedValueChanged event

Something like:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var comboBox = sender as ComboBox;
    if (comboBox.SelectedItem != null)
    {
        string selectedItem = comboBox.SelectedItem.ToString();
        comboBox.Items = myDataSource.Where(x => x.StartsWith(selectedItem.Substring(0, 2))
                                              && x.Length.Equals(selectedItem.Length));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM