none
Open form2 from form1 and insert text C#

    Question

  • I have a form for customer information. I take the First and Last names from text boxes. And insert them into string variables.
    Now I want to use Button1 to open frmCustPurchase and insert First and Last names into labels. 
    I have this code but it does not work.
      private frmCustPurchase  otherForm = new frmCustPurchase();
            private void Button1_Click(object sender, EventArgs e)
            {           
                string FName = "John;
                string LName = "Smith";
                FName = otherForm.lblFirstName.Text;
       LName = otherForm.lblLastName.Text
                otherForm.Show();          
            }
    this code opens a blank form, help please.
    Friday, March 11, 2016 2:57 AM

Answers

  • Try this change:

        string FName = "John;
        string LName = "Smith";
        otherForm.lblFirstName.Text = FName;
        otherForm.lblLastName.Text = LName;
        otherForm.Show();         

    Also make sure that the labels are available: open frmCustPurchase in Designer, select the label and change the “Modifiers” property to “Public”.

    • Marked as answer by Jay8000 Saturday, March 12, 2016 12:00 PM
    Friday, March 11, 2016 6:12 AM

All replies

  • Try this change:

        string FName = "John;
        string LName = "Smith";
        otherForm.lblFirstName.Text = FName;
        otherForm.lblLastName.Text = LName;
        otherForm.Show();         

    Also make sure that the labels are available: open frmCustPurchase in Designer, select the label and change the “Modifiers” property to “Public”.

    • Marked as answer by Jay8000 Saturday, March 12, 2016 12:00 PM
    Friday, March 11, 2016 6:12 AM
  • while coding this into the Visual Studio code editor it Likes this code. (no errors)

    then when I start (F5) all goes well.

    When I click the button I get:

    error,   Additional information: Object reference not set to an instance of an object.

    Friday, March 11, 2016 1:21 PM
  • my apologies

    after rechecking my work this works.

    i was relying too much on the code editor to make minor typing corrections.  

    C# is much more Case Sensitive than VB.

    my advice to others is ...   Pay close attention to your typing.

    Saturday, March 12, 2016 12:06 PM