locked
Passing info to a new windows form RRS feed

  • Question

  • Hopefully this will be easy for someone here.
    I am wanting to pass and append a string into a textbox in a new windows form.
    Currently I am trying the following...

    in first form on a button press do this:
    Code:
    NewForm newForm = new NewForm("text");
    newForm.Show();

    in second form I am doing like this
    Code:
     public NewForm(string textFromForm1)
            {
    
    
                if(textFromForm1 != null)
                {
                
                this.NameOfTextBoxInForm2.AppendText(textFromForm1);
    
    
                } else 
                {
                    MessageBox.Show("No text received!");
                }
                
            }
    I am receiving the following error: 
    An unhandled exception of type 'System.NullReferenceException' occurred in ProgramName.exe
    Additional information: Object reference not set to an instance of an object.


    Troubleshooting tips:
    Check to determine if the object is null before calling the method.
    Use the "new" keyword to create and object instance.
    Get general help about this exception.

    dogdaynoon

    Friday, April 10, 2015 10:13 PM

Answers

  • If you overload or change the existing constructor, a call to InitializeComponent() is required at the beginning. Otherwise, controls are not being created, hence the Exception.

    Armin

    • Marked as answer by dogdaynoon Friday, April 10, 2015 10:36 PM
    Friday, April 10, 2015 10:32 PM

All replies

  • If you overload or change the existing constructor, a call to InitializeComponent() is required at the beginning. Otherwise, controls are not being created, hence the Exception.

    Armin

    • Marked as answer by dogdaynoon Friday, April 10, 2015 10:36 PM
    Friday, April 10, 2015 10:32 PM
  • That was it. Thank you!

    Sigh...


    dogdaynoon

    Friday, April 10, 2015 10:36 PM