I have the following static method that adds a selected product into an order.
public static void addToOrderFromPicture(string product, string qty, string _price)
{
//I've cut the code as it's not important to the question.
order.Add(product);
}
The products are displayed as Controls on a flow layout panel. The user will click the 'Add to Order' button on the control. This activates the following method.
private void btn_add_Click(object sender, EventArgs e)
{
if (Main.sessionInProgress == true)
{
OrderQty qty = new OrderQty(lbl_caseSize.Text.ToString(), lbl_wholesale.Text.ToString(), lbl_product.Text, lbl_volume.Text.ToString(), lbl_physical.Text, lbl_available.Text, lbl_oo.Text, lbl_inner.Text, lbl_pltQty.Text, lbl_repeat.Text);
qty.StartPosition = FormStartPosition.CenterParent;
DialogResult result = qty.ShowDialog();
if (result == DialogResult.Yes)
{
if (Main.roundCheck == true)
{
// MessageBox.Show(qty.qtyReturn.ToString());
qty.qtyReturn = autoRoundToCaseSize(qty.qtyReturn);
//MessageBox.Show(qty.qtyReturn.ToString());
Main.addToOrderFromPicture(lbl_product.Text.ToString(), qty.qtyReturn.ToString(), qty.priceReturn.ToString());
}
else
{
Main.addToOrderFromPicture(lbl_product.Text.ToString(), qty.qtyReturn.ToString(), qty.priceReturn.ToString());
}
btn_add.Text = "X";
btn_add.BackColor = Color.FromArgb(236, 112, 99);
}
}
}
The reason for the main function being static is so I can call it from this method. In Swift I would use a delegate to pass data between forms etc. However, I'm unsure on how to do this within C#.
Is there a similar method for passing data between forms as there is in Swift. How would I go about doing it? Or is there a way for me to call the method in Main without the need for it to be static.
EDIT: I don't think I've explained the forms etc very well so will try clear it up.
The addToOrderFromPicture method is contained within Main. This function adds products to the order list which is also static and contained within Main.
The 'btn_add_Click' method is contained in Product.cs which is a UserControl. This user control is displayed on a flowPanel which sits on the main form.
So the user clicks activates a function on Product.cs, this creates an instance of OrderQty.cs which is returns a qty to Product.cs - From Product.cs the addToOrder method within Main is called and the data like qty etc is passed to it.
So Product.cs -> OrderQty.cs -> Product.cs -> Main.cs