开发者

How do I add multiple labels to a form in VC++?

I need help to figure out code that can be used to add a custom number of labels to a Windows Form. I am using a default Windows Forms Application project for testing purposes. The way I figure out my code may work is if I employ an array of objects and add loops to in particular places to iterate each declaration.

Here is what I have so far, I am not really sure how to make the program recognize the smartLabel array, please help.

#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 POSSI开发者_如何学编程BLE 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
    };
}


Figured out on my own.

#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
    };
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜