2012-04-22 85 views
2

我試圖通過添加幾個函數來擴展列表框。我收到一個錯誤(錯誤C2144:語法錯誤:'Extended_ListBox'應該以':'開頭)。有誰會請教我如何解決它?我去了VC++所說的錯誤,但我不知道爲什麼構造函數有錯誤。如何在VC++中擴展列表框

using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 
using namespace System::Collections; 
using namespace System::Collections::Generic; 

#include <stdio.h> 
#include <stdlib.h> 
#using <mscorlib.dll> 


public ref class Extended_ListBox: public ListBox{ 

    public Extended_ListBox(array<String ^>^textLineArray, int counter){ 
     textLineArray_store = gcnew array<String ^>(counter); 
     for (int i=0; i<counter; i++){ 
      this->Items->Add(textLineArray[i]); 
      textLineArray_store[i] = textLineArray[i]; 
     } 
     this->FormattingEnabled = true; 
     this->Size = System::Drawing::Size(380, 225); 
     this->TabIndex = 0; 
     this->SelectedIndexChanged += gcnew System::EventHandler(this, &Extended_ListBox::listBox1_SelectedIndexChanged); 
    } 
    public Extended_ListBox(){ 
     this->FormattingEnabled = true; 
     this->Size = System::Drawing::Size(380, 225); 
     this->TabIndex = 0; 
     this->SelectedIndexChanged += gcnew System::EventHandler(this, &Extended_ListBox::listBox1_SelectedIndexChanged); 
    } 
    private: System::Void listBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) { 
       int index=this->SelectedIndex; 
       tempstring = textLineArray_store[index]; 
     } 

private: array<String ^>^textLineArray_store; 
private: String ^tempstring; 
public: String ^GetSelectedString(){ 
      return tempstring; 
     } 
public: void ListBox_Update(array <String ^>^textLineArray, int counter){ 
     textLineArray_store = gcnew array<String ^>(counter); 
     for (int i=0; i<counter; i++){ 
      this->Items->Add(textLineArray[i]); 
      textLineArray_store[i] = textLineArray[i]; 
     } 
     } 
}; 

回答

1

在C++/CLI中,指定訪問修飾符(public,private等)的方式與在C#或Java中的方式不同。

相反,你只寫一行(注意冒號,這是必需的):

public: 

和所有以下成員是公開的。因此,在構造函數之前插入該行,並在構造函數之前刪除關鍵字public。這樣的:

public ref class Extended_ListBox: public ListBox{ 
public: 
    Extended_ListBox(array<String ^>^textLineArray, int counter){ 
     // constructor code 
    } 

    Extended_ListBox(){ 
     // default constructor code 
    } 

    // other public members 
    // ... 

private: 
    // private members 
    // ... 
} 

類似於下面的構造函數中的成員當前的例子,只是你沒有明確地重申public:private:如果下一個成員具有相同的可見性。