我試圖從數據庫表中的數據在運行時創建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());
}
WinForms或WPF? – Vlad
窗體.. –
對不起,不是我的區域:( – Vlad