2

frmPlaceOrder is my form1. I need to pass the firstName, lastname and Address from this form to the second one which will do the other functions. I don't know how to do that.

namespace Lab1_OrderCake
{
public partial class frmPlaceOrder : Form
{
    public static CustomerInformation customer;
    public static Address address;

    public frmPlaceOrder()
    {
        InitializeComponent();
        customer = new CustomerInformation(txtFName.Text, txtLName.Text);
        address = new Address(txtAddress.Text, txtCity.Text, txtPC.Text, txtProvince.Text);

    }

    private void btnPlaceOrder_Click(object sender, EventArgs e)
    {


        DialogResult dlgMsg;
        if (txtFName.Text == "")
        {
            MessageBox.Show("Please enter first name", "Data Missing");
            txtFName.Focus();
            return;
        }
        if (txtLName.Text == "")
        {
            MessageBox.Show("Please enter Last name", "Data Missing");
            txtLName.Focus();
            return;
        }
        else
        {
            frmCakeOrder  newCust = new frmCakeOrder();
            this.Hide();
            newCust.ShowDialog();
            this.Close();

        }

    }
  }
}

The second form; after the first one has been filled out needs to take the values from form1 and display it with the other values(frmCakeOrder values) in the second form. It needs to be seen in the View and Order events when i click it.

here is the Second form:

namespace Lab1_OrderCake
{
public partial class frmCakeOrder : Form
{

    Order cakeOrder;
    public List<Cake> cakeList;
    public frmCakeOrder()
    {
        InitializeComponent();
        cmbTraditionalCake.SelectedIndex = 0;
        cakeOrder = new Order();
        gbCustomCake.Visible = false;
        this.Size = new Size(700,360);
        cakeList = new List<Cake>();

    } 

    private void bttnOrder_Click(object sender, EventArgs e)
    {
        DialogResult dlgMsg;
        dlgMsg = MessageBox.Show(cakeOrder.ToString(), "Confirm Order", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

        if (dlgMsg == DialogResult.Yes)
        {  
            MessageBox.Show(cakeOrder.PrintConfirmation());

        }
        else
        { 
            MessageBox.Show ("The order has not been placed");
        }
        bttnReset.Focus();
        cakeOrder.ClearCart();

    }

    private void radCustom_CheckedChanged(object sender, EventArgs e)
    {
        if (radCustom.Checked)
        {
            cmbTraditionalCake.Enabled = false;
            gbCustomCake.Visible = true;
        }
        else
        {
            cmbTraditionalCake.Enabled = true;
            gbCustomCake.Visible = false;
        }
    }

    private void btnView_Click(object sender, EventArgs e)
    {
        DialogResult dlgMsg;

        cakeOrder.NumOfCakes=1;
        dlgMsg = MessageBox.Show(cakeOrder.ToString(), "Your order: ", MessageBoxButtons.YesNo , MessageBoxIcon.Information);
        if (dlgMsg == DialogResult.No)
        {
            cakeOrder.ClearCart();
            MessageBox.Show("Please enter and confirm your order!");
  }



    private void btnAdd_Click(object sender, EventArgs e)
    {

        if (radCustom.Checked)
        {
            string flavour, occasion;
            flavour = occasion = "";
            int layers;

            //for flavor
            if (radBanana.Checked)
                flavour = "Banana";
            else if (radChocolate.Checked)
                flavour = "Chocolate";
            else if (radVanilla.Checked)
                flavour = "Vanilla";

            if (radTier2.Checked)
                layers = 2;
            else if (radTier3.Checked)
                layers = 3;
            else
                layers = 1;

            if (radGraduation.Checked)
                occasion = radGraduation.Text.TrimStart(new char[] { '&' });
            else if (radWedding.Checked)
                occasion = radWedding.Text.TrimStart(new char[] { '&' });
            else occasion = radAnniversary.Text.TrimStart(new char[] { '&' });
            cakeOrder.AddCake(new Custom(flavour, occasion, layers));
        }
        else
        {
            cakeOrder.AddCake(new Traditional(cmbTraditionalCake.SelectedItem.ToString()));
        }
        cakeList.Add(cakeOrder);
    }


}
}
Atheya
  • 99
  • 10
  • Why don't you pass the arguments in the constructor of `frmCakeOrder`? Alternatives that come to mind: dedicated property to be set after calling the constructor, event handler. Both are usually more hassle, so when in doubt go with constructor injection. – mbx Apr 06 '18 at 15:15

2 Answers2

2

There are many ways to do this. Try it this way.

private void btnPlaceOrder_Click(object sender, EventArgs e) {
   string fname = textBox1.Text;
   frmCakeOrder frm = new frmCakeOrder(textBox1.Text);
   frm.Show();
} 

And in frmCakeOrder,

public frmCakeOrder(string fname) {
   InitializeComponent(); 
   textBox1.Text = fname; 
}
Nishan
  • 3,644
  • 1
  • 32
  • 41
  • But how would I do this with a CustomerInformation class that holds the firstname, lastname like would it be frmPlaceOrder.customer.FName = txtFName.Text? – Atheya Apr 07 '18 at 05:34
  • Yes, that also can be done. You can do it frmCakeOrder since you are passing values to that form. In there it is not necessary to to have a text box; you already have the values as parameters, fname in above case. You can use it the way you want. – Nishan Apr 07 '18 at 06:24
0

You can pass the data in the constructor:

public class Form1: from{
    //constructor
    public void Form1(){
    }

    public void button_click(){
        //Get the data
        var firstName = textFirstName.text;
        var secondName= textSecondName.text;
        var address= textAddress.text;
        //Pass the data on the constructor of Form2
        Form2 f2 = new Form2(firstName,secondName, address);
        f2.show();
    }
}

public class Form2: Form{
    //constructor with data or parameters
    public void Form2(string firstName, string lastName, string Address){
         //do dosomething with the data
         txtFirstName.text = firstName;
    }
}

*sorry if it has sintaxys errors.... but thats the idea.