我的情况:只添加检查的项目从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