2011-07-16 49 views
0

我試圖從數據庫表中的數據在運行時創建CheckedListBox。在運行時創建控件並訪問它們的值

表中數據例如

Kern 
Formax 
Buhrs 

在我想要的代碼來創建一個新的CheckedListBox與行的名稱表的每一行。

創建CheckedListBox後,我想從另一個包含其他數據的表中填充它。

只要客戶端在CheckedListBox的數據選擇完成,我想讀取CheckedListBox的所有選中的值並將其保存到數組中。

我的麻煩來自於創建CheckedListBox並在客戶端點擊完成後立即訪問數據。

我很新在運行時創建控件。

感謝提前:)

我已經通過創建checkedlistboxs陣列解決了這個,並在我的表創建具有行ammount的每個checkedlistbox,並通過與表中的數據填充checkedlistboxs。通過遍歷checkedlistboxs中的所有對象,我獲取所有檢查的值並將它們存儲到另一個表中。

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()); 
     } 
+0

WinForms或WPF? – Vlad

+0

窗體.. –

+0

對不起,不是我的區域:( – Vlad

回答

0

訪問數據,如果我理解正確這個,你有2種類型的實體......讓我們稱之爲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; 
     } 
    } 
} 
相關問題