将列表从 class 传递到另一个表单的列表框

Pass a list from a class to another form's listbox

我目前正在做我的第一个真正的项目,WinForms 中基于 gecko 的原始浏览器(我是初学者,请不要对我苛刻)。 浏览器本身位于 Form1 (ablak) 中,当我单击历史记录按钮时,Form 2 应该会弹出一个历史列表框。 当我试图弄清楚如何将列表从 Form1 发送到 Form2 时,我的思路被阻塞了。 (当我用 form1 中的 ListBox 尝试它时它起作用了)

表格 1:

public partial class ablak : Form // ablak=Form1
    {

 // codes..

        List<string> elozmenyek = new List<string>(); // the history list
        public void elozmenyek_method(ref List<string> elozmenyek)
        {

            foreach (GeckoHistoryEntry _E in geckoWebBrowser1.History)
            {
                elozmenyek.Add(_E.Url.ToString()); // putting the urls into the list
            }
        }

        public void elozmenyek_gomb_Click(object sender, EventArgs e) 
        {
            elozmenyek_method(ref elozmenyek);

            Form2.listbox_transfer.DataSource = elozmenyek; // when i click it sends the history list to Form2's listbox_transfer list
        }
    }

表格 2:

public partial class Form2 : Form
    {
        public ListBox listbox_transfer;

        public Form2()
        {
            InitializeComponent();
            listBox1 = listbox_transfer; // puts the transferred list into the ListBox
        }


    }

提前致谢!

表格1 public 部分 class 窗口:窗体 // window=Form1 {

//代码..

    List<string> elozmenyek = new List<string>(); // the history list
    public void elozmenyek_method(ref List<string> elozmenyek)
    {

        foreach (GeckoHistoryEntry _E in geckoWebBrowser1.History)
        {
            elozmenyek.Add(_E.Url.ToString()); // putting the urls into the list
        }
    }

    public void elozmenyek_gomb_Click(object sender, EventArgs e) 
    {
        elozmenyek_method(ref elozmenyek);

        Form2 form2 = new Form2(elozmenyek);//Pass list as constructor parameter to Form2
        form2.ShowDialog();
    }
}

表格 2:

public partial class Form2 : Form
{
    public Form2(List<string> elozmenyek)
    {
        InitializeComponent();
        listBox1.DataSource = elozmenyek; // puts the transfered list into the listbox
    }
}