如何將一個變量傳遞給另一窗體?Windows窗體將列表傳遞給一個新窗體
我創建了以下類:
class Cart
{
private string productName;
private int qtd;
private decimal price;
public decimal Price
{
get
{
return price;
}
set
{
price = value;
}
}
public string ProductName
{
get
{
return productName;
}
set
{
productName = value;
}
}
public int Qtd
{
get
{
return qtd;
}
set
{
qtd = value;
}
}
}
我有一個形式,我添加值到我的購物車:
public partial class frmProducts : Form
{
List<Cart> cartList = new List<Cart>();
private void btnAddCart_Click(object sender, EventArgs e)
{
if(txtQtd.Text == "")
{
MessageBox.Show("Enter how many items do you want.", "Products", MessageBoxButtons.OK);
return;
}
if (Convert.ToInt32(txtQtd.Text) > Convert.ToInt32(lblQtd.Text))
{
MessageBox.Show("We onlye have " + lblQtd.Text + " items in stock.", "Products", MessageBoxButtons.OK);
return;
}
Cart cart = new Cart();
cart.ProductName = lblProductName.Text;
cart.Qtd = Convert.ToInt32(lblQtd.Text);
cart.Price = Convert.ToDecimal(lblPrice.Text);
cartList.Add(cart);
}
}
我haave另一個WindowsForms將與cartList工作。我該如何將cartList發送到新的WindowsForms?
試圖從變量的範圍考慮是否需要全局級別,那麼最好讓它們成爲全局變量。而且創建實體類來存儲和傳遞全局數據也更容易。 –
您可以將購物車作爲靜態屬性放在主窗體中[public static List Cart {Private set;得到; },用你的主表單初始化它。使用它與MainForm.Cart –
Eric