【问题标题】:Add ONLY checked items from a checklistbox to a listView control仅将复选框中的选中项添加到 listView 控件
【发布时间】:2017-07-07 09:07:34
【问题描述】:

我的情况:

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



}
}

但是我得到了错误

字段初始值设定项不能引用非静态字段、方法或属性“Form1.clbKruidenierswaren”。

您能指导我找到一个可行的解决方案吗?

【问题讨论】:

  • 不要将checkboxlist 传递给ctor,而是传递所选ID 的列表,而不是像List&lt;int&gt;。然后根据 id 而不是复选框填充您的 ListView。如果您想在创建时将数据传递给Form,请创建一个继承FormBaseForm 类,然后放置一个类似object InitialisationData {get;set;} 的属性。
  • @Dan Rayson:你能给我举个例子吗?老实说,这对我来说还没有多大意义..
  • Hoi Richard ;),我看到的是你以多种方式填充你的列表,它背后的想法是你正在学习还是你实际上正在实施这个?因为有些东西可能需要大修..
  • Hoi Blaatz0r,嗯,我是 C# 新手,并且正在学习。如果我的代码对您没有意义,请评论它,并告诉我我需要更改哪些内容才能使其正常工作。 :)
  • @RichardPostma 我可以看出你是荷兰人:P 为什么类是抽象的?

标签: c# winforms listview checkeditems


【解决方案1】:

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

类本身:

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;
    }
}

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

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

Boodschappenlijst boodschappenLijst = 
           new Boodschappenlijst(clbKruidenierswaren.CheckedItems.Cast<string>().ToList());

【讨论】:

  • 感谢您的回答,EpicKip。但是现在我在这段代码中收到错误“当前上下文中不存在名称'clbKruidenierswaren'”:Boodschappenlijst boodschappenLijst = new Boodschappenlijst(clbKruidenierswaren.CheckedItems.Cast&lt;string&gt;().ToList());
  • 它应该可以工作,但你可以通过属性访问这些项目,因为它是私有的(所以如果你想交换到公共;))
  • 您必须以组合框所在的形式制作该实例
  • 实例位于Form1,其中CheckedListBox 控件也存在。
  • @RichardPostma 制作一个类的实例......也许可以问你的老师或其他什么,因为那是基本的 oop 东西。 - 编辑所以也许它更清楚。如果你愿意,我们可以去聊天室,我可以用荷兰语解释
【解决方案2】:

问题是您正在传递 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();

【讨论】:

  • 对于操作员的要求,丹并不是真正的答案。
  • 不是吗?我以为他在问如何从表单 A 中包含的用户数据填充表单 B。
  • 但我收到错误字段初始化程序无法引用非静态字段、方法或属性“Form1.clbKruidenierswaren”。你能指导我找到一个可行的解决方案吗?
  • @Dan Rayson:谢谢你的回复,Dan。
【解决方案3】:
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;
        }
    }
}

【讨论】:

  • 今天我不能再继续了,但这是你继续的基础。如果我有更多的空闲时间,我会补充这个问题。
  • 感谢 Blaatz0r。我将尝试继续使用这个新的基础。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
相关资源
最近更新 更多