zhu_zy123
2020-07-30 18:52
采纳率: 87.5%
浏览 524

windowsForm开发 如何取tableLayoutPanel中各个单元格上面panel中的控件值

1.背景:
动态创建了一张tableLayoutPanel表,并且在表的单元格里加入了panel控件,然后在各个panel上面加入N个其他控件(例如checkBox控件)
2.问题:
①如何取得tableLayoutPanel表格中各个panel上面的checkBox是否打勾(或者textBox控件中值)?(例如:取表格的(1,1)中panel上面的textBox的值)
②在表格的每个单元格内都有同样的控件,如何取它们的值,参考下图
图片说明

3.开发语言:
C++/CLR
4.希望:
求大神指教方法,或者具体功能的代码,因为本人是新人,求耐心仔细解答(C++/CLR的代码)
6.简略代码:
下面的函数在InitializeComponent()中执行
void add(Col_Num,Row_Num)
{
省略
int indexNum = 0;

for ( int i = 0; i < Col_Num; i++ )
{
for ( int j = 0; j < Row_Num; j++ )
{
省略
this->comboBox_display = gcnew ComboBox();

    //
    //comboBox_display
    //
    this->comboBox_display->AddRange(strItem);  //strItem里面为"苹果","梨"
    this->comboBox_display->name = "comboBox_" + indexNum;
    this->comboBox_display->Text = "";
    this->comboBox_display->SelectedIndexChanged += gcnew System::EventHandler(this,&Form,CcomboBox_display_SelectedIndexChanged); //创建触发事件
    ......
    //还有其他几个属性

    省略
            this->panel_display->add(this->comboBox_display);  //把comboBox控件添加到panel上
            this->panel_display->add(this->checkbox_display);  //把checkBox控件添加到panel上
            ........等等
    this->tableLayoutPanel_display->add(this->panel_display,i,j);  //把panel控件添加到对应的表格里面

    省略

    indexNum++;
}

}
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • threenewbee 2020-07-30 21:20
    最佳回答
    #pragma once
    
    namespace Q1092727 {
    
        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>
        /// Summary for Form1
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
            Form1(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }
    
        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~Form1()
            {
                if (components)
                {
                    delete components;
                }
            }
        private: System::Windows::Forms::TableLayoutPanel^  tableLayoutPanel1;
        private: System::Windows::Forms::Button^  button1;
        protected: 
    
        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
                this->tableLayoutPanel1 = (gcnew System::Windows::Forms::TableLayoutPanel());
                this->button1 = (gcnew System::Windows::Forms::Button());
                this->SuspendLayout();
                // 
                // tableLayoutPanel1
                // 
                this->tableLayoutPanel1->ColumnCount = 2;
                this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
                    50)));
                this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent, 
                    50)));
                this->tableLayoutPanel1->Location = System::Drawing::Point(63, 30);
                this->tableLayoutPanel1->Name = L"tableLayoutPanel1";
                this->tableLayoutPanel1->RowCount = 2;
                this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
                this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent, 50)));
                this->tableLayoutPanel1->Size = System::Drawing::Size(386, 264);
                this->tableLayoutPanel1->TabIndex = 0;
                // 
                // button1
                // 
                this->button1->Location = System::Drawing::Point(498, 361);
                this->button1->Name = L"button1";
                this->button1->Size = System::Drawing::Size(88, 25);
                this->button1->TabIndex = 1;
                this->button1->Text = L"button1";
                this->button1->UseVisualStyleBackColor = true;
                this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
                // 
                // Form1
                // 
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(609, 409);
                this->Controls->Add(this->button1);
                this->Controls->Add(this->tableLayoutPanel1);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                this->ResumeLayout(false);
    
            }
    #pragma endregion
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                     add(2,2);
                 }
                 System::Void add(int Col_Num, int Row_Num){
                    int indexNum = 0;
                    for ( int i = 0; i < Col_Num; i++ )
                    {
                        for ( int j = 0; j < Row_Num; j++ )
                        {
                            Panel^ pn = gcnew Panel();
                            CheckBox ^cb = gcnew CheckBox();
                            cb->Size = System::Drawing::Size(50, 25);
                            Label^ lb = gcnew Label();
                            pn->Controls->Add(cb);
                            pn->Controls->Add(lb);
                            lb->Top = 30;
                            lb->Text = L"label" + Convert::ToString(indexNum);
                            tableLayoutPanel1->Controls->Add(pn, i, j);
                            indexNum++;
                        }
                    }
                }
        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                     String^ s = "";
                    for (int i = 0; i < tableLayoutPanel1->Controls->Count; i++)
                    {
                        CheckBox^ cb = (CheckBox^)((Panel^)tableLayoutPanel1->Controls[i])->Controls[0];
                        Label^ lb = (Label^)((Panel^)tableLayoutPanel1->Controls[i])->Controls[1];
                        if (cb->Checked)
                            s = s + lb->Text + L" ";
                    }
                    MessageBox::Show(s);
                 }
    };
    }
    
    

    图片说明

    我已经关注你了,你可以给我发信息。也可以在问题下留言。

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题