2017-07-07 59 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。我會盡力繼續這個新的基礎。 :) –