允许函数以另一种形式查看列表框

时间:2015-02-04 14:39:19

标签: c++-cli

我的所有代码都在我的.cpp文件中。我有一个功能:

void funct1 (void)    
{
    ...
    if (num_fields) {
        for (ix = 0; ix < num_fields; ix++)
            if (status == OK)
                checkedListBox1->Items->Add(gcnew String(buffer));
    } else
        checkedListBox1->Items->Add("No available extra data fields");
}

但是我的功能无法从我的checkedlistbox1看到Form5.h

如何让我的功能看到这个?

我从我的cpp文件调用我的函数:

System::Void Form5::MainMAFBrowseBtn_Click(System::Object^  sender,
        System::EventArgs^  e) {
    checkedListBox1->Items->Clear();
    System::String^ paf_path2 = textBox1->Text;

    FolderBrowserDialog^ folderBrowserDialog1;
    folderBrowserDialog1 = gcnew System::Windows::Forms::FolderBrowserDialog;
    folderBrowserDialog1->Description = L"Select the directory of your MAF files ";
    folderBrowserDialog1->ShowNewFolderButton = false;      

     // Show the FolderBrowserDialog.
    System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
    if ( result == ::DialogResult::OK )
        paf_path2 = folderBrowserDialog1->SelectedPath;
    textBox1->Text = paf_path2;

    paf_path = (char*)(void*)Marshal::StringToHGlobalAnsi(paf_path2);

    funct1();
}

1 个答案:

答案 0 :(得分:0)

如果functl()不属于班级,则您需要传递checkedListBox1作为参数,如下所示:

void funct1(System::Windows::Forms::CheckedListBox% checkedListBox1)
{
    ...
}

比你的通话功能:

System::Void Form5::MainMAFBrowseBtn_Click(System::Object^  sender, System::EventArgs^  e)
{
    ...
    functl(checkedListBox1);
}
在您的班级Form5

,您可以在其中声明functl

class Form5
{
    ...
private:
    void functl();
};

然后在.cpp文件中,将functl声明为:

void Form5::functl()
{
    /// Now you have direct access to checkedListBox1.
}