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

时间:2020-04-12 15:32:17

标签: c# winforms class listbox

我目前正在从事我的第一个实际项目,即WinForms中基于壁虎的原始浏览器(我是初学者,请不要对我苛刻)。 浏览器本身位于Form1(空白)中,当我单击历史记录按钮时,Form 2应该弹出带有历史记录的ListBox。 当我试图弄清楚如何将列表从Form1发送到Form2时,我被阻挠了。 (当我使用Form1中的ListBox尝试时,它起作用了)

Form1:

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 个答案:

答案 0 :(得分:0)

Form1 公共局部类ablak:Form // ablak = Form1 {

//代码。

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

Form2:

def cart_add(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    form = CartAddProductForm(request.POST)
    if form.is_valid():
        cd = form.cleaned_data
        cart.add(product=product,
                quantity=cd['quantity'],
                update_quantity=cd['update'])
    return redirect('cart:cart_detail')

def cart_remove(request, product_id):
    cart = Cart(request)
    product = get_object_or_404(Product, id=product_id)
    cart.remove(product)
    return redirect('cart:cart_detail')

def cart_detail(request):
    cart = Cart(request)
    for item in cart:
        item['update_quantity_form'] = CartAddProductForm(initial={'quantity': item['quantity'],'update': True})
    return render(request, 'cart/detail.html', {'cart': cart})