본문 바로가기
2/[ C# ]

[C#] Pass Listbox items from Main Form to Sub Form

by Kieran_Han 2022. 6. 1.

부모폼에 있는 Listbox의 items들을 자녀폼의 Listbox로 옮기는 방법이다.

자녀 품을 Show 해주는 Button이 있다. (혹은 다른 event)

Main Form에 작성

private void button1_Click(object sender, EventArgs e)
{
    SubForm f = new SubForm(listbox1.Items);
    f.show();
}

Sub Form에 작성

public partial class SubForm : Form
{
    public SubForm()
    {
        InitializeComponent();
    }
    
    public SubForm(ListBox.ObjectCollection objectCollection)
    {
        InitializeComponent();
        
        this.listBox1.DataSource = objectCollection;
    }
}

 

Source: https://stackoverflow.com/questions/23424880/how-to-pass-listbox-items-from-one-to-other-form