如何在VC ++中向表单添加多个标签?

时间:2011-08-24 15:47:12

标签: winforms visual-studio-2010

我需要帮助来找出可用于向Windows窗体添加自定义标签数量的代码。我正在使用默认的Windows窗体应用程序项目进行测试。我找出代码的方法可能有用,如果我使用一个对象数组并在特定的地方添加循环来迭代每个声明。

这是我到目前为止所做的,我不确定如何让程序识别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
    };
}

1 个答案:

答案 0 :(得分: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
    };
}