【问题标题】:C++ | How to remove the DataSource from a combobox?C++ |如何从组合框中删除数据源?
【发布时间】:2019-11-21 23:08:08
【问题描述】:

我正在用 c++ 和 CLR 在 Visual Studio 中编写一个 Windows 窗体应用程序。 现在,我遇到了以下问题:

我读入一个文件并将所有重要信息保存在一个向量中。

第二步,我将所有项目转换为 CLR 数组并将此数组设置为 DataSource。

array<System::String^>^ PREDEFINED = gcnew array <System::String^>(OP->CIS_Values[OP->selectedCIS]->PREDEFINED_order.size());
                for (int i = 0; i < OP->CIS_Values[OP->selectedCIS]->PREDEFINED_order.size(); i++) {
                    PREDEFINED[i] = msclr::interop::marshal_as<System::String^>(OP->CIS_Values[OP->selectedCIS]->PREDEFINED_order[i]);
                }
                this->comboBox_header_predefinedtypes->DataSource = PREDEFINED;
                this->comboBox_header_predefinedtypes->SelectedIndex = -1;
                delete PREDEFINED;

设置 DataSource 后,必须可以将其删除。在 C# 中,我可以使用

comboBox.DataSource = null;

但是它在 C++ 中是如何工作的呢?

我尝试了以下几种情况:

comboBox_header_predefinedtypes->DataSource = NULL;
comboBox_header_predefinedtypes->DataSource = 0;
comboBox_header_predefinedtypes->DataSource = -1;

comboBox_header_predefinedtypes->DataBindings->Clear();
comboBox_header_predefinedtypes->Items->Clear();

我在 DataSource 之前和之后使用 DataBindings 进行了尝试,但我总是得到相同的异常:

System.ArgumentException:“复杂的 DataBinding 接受 IList 或 IListSource 作为数据源”

我该如何解决这个问题?

谢谢帮助。

【问题讨论】:

    标签: c++ visual-studio clr windows-forms-designer


    【解决方案1】:

    在 C++ CLR 中你只需要写:

    comboBox_header_predefinedtypes->DataSource = nullptr;
    

    你不需要写

    comboBox_header_predefinedtypes->Items->Clear();
    

    删除 DateSource 后删除项目。

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多