zhu_zy123 2020-07-29 14:01 采纳率: 87.5%
浏览 236
已采纳

windowsForm开发 如何取tableLayoutPanel中控件的值

1.背景:
动态创建了一张tableLayoutPanel表(例如:根据用户操作面板上入力:行为3,列为2,点击按钮生成3行2列的tableLayoutPanel),并且在表的里面各加入了comboBox控件

2.问题:
如何取得tableLayoutPanel表格中各个ComboBox控件的当前显示值?(例如:取表格的(1,1)中ComboBox的值[苹果],如,用户选择了“苹果”)

3.想法:
先动态创建tableLayoutPanel,然后用循环创建N(例如:6个)个comboBox,放到对应的tableLayoutPanel表格中。

4.开发语言:
vs C++/CLR

5希望:
求大神指教方法,或者具体功能的代码(C++/CLR的代码或者C#的dai'ma)

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->tableLayoutPanel_display->add(this->comboBox_display,i,j);  //把comboBox控件添加到对应的表格里面

        省略

        indexNum++;
    }
}

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-07-29 15:04
    关注
    #pragma once
    
    namespace Q1092586 {
    
        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;
                    array<String^>^ strItem = {"苹果", "梨","a","b"};
                    for ( int i = 0; i < Col_Num; i++ )
                    {
                        for ( int j = 0; j < Row_Num; j++ )
                        {
                            ComboBox^ comboBox_display = gcnew ComboBox();
                            //
                            //comboBox_display
                            //
                            comboBox_display->Items->AddRange(strItem);  //strItem里面为"苹果","梨"
                            comboBox_display->Name = "comboBox_" + indexNum;
                            comboBox_display->Text = "";
                            //comboBox_display->SelectedIndexChanged += gcnew System::EventHandler(this,&Form,CcomboBox_display_SelectedIndexChanged); //创建触发事件
                            tableLayoutPanel1->Controls->Add(comboBox_display, i, j);  //把comboBox控件添加到对应的表格里面
                            indexNum++;
                        }
                    }
                }
        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                     int i = 1;
                     int j = 0;
                     MessageBox::Show(((ComboBox^)tableLayoutPanel1->Controls[i * 2 + j])->Text);
                 }
    };
    }
    
    

    图片说明

    辛苦写代码不易,如果问题解决的话,请点下我回答下面的采纳。如果还有不明白,请留言

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?