c#访问动态控件Windows窗体

本文关键字:窗体 Windows 动态控件 访问 | 更新日期: 2023-09-27 18:13:18

我正在创建一些基于用户输入的组合框。我创建的方框很好,但是当要检查其中的文本时,我很挣扎。

我想也许把它们存储在一个列表中,但到目前为止,这似乎还不起作用。我的目标是通过点击一个按钮来改变所有的文本,但是在尝试了几次之后,我变得很沮丧。

IList<ComboBox> comboBoxes = new List<ComboBox>();

private void AddComboBox(int i)
    {
        var comboBoxStudentAttendance = new ComboBox();           
        comboBoxStudentAttendance.Top = TopMarginDistance(i);  
        comboBoxStudentAttendance.Items.Add("");
        comboBoxStudentAttendance.Items.Add("Present");
        comboBoxStudentAttendance.Items.Add("Absent");
        comboBoxStudentAttendance.Items.Add("Late");
        comboBoxStudentAttendance.Items.Add("Sick");
        comboBoxStudentAttendance.Items.Add("Excused");
        comboBoxes.Add(comboBoxStudentAttendance);
        this.Controls.Add(comboBoxStudentAttendance);
    }

我尝试了以下操作,但没有成功。

private void DistributeAttendanceButton_Click(object sender, EventArgs e) 
    {
        for (int i = 0; i < sampleNum; i++)
        {
            switch (MasterComboBox.Text)
            {
            case "Present": 

                    comboBoxes.ElementAt(i).Text = "Present";
                    break;
                }
            }
    }

c#访问动态控件Windows窗体

试试这个

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int TOP_MARGIN = 10;
        const int LEFT_MARGIN = 10;
        const int WIDTH = 200;
        const int HEIGHT = 10;
        const int SPACE = 15;
        const int NUMBER_OF_BOXES = 10;
        public Form1()
        {
            InitializeComponent();
            MasterComboBox.Text = "Present";
            for (int i = 0; i < NUMBER_OF_BOXES; i++)
            {
                AddComboBox(i);
            }
        }
        List<ComboBox> comboBoxes = new List<ComboBox>();

        private void AddComboBox(int i)
        {
            var comboBoxStudentAttendance = new ComboBox();
            comboBoxStudentAttendance.Top = TOP_MARGIN + i * (SPACE + HEIGHT);
            comboBoxStudentAttendance.Left = LEFT_MARGIN;
            comboBoxStudentAttendance.Width = WIDTH;
            comboBoxStudentAttendance.Height = HEIGHT;
            comboBoxStudentAttendance.Items.Add("");
            comboBoxStudentAttendance.Items.Add("Present");
            comboBoxStudentAttendance.Items.Add("Absent");
            comboBoxStudentAttendance.Items.Add("Late");
            comboBoxStudentAttendance.Items.Add("Sick");
            comboBoxStudentAttendance.Items.Add("Excused");
            comboBoxes.Add(comboBoxStudentAttendance);
            this.Controls.Add(comboBoxStudentAttendance);
        }
        private void DistributeAttendanceButton_Click(object sender, EventArgs e) 
        {
            for (int i = 0; i < comboBoxes.Count; i++)
            {
                switch (MasterComboBox.Text)
                {
                    case "Present":

                        comboBoxes[i].Text = "Present";
                        break;
                }
            }
        }
    }
}