我知道如何将信息从Form1发送到Form2,但如果数据包含在List<;T>;表格1

本文关键字:List 数据包 lt gt 如果 表格 信息 Form1 Form2 我知道 | 更新日期: 2023-09-27 18:24:11

我有这个代码可以将它从Form1发送到Form2:

public partial class Form1 : Form
{
    public ShoppingBasket myBasket = new ShoppingBasket();
    public Form1()
    {
        InitializeComponent();
    }
    private void editButton_Click(object sender, EventArgs e)
    {
        int c = lstCart.Items.Count - 1;
        for (int i = c; i <= 0; i++)
        {
            if (lstCart.GetSelected(i))
            {
                Form2 fm2 = new Form2();
                fm2.productNameTextBox.Text = myBasket[i].ProductName;
                fm2.quantityTextBox.Text = Convert.ToString(myBasket[i].Quantity);
                fm2.latestPriceTextBox.Text = Convert.ToString(myBasket[i].LatestPrice);
                fm2.ShowDialog();
            }
        }
    }
}

这是我的Form2代码:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    Form1 fm1 = new Form1();
    private void okBtn_Click(object sender, EventArgs e)
    {
        int c = fm1.lstCart.Items.Count - 1;
        for (int i = c; i <= 0; i++)
        {
            if (this.fm1.lstCart.GetSelected(i))
            {
                this.fm1.myBasket[i].ProductName = this.productNameTextBox.Text;
                this.fm1.myBasket[i].Quantity = Convert.ToInt32(this.quantityTextBox.Text);
                this.fm1.myBasket[i].LatestPrice = Convert.ToDecimal(this.latestPriceTextBox.Text);
                this.Close();
            }
        }
    }
    private void cancelBtn_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

这是我的购物篮类:

public class ShoppingBasket : List<OrderItem>
{
    public ShoppingBasket()
    {
    }
    public decimal BasketTotal { get; set; }
    public new void Add(OrderItem i)
    {
        base.Add(i);
    }
    public new void Remove(OrderItem i)
    {
        base.Remove(i);
    }

订单项类别:

public class OrderItem
{
    public OrderItem(string productName, 
    decimal latestPrice, int quantity)
    {
        ProductName = productName;
        LatestPrice = latestPrice;
        Quantity = quantity;
        TotalOrder = latestPrice * quantity;
    }
    public string ProductName { get; set; }
    public decimal LatestPrice { get; set; }
    public int Quantity { get; set; }
    public decimal TotalOrder { get; set; }
}

我遇到的问题是,它给了我:"ArgumentOutOfRangeException未处理",说"Index-1超出范围。参数名称:Index"指向此行:如果(this.fm1.lstCart.GetSelected(i))

但之前它给了我另一个错误,说"对象引用没有设置为对象的实例。"

如何将Form1中先前选定字段中的值更改为从Form2传递回Form1的值?

我知道如何将信息从Form1发送到Form2,但如果数据包含在List<;T>;表格1

正如daniel所提到的,你需要将对form1的引用传递到form2,我个人会在构造函数中完成

public Form2(Form1 form)
{
    fm1 = form;
}

然后你真的应该尽可能只更新表单中的表单字段,因为form2是模态的,我会做一些类似于这个的事情

using(Form2 fm2 = new Form2(this))
{
    fm2.productNameTextBox.Text = myBasket[i].ProductName;
    fm2.quantityTextBox.Text = Convert.ToString(myBasket[i].Quantity);
    fm2.latestPriceTextBox.Text = Convert.ToString(myBasket[i].LatestPrice);
    if(DialogResult.OK == fm2.ShowDialog(this))
    {
        myBasket[i].ProductName = frm2.productNameTextBox.Text;
        myBasket[i].Quantity = Convert.ToInt32(frm2.quantityTextBox.Text);
        myBasket[i].LatestPrice = Convert.ToDecimal(frm2.latestPriceTextBox.Text);
    }
}

然后使用关闭窗体2

this.DialogResult = DialogResult.OK;

当您在Form2中创建new Form1()时,您正在创建一个没有填充数据的全新Form1。不是最初的Form1调用Form2。

所以,你需要的是将Form1设置为原来的,而不是新的:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    public Form1 fm1; //just declare, but let Form1 do the assign job.
    //the rest of the code...
}

在表格1 中

for (int i = c; i <= 0; i++)
{
    if (lstCart.GetSelected(i))
    {
        Form2 fm2 = new Form2();
        fm2.productNameTextBox.Text = myBasket[i].ProductName;
        fm2.quantityTextBox.Text = Convert.ToString(myBasket[i].Quantity);
        fm2.latestPriceTextBox.Text = Convert.ToString(myBasket[i].LatestPrice);
        //here is the news:
        fm2.fm1 = this;
        fm2.ShowDialog();
    }
}