我的情況:只添加檢查的項目從checklistbox到ListView控件
我有Form1
一個人口checklistbox
控制。 然後我有一個listView
控制Form2
。
我希望用戶能夠檢查0123B上的checklistbox
上的項目,然後點擊Form1
上的按鈕打開Form2
。
Form2
包含listView
控制,我想僅在檢查了Form1
從checklistbox
項目來填充。
我試圖
namespace Boodschappenlijst
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string[] strKruideniersw = new string[] { Boodschappenlijst.producten[0], Boodschappenlijst.producten[1], Boodschappenlijst.producten[2] };
public static string[] strVerswaren = new string[] { Boodschappenlijst.producten[3], Boodschappenlijst.producten[4], Boodschappenlijst.producten[5] };
public static string[] strVerzorgingspr = new string[] { Boodschappenlijst.producten[6], Boodschappenlijst.producten[7], Boodschappenlijst.producten[8], Boodschappenlijst.producten[9] };
public static List<string> kruidenierswList = new List<string>(strKruideniersw);
public static List<string> verswarenList = new List<string>(strVerswaren);
public static List<string> verzproductenList = new List<string>(strVerzorgingspr);
public static string[] strKruidenierswCh;
public void Form1_Load(object sender, EventArgs e)
{
clbKruidenierswaren.Items.AddRange(strKruideniersw);
clbVerswaren.Items.AddRange(strVerswaren);
clbVerzproducten.Items.AddRange(strVerzorgingspr);
strKruidenierswCh = clbKruidenierswaren.CheckedItems;
}
// TODO
// public string kruidenierswChecked = clbKruidenierswaren.CheckedItems;
private void button1_Click(object sender, EventArgs e)
{
// Create a new instance of the Form2 class
Form2 form2 = new Form2();
// Show the settings form
form2.Show();
}
}
public abstract class Boodschappenlijst : Form1
{
public static string[] producten = new string[] { "Peper", "Zout", "Kruidnagel", "Sla", "Komkommer", "Tomaten", "Tandpasta", "Shampoo", "Wax", "Deodorant" };
// Not working.. clbKruidenierswaren is not static.
List<string> items = clbKruidenierswaren.CheckedItems.Cast<string>().ToList();
// Make form1 controls accessible for other classes?
// Form1 form1 = Application.OpenForms.OfType<Form1>().FirstOrDefault();
}
}
但我得到的錯誤
一個字段初始不能引用非靜態字段,方法或屬性「Form1.clbKruidenierswaren」。
你能指點我的解決方案嗎?
不是傳遞了'checkboxlist'在構造函數中,通過選擇ID的列表,而不是像'名單'。然後根據id而不是複選框填充你的'ListView'。如果你想在創建時將數據傳遞給一個'Form',創建一個'BaseForm'類繼承'Form',並且只需要放置一個屬性,例如'object InitialisationData {get; set;}'。 –
@丹·瑞森:你能給我一個例子嗎? 這對我來說還沒有很大的意義,說實話.. –
Hoi Richard;),我看到的是你用多種方式填充你的列表,它是你背後的想法,你正在學習或者你實際上實施這個?因爲有些事情可能需要進行大修。 – Blaatz0r