在运行时创建控件并访问其值

时间:2011-07-16 14:08:29

标签: c# winforms

我正在尝试在运行时根据数据库表中的数据创建CheckedListBox。

表中的数据,例如

Kern
Formax
Buhrs

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

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

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

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

我非常擅长在运行时创建控件。

提前致谢:)

我已经通过创建一个checkedlistbox数组来解决这个问题,并通过在我的表中使用ammount行创建每个checkedlistbox,并使用表中的数据填充checkedlistbox。通过遍历所有pf的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());
        }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你有两种类型的实体......让我们称它们为T1和T2

现在您想为每个T1实体添加一个控件到表单,使您能够将T2实体与T1实体相关联,并且该控件应该是CheckedListBox ......

嗯,这是一个快速而又脏的示例表单:

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