【问题标题】:Search from selected values of comboboxes windows forms C#从组合框的选定值中搜索窗口窗体 C#
【发布时间】:2014-09-10 21:53:27
【问题描述】:

我在一个表单中有 10 个组合框,我需要做的是,如果选择了来自任何组合框的项目(可能是 1 个或多个组合框),则搜索字符串数组和值是否重合从组合框中选择,但排除尚未选择或更改的组合框,我尝试使用 if 但我认为它不够或有效的方法,我只是想知道是否有任何方法可以做到这一点。

【问题讨论】:

  • “如果字符串数组中存在巧合”是什么意思?此外,如果您发布您尝试过的代码,也会有很大帮助。

标签: c# search combobox


【解决方案1】:

您应该能够以一种可以确定当前ComboBox 是否与其他人不同的方式存储有关您班级的信息。我制作了一个小示例,它使用SelectedIndexChanged 上的事件处理程序跟踪对ComboBox 所做的更改,并使用LINQ 过滤并排除当前的ComboBox

List<ComboBox> _allComboBoxes;
ComboBox _comboBox1;
ComboBox _comboBox2;
string[] _values;

void InitForm()
{
    // TODO Implement values.
    _values = new string[0];
    _allComboBoxes = new List<ComboBox>();

    _comboBox1 = new ComboBox();
    _allComboBoxes.Add(_comboBox1);
    _comboBox1.SelectedIndexChanged += ComboBoxSelectedIndexChanged;

    _comboBox2 = new ComboBox();
    _allComboBoxes.Add(_comboBox2);
    _comboBox2.SelectedIndexChanged += ComboBoxSelectedIndexChanged;
}

void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
    if (HasCoincidence(sender as ComboBox, _values, _allComboBoxes))
    {
        throw new InvalidOperationException("Coincidence occurred.");
    }
}

bool HasCoincidence(ComboBox comboBox, string[] values, IEnumerable<ComboBox> allComboBoxes)
{
    IEnumerable<ComboBox> excludedComboBoxes = allComboBoxes.Where(c => c != comboBox);

    throw new NotImplementedException("Implement ComboBox to string[] comparison.");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多