[英]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?
我有以下代碼將其從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的值?
正如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;
在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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.