如何在VC ++中扩展列表框

时间:2012-04-22 04:38:08

标签: visual-studio-2010 c++-cli

我尝试通过添加几个函数来扩展列表框。我收到错误(错误C2144:语法错误:'Extended_ListBox'应该以':'开头)。有人可以教我如何解决它吗?我去了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 个答案:

答案 0 :(得分:1)

在C ++ / CLI中,您指定访问修饰符(公共,私有等)的方式与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: