2017-07-07 76 views
1

我的情况:只添加检查的项目从checklistbox到ListView控件

我有Form1一个人口checklistbox控制。 然后我有一个listView控制Form2

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

Form2包含listView控制,我想在检查了Form1checklistbox项目来填充。

我试图

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”。

你能指点我的解决方案吗?

+0

不是传递了'checkboxlist'在构造函数中,通过选择ID的列表,而不是像'名单'。然后根据id而不是复选框填充你的'ListView'。如果你想在创建时将数据传递给一个'Form',创建一个'BaseForm'类继承'Form',并且只需要放置一个属性,例如'object InitialisationData {get; set;}'。 –

+0

@丹·瑞森:你能给我一个例子吗? 这对我来说还没有很大的意义,说实话.. –

+0

Hoi Richard;),我看到的是你用多种方式填充你的列表,它是你背后的想法,你正在学习或者你实际上实施这个?因为有些事情可能需要进行大修。 – Blaatz0r

回答

0

问题是你传递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(); 
+0

的构造不是真正的答案丹作为操作要求。 – Blaatz0r

+0

不是吗?我以为他问的是如何从表单A中包含的用户数据填充表单B. –

+0

但我得到错误 字段初始值设定项不能引用非静态字段,方法或属性'Form1.clbKruidenierswaren'。 你可以请直接给我一个解决方案吗? – Blaatz0r

0

你应该做一个构造函数这样的类:

类本身:

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

感谢您的回答,EpicKip。 但现在我得到的错误在这一段代码“名字‘clbKruidenierswaren’不存在当前上下文存在”: 'Boodschappenlijst boodschappenLijst =新Boodschappenlijst(clbKruidenierswaren.CheckedItems.Cast ().ToList()); ' –

+0

它应该可以工作,但是您可以通过属性访问这些项目(因为它是私有的(所以如果您想将该交换项公开;)) – EpicKip

+0

您必须以组合框的形式创建该实例 – EpicKip

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

今天我不能继续这一更多,但这是为你继续的基础。如果我有更多的空闲时间,我会加入这个问题。 – Blaatz0r

+0

感谢Blaatz0r。我会尽力继续这个新的基础。 :) –