我知道如何将信息从Form1发送到Form2,但是如果数据包含在List< T>中,该如何编辑和发送回信息.在Form1中? [英] I know how to send information from Form1 to Form2, but how do I edit it and send it back if the data is contained in a List<T> in Form1?

查看:164
本文介绍了我知道如何将信息从Form1发送到Form2,但是如果数据包含在List< T>中,该如何编辑和发送回信息.在Form1中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码将其从Form1发送到Form2:

I have this code to send it from Form1 to 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代码:

Then this is my Form2 code:

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();
    }
}

这是我的ShoppingBasket类:

This is my ShoppingBasket class:

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);
    }

OrderItem类:

OrderItem class:

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未处理',说索引-1超出范围.参数名称:index"指向此行: 如果(this.fm1.lstCart.GetSelected(i))

The problem I am getting is that it gives me: 'ArgumentOutOfRangeException was unhandled' saying "Index -1 is out of range. Parameter name: index" pointing to this line: if (this.fm1.lstCart.GetSelected(i))

但是以前它给了我另一个错误,说对象引用未设置为对象的实例."

But previously it has given me another error saying "Object reference not set to an instance of an object."

如何做到这一点,以便将以前在Form1中所选字段中的值更改为我从Form2传递回Form1的值?

How do I make it so that the values previously in the selected field in Form1 are changed to the values I pass from Form2 back to Form1?

推荐答案

Form2中创建new Form1()时,将创建一个没有填充数据的全新Form1.不是最初的Form1调用了Form2.

When you make new Form1() inside Form2, you are creating a completely new Form1 with no filled data. It's not the original Form1 that called Form2.

因此,您需要将Form1设置为原始版本,而不是新版本:

So, what you need is to set that Form1 to the original, not to a new one:

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();
    }
}

这篇关于我知道如何将信息从Form1发送到Form2,但是如果数据包含在List&lt; T&gt;中,该如何编辑和发送回信息.在Form1中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆