仅将检查列表框中的选中项目添加到 listView 控件

Add ONLY checked items from a checklistbox to a listView control

我的情况:

我在 Form1 上有一个已填充的 checklistbox 控件。 然后我在 Form2.

上有一个 listView 控件

我希望用户能够在 Form1 上检查 checklistbox 上的项目,然后单击 Form1 上的按钮打开 Form2

Form2 包含 listView 控件,我只想用 填充 来自 checklistboxForm1 选中的项目].

我试过了

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();



}
}

但是我收到错误

A field initializer cannot reference the non-static field, method, or property 'Form1.clbKruidenierswaren'.

你能告诉我一个有效的解决方案吗?

问题是您正在传递 UI 对象,您应该传递数据。这是一个 Form 的示例,可以在构造期间给定数据。

public class BaseForm : Form
{
    public object InitialisationData { get; set; }
}

public partial class MagicForm : BaseForm
{
    public string MyBindableGuy;

    public MagicForm()
    {
        InitializeComponent();

        MyBindableGuy = InitialisationData as string;
    }
}

并使用以下命令打开它:

var myForm = new MagicForm();
myForm.InitialisationData = "Hi, I'm a string.";
myForm.Show();

您应该像这样在 class 中创建一个构造函数:

Class本身:

public class Boodschappenlijst
{
    public static string[] producten { get; set; } = new string[] { "Peper", "Zout", "Kruidnagel", "Sla", "Komkommer", "Tomaten", "Tandpasta", "Shampoo", "Wax", "Deodorant" };
    private List<string> Items { get; set; }

    public Boodschappenlijst(List<string> items)// << Constructor
    {
        Items = items;
    }
}

然后像这样创建一个 class 实例:

在这个地方(表格?)你有 clbKruidenierswaren:

Boodschappenlijst boodschappenLijst = 
           new Boodschappenlijst(clbKruidenierswaren.CheckedItems.Cast<string>().ToList());
public partial class Form1 : Form
{
    // Todo declare the variables
    private List<string> kruidenierswList;
    private List<string> verswarenList;
    private List<string> verzproductenList;

    public Form1()
    {
        InitializeComponent();

        // call the instance once and add that to the variable lijst
        Boodschappenlijst lijst = Boodschappenlijst.Instance; // <- @ others on S/O this is just used as information I know I could do a new as well.

        // initialize the variables
        kruidenierswList = new List<string>() { lijst.Products[0], lijst.Products[1], lijst.Products[2] };
        verswarenList = new List<string>() { lijst.Products[3], lijst.Products[4], lijst.Products[5] };
        verzproductenList = new List<string>() { lijst.Products[6], lijst.Products[7], lijst.Products[8], lijst.Products[9] };
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        // populate the checklist boxes
        clbKruidenierswaren.Items.AddRange(kruidenierswList.ToArray());
        clbVerswaren.Items.AddRange(verswarenList.ToArray());
        clbVerzproducten.Items.AddRange(verzproductenList.ToArray());
    }

    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 class Boodschappenlijst
{
    private static Boodschappenlijst instance;

    public string[] Products
    {
        get;
        private set;
    }

    private Boodschappenlijst()
    {
        Products = new string[] { "Peper", "Zout", "Kruidnagel", "Sla", "Komkommer", "Tomaten", "Tandpasta", "Shampoo", "Wax", "Deodorant" };
    }

    public static Boodschappenlijst Instance
    {
        get
        {
            // singleton initialization => look for design pattern - singleton  <- Design patterns can brighten your day.
            // 
            return instance == null ? instance = new Boodschappenlijst() : instance;
        }
    }
}