我在Microsoft Visual Studio中创建了空C ++ / CLI表单。
我想要做的是将汽车品牌保留在一个阵列中,将车型保留在其他阵列中。 然后我将绘制一个品牌,将其贴上标签,一个来自该品牌的车型和另外三个来自其他品牌的车型。
在我的表单中,我想创建2个数组:
String brands[7]={"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};
String models[7][5]={{"Benz","Vito","AMG","Klasa A","Klasa E"},
{"Astra","Corsa","Insignia","Zafira","Mokka"},
{"Avensis","Corolla","Yaris","Auris","RAV4"},
{"126p","Panda","Punto","500","Tipo"},
{"A4","A6","Q7","R8","A7"},
{"Megane","Captur","Scenic","Kadjar","Espace"},
{"Golf","Passat","Tiguan","Beetle","Touran"}};
我无法将它放在我的MyForm.h文件中,我收到E2022错误。 你建议我用什么简单的解决方案?
#pragma once
namespace Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Podsumowanie informacji o MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: W tym miejscu dodaj kod konstruktora
//
}
protected:
/// <summary>
/// Wyczyść wszystkie używane zasoby.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Label^ label1;
String marki[7]={"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};
String modele[7][5]={{"Benz","Vito","AMG","Klasa A","Klasa E"},
{"Astra","Corsa","Insignia","Zafira","Mokka"},
{"Avensis","Corolla","Yaris","Auris","RAV4"},
{"126p","Panda","Punto","500","Tipo"},
{"A4","A6","Q7","R8","A7"},
{"Megane","Captur","Scenic","Kadjar","Espace"},
{"Golf","Passat","Tiguan","Beetle","Touran"}};
private:
/// <summary>
/// Wymagana zmienna projektanta.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Wymagana metoda obsługi projektanta — nie należy modyfikować
/// zawartość tej metody z edytorem kodu.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 108);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(147, 23);
this->button1->TabIndex = 1;
this->button1->UseVisualStyleBackColor = true;
//
// button2
//
this->button2->Location = System::Drawing::Point(178, 108);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(146, 23);
this->button2->TabIndex = 1;
this->button2->UseVisualStyleBackColor = true;
//
// button3
//
this->button3->Location = System::Drawing::Point(12, 183);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(147, 23);
this->button3->TabIndex = 1;
this->button3->UseVisualStyleBackColor = true;
//
// button4
//
this->button4->Location = System::Drawing::Point(178, 183);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(146, 23);
this->button4->TabIndex = 1;
this->button4->UseVisualStyleBackColor = true;
//
// button5
//
this->button5->Location = System::Drawing::Point(178, 250);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(146, 23);
this->button5->TabIndex = 1;
this->button5->Text = L"Nastepne";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &MyForm::button5_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(140, 28);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(0, 13);
this->label1->TabIndex = 2;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(361, 306);
this->Controls->Add(this->label1);
this->Controls->Add(this->button5);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"MyForm";
this->Text = L"QUIZ";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
}
};
}
答案 0 :(得分:0)
以下是这样做的方法:
首先在受保护的部分声明您的数组(就像您在那里一样)。
array<String^>^ marki;
array<String^,2>^ modele; //Note the declration 2 next to String^. It is rank of the array. 2 for two dimensional.
在构造函数(MyForm()
)中初始化数组,如下所示。
//SINGLE DIMENSION ARRAY
marki = gcnew array<String^> {"Mercedes","Opel","Toyota","Fiat","Audi","Renault","Volkswagen"};
//MULTI DIMENSIONAL ARRAY
modele = gcnew array<String^,2> {{"Benz","Vito","AMG","Klasa A","Klasa E"},
{"Astra","Corsa","Insignia","Zafira","Mokka"},
{"Avensis","Corolla","Yaris","Auris","RAV4"},
{"126p","Panda","Punto","500","Tipo"},
{"A4","A6","Q7","R8","A7"},
{"Megane","Captur","Scenic","Kadjar","Espace"},
{"Golf","Passat","Tiguan","Beetle","Touran"}};
}
测试:
String^ test = modele[2,4]; //RAV4