Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 如何在VC++中向窗体添加多个标签?_Winforms_Visual Studio 2010 - Fatal编程技术网

Winforms 如何在VC++中向窗体添加多个标签?

Winforms 如何在VC++中向窗体添加多个标签?,winforms,visual-studio-2010,Winforms,Visual Studio 2010,我需要帮助找出可以用来向Windows窗体添加自定义数量标签的代码。我正在使用默认的Windows窗体应用程序项目进行测试。如果我使用一个对象数组,并在特定的位置添加循环来迭代每个声明,那么我就知道我的代码可能会起作用 这是我到目前为止所拥有的,我不确定如何让程序识别smartLabel阵列,请帮助 #pragma once namespace gui { using namespace System; using namespace System::ComponentMode

我需要帮助找出可以用来向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
    };
}
我自己想出来的

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