我有这段代码将它从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();
}
}
这是我的ShoppingBasket类:
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类:
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超出范围。参数名称:索引”指向这一行: if(this.fm1.lstCart.GetSelected(i))
但之前它给了我另一个错误,说“对象引用未设置为对象的实例。”
如何将以前在Form1中所选字段中的值更改为从Form2传递回Form1的值?
答案 0 :(得分:2)
正如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);
}
}
然后关闭form2使用
this.DialogResult = DialogResult.OK;
答案 1 :(得分:1)
当您在new Form1()
中创建Form2
时,您正在创建一个没有填充数据的全新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();
}
}