在运行时创建控件并访问它们的值

本文关键字:访问 运行时 创建 控件 | 更新日期: 2023-09-27 18:02:11

我试图在运行时从数据库表中的数据创建CheckedListBox。

表中的数据,例如

Kern
Formax
Buhrs

对于表中的每一行,我希望代码以行名创建一个新的CheckedListBox。

在创建CheckedListBox之后,我想从包含其他数据的另一个表中填充它。

一旦客户端完成了CheckedListBox的数据选择,我想读取CheckedListBox的所有选中值并将其保存到数组中。

我的麻烦是创建CheckedListBox's并在客户端点击完成后立即访问数据。

我对在运行时创建控件非常陌生。

Thanks in advance:)

我已经解决了这个问题,方法是创建一个checkedlistbox数组,用表中的行数创建每个checkedlistbox,并用表中的数据填充checkedlistbox。通过遍历所有选中的列表框,我得到所有选中的值,并将它们存储到另一个表中。

private CheckedListBox[] Machines;
Machines = new CheckedListBox[test.Length];
int iLocation = 3;
        for (int i = 0; i < Machines.Length; i++)
        {
            Machines[i] = new CheckedListBox();
            Machines[i].Name = "clb" + test[i].ToString();
            Machines[i].Location = new Point(iLocation, 6);
            Machines[i].Size = new Size(120, 139);
            iLocation += Machines[i].Width + 6;

            this.Controls.Add(Machines[i]);
        }
        Machines[2].Items.Add("Hello");
        Machines[2].Items.Add("Goodbye");

此代码用于访问选中列表框

中的数据
ArrayList arrCheckedItems = new ArrayList();
        CheckedListBox.CheckedItemCollection objCheckedItem = Machines[2].CheckedItems;
        for (int i = 0; i < objCheckedItem.Count; i++)
        {
            arrCheckedItems.Add(objCheckedItem[i]);
        }
        string[] srtArray = arrCheckedItems.ToArray(Type.GetType("System.String")) as string[];
        int iMachines = srtArray.Count();
        for (int i = 0; i < iMachines; i++)
        {
            MessageBox.Show(srtArray[i].ToString());
        }

在运行时创建控件并访问它们的值

如果我理解正确的话,你有2种类型的实体…我们称它们为T1和T2

现在你想为每个T1实体添加一个控件,使你能够将T2实体关联到T1实体,这个控件应该是一个checklistbox…

好吧,这里有一个简单的例子:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace controlsAtRuntime
{
    public class Form1 : Form
    {
        private FlowLayoutPanel mainPanel;
        private Button button1;
        private Button button2;
        private Button button3;
        MyDataClass1[] T1Items = new MyDataClass1[] { new MyDataClass1 { Name = "I am element 1" }, new MyDataClass1 { Name = "I am element 2" } };
        MyDataClass2[] T2Items = new MyDataClass2[] { new MyDataClass2 { Name = "Foo" }, new MyDataClass2 { Name = "Bar" } };
        public Form1()
        {
            InitializeComponent();
        }
        private void setUpControlsAtRuntime()
        {
            mainPanel.Controls.Clear();
            foreach (var item in T1Items)
            {
                mainPanel.Controls.Add(getControlForT1Entity(item, T2Items));
            }
        }
        private GroupBox getControlForT1Entity(MyDataClass1 entity,IEnumerable<MyDataClass2> asscociatableData)
        {
            GroupBox box = new GroupBox();//outer element that can give our checked list box a visible name
            box.Text = entity.Name;
            box.Tag = entity; // so you can know the entity this box belongs to ...
            box.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            box.AutoSize = true;
            CheckedListBox chklb = new CheckedListBox();
            foreach (var item in asscociatableData)
            { 
                chklb.Items.Add(item, entity.associatedData != null && entity.associatedData.Contains(item));
                //add all items, and check them if they were contained in entity.associatedData
            }
            box.Controls.Add(chklb);
            chklb.Location = new Point(10, 20);
            return box;
        }
        private IEnumerable<MyDataClass2> getCheckedItemsFromOuterBox(GroupBox box)
        { 
            var chklb = box.Controls[0] as CheckedListBox;
            if (chklb != null)
            { 
                return chklb.CheckedItems.OfType<MyDataClass2>();
            }
            throw new Exception("whoops... that was none of the expected GroupBox Controls...");
        }
        private void InitializeComponent()
        {
            this.mainPanel = new System.Windows.Forms.FlowLayoutPanel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // mainPanel
            // 
            this.mainPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.mainPanel.Location = new System.Drawing.Point(12, 12);
            this.mainPanel.Name = "mainPanel";
            this.mainPanel.Size = new System.Drawing.Size(417, 269);
            this.mainPanel.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.Location = new System.Drawing.Point(330, 287);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(99, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "save selection";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(12, 287);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "clear panel";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(93, 287);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "setup panel";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(441, 322);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.mainPanel);
            this.Name = "Form1";
            this.ResumeLayout(false);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item in mainPanel.Controls.OfType<GroupBox>().Where(x=>x.Tag is MyDataClass1))
            {
                var entity = item.Tag as MyDataClass1;
                entity.associatedData = getCheckedItemsFromOuterBox(item);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            mainPanel.Controls.Clear();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            setUpControlsAtRuntime();
        }
    }
    public class MyDataClass1
    {
        public String Name { get; set; }
        public IEnumerable<MyDataClass2> associatedData { get; set; }
    }
    public class MyDataClass2
    {
        public String Name { get; set; }
        public override string ToString()
        {
            return Name;
        }
    }
}