问题
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();
}
}
}
}
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();
}
}
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 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; }
}
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."
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?
回答1:
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.
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...
}
And in Form 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();
}
}
回答2:
As daniel mentioned you need to pass a reference to your form1 into form2, personally I'd do it in a constructor
public Form2(Form1 form)
{
fm1 = form;
}
Then you really should try to only update a forms fields within the form itself whereever possible so since form2 is modal I'd do something similar to this
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);
}
}
then to close form2 use
this.DialogResult = DialogResult.OK;
来源:https://stackoverflow.com/questions/17792102/i-know-how-to-send-information-from-form1-to-form2-but-how-do-i-edit-it-and-sen