2011-08-24 107 views
0

我需要幫助才能找出可用於將自定義數量的標籤添加到Windows窗體的代碼。我正在使用默認的Windows Forms應用程序項目進行測試。我找出我的代碼的方式可能會起作用,如果我使用一個對象數組並在特定位置添加循環來迭代每個聲明。如何在VC++中將多個標籤添加到表單中?

這是我到目前爲止,我真的不知道如何使程序識別smartLabel陣列,請幫助。

#pragma once 
namespace gui { 

    using namespace System; 
    using namespace System::ComponentModel; 
    using namespace System::Collections; 
    using namespace System::Windows::Forms; 
    using namespace System::Data; 
    using namespace System::Drawing; 
    public ref class Form1 : public System::Windows::Forms::Form 
    { 
    public: 
     Form1(void) 
     { 
      InitializeComponent(); 
     } 

    protected: 
     ~Form1() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    private: System::Windows::Forms::Label^ label1; 
    private: System::Windows::Forms::Label^ smartLabel[0]; //INSERTED FOR POSSIBLE SOLUTION 
    protected: 
    private: 
     System::ComponentModel::Container ^components; 

#pragma region Windows Form Designer generated code 
     void InitializeComponent(void) 
     { 
      this->label1 = (gcnew System::Windows::Forms::Label()); 
      this->smartLabel[0] = (gcnew System::Windows::Forms::Label());//INSERTED FOR POSSIBLE SOLUTION 
      this->SuspendLayout(); 
      // 
      // label1 
      // 
      this->label1->AutoSize = true; 
      this->label1->Location = System::Drawing::Point(10, 10); 
      this->label1->Name = L"label1"; 
      this->label1->Size = System::Drawing::Size(50, 15); 
      this->label1->TabIndex = 0; 
      this->label1->Text = L"label1"; 

      // 
      // smartLabel[0] 
      // 
      this->smartLabel[0]->AutoSize = true; //INSERTED FOR POSSIBLE SOLUTION 
      this->smartLabel[0]->Location = System::Drawing::Point(30, 10); //INSERTED FOR POSSIBLE SOLUTION 
      this->smartLabel[0]->Name = L"label2"; //INSERTED FOR POSSIBLE SOLUTION 
      this->smartLabel[0]->Size = System::Drawing::Size(50, 15); //INSERTED FOR POSSIBLE SOLUTION 
      this->smartLabel[0]->TabIndex = 0; //INSERTED FOR POSSIBLE SOLUTION 
      this->smartLabel[0]->Text = L"label2"; //INSERTED FOR POSSIBLE SOLUTION 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); //INSERTED FOR POSSIBLE SOLUTION 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; //INSERTED FOR POSSIBLE SOLUTION 
      this->ClientSize = System::Drawing::Size(550, 498); //INSERTED FOR POSSIBLE SOLUTION 


      this->Controls->Add(this->label1); 
      this->Controls->Add(this->smartLabel[0]); //INSERTED FOR POSSIBLE SOLUTION 
      this->Name = L"Form1"; 
      this->Text = L"Form1"; 
      this->ResumeLayout(false); 
      this->PerformLayout(); 

     } 
#pragma endregion 
    }; 
} 

回答

0

自己想出來。

#pragma once 
namespace gui { 

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

    public ref class Form1 : public System::Windows::Forms::Form{ 
     public: 
     Form1(void){ 
      InitializeComponent(); 
      BuildLabels(); 
     } 

     private:array<System::Windows::Forms::Label^>^ labels; 

#pragma region Windows Form Designer generated code 
    void InitializeComponent(void){ 
     this->SuspendLayout(); 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(200, 220); 
     this->Name = L"Form1"; 
     this->Text = L"Form1"; 
     this->ResumeLayout(false); 
    } 
    void BuildLabels(){ 
     array<String^>^ smartLabel = gcnew array<String^> {L"label1",L"label2",L"label3",L"label4", L"label5",L"label6",L"label7",L"label8",L"label9",L"label10"}; 
     labels = gcnew array<System::Windows::Forms::Label^>(10); 
     for (int i = 0; i < labels->Length; i++) 
     { 
      labels[i] = gcnew Label(); 
      labels[i]->AutoSize = true; 
      labels[i]->Location = System::Drawing::Point(10, 20*i+10); 
      labels[i]->Name = smartLabel[i]; 
      labels[i]->Size = System::Drawing::Size(50, 15); 
      labels[i]->TabIndex = 0; 
      labels[i]->Text = smartLabel[i]; 
     } 
     Controls->AddRange(labels); 
    } 
#pragma endregion 
    }; 
} 
相關問題