简体   繁体   English

如何过滤与字符串模式匹配的组合框C#

[英]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. 我的组合框包含诸如DBaaaaa,DBbbbbb,Dbccccc,FBaaaaa,FBcccc之类的值,这些值来自数据库。 i want when user select a value from the combo box it should match the Prefix("DB","FB") and length of the selected value. 我想当用户从组合框中选择一个值时,它应该匹配Prefix(“ DB”,“ FB”)和所选值的长度。 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. 例如,如果用户选择了DBaaaaa。 now combo box should contain all values starting From "DB" and its matching length. 现在,组合框应包含从“ DB”开始的所有值及其匹配的长度。 where like DBbbbbb, Dbccccc. 像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. 因此,您可以在代码中执行一个简单的if语句。

Assuming you have the full list of strings somewhere you could handle this in the ComboBox SelectedIndexChanged/SelectedValueChanged event 假设您具有完整的字符串列表,则可以在ComboBox SelectedIndexChanged/SelectedValueChanged事件中处理该字符串

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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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