Passing info to a new windows form

dogdaynoon

New member
Joined
Apr 10, 2015
Messages
2
Programming Experience
Beginner
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:
C#:
NewForm newForm = new NewForm("text");
newForm.Show();


in second form I am doing like this
C#:
 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

New member
Joined
Apr 10, 2015
Messages
2
Programming Experience
Beginner
I thought I would post the answer to this just in case anyone else needed it. Not sure how to mark answered...?

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.
got the answer from this thread here: https://social.msdn.microsoft.com/F...nfo-to-a-new-windows-form?forum=csharpgeneral


 
Top Bottom