Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:15:29+00:00 2026-05-28T18:15:29+00:00

I’m trying to pass values between a few winforms, I’ve got a total of

  • 0

I’m trying to pass values between a few winforms, I’ve got a total of 6 winforms, that the user will cycle through. I’m passing values between the forms using TextBox and Label Controls.

When I open the Primary winform, then click a button to load the second winform, everything works fine (I can pass values to the First Form). My problem is that once I direct the user to another form and this.Hide(); the current (2nd Winform) then try to use the Third form to pass values to the first, I get the following error:

Object reference not set to an instance of an object.

I’m confused because the control that the should be passing the value is passing the value to the first Form isn’t NULL

I’m using the same code to connect all the forms together.

public MainForm MainForm;

Then I’m trying to pass the values like so:

 MainForm.textBox1.Text = txt_FileName.Text;

Note: All the TextBox and Label controls that are passing values between the forms are public

Anyone run into this? Or any Ideas?
.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:15:30+00:00Added an answer on May 28, 2026 at 6:15 pm

    You need to make sure that all your forms are instantiated (through new MyForm1()…). Just declaring a variable of type MainForm won’t create a form instance – you’ll have to do it. My guess is that one of your forms is not created yet when you try to access a control.

    This is yet another reason to not to use public controls (see my comment too), since the lifetime of your controls are tied to the lifetime of your form. It’s better to hide controls from public access and send data to the form through data objects – the form will set all those values to its own controls. This also makes validation a lot easier, since a control’s value can only be set to values allowed by the form. If you set control values from the outside, you’ll have a tough time validating them in all scenarios.

    I assume you’re trying to use modal forms that work similar to a wizard where users go from one form to the next, following a clear path. If so, you can do something like this:

    // Data class to set data in Form2
    internal class Form2Data
    {
        public string Name;
        ...
    }
    
    ...
    
    internal class Form2 : Form
    {
        public static DialogResult ShowDlg ( Form2Data oData )
        {
            Form2 oFrm = new Form2 ();
    
            oFrm.SetData ( oData );
            DialogResult nResult = oFrm.ShowDialog ();
    
            if ( nResult == DialogResult.Ok )
                oFrm.GetData ( oData );
    
            return ( nResult );
        }
    
        private void SetData ( Form2Data oData )
        {
            // Set control values here
        }
    
        private void GetData ( Form2Data oData )
        {
            // Read control values here
        }
    }
    
    ...
    // You call this as such:
    Form2Data oData = new Form2Data ();
    oData.Name = "...";
    
    DialogResult nResult = Form2.ShowDlg ( oData );
    
    // after the call, oData should have updated values from Form2
    if ( nResult == DialogResult.Ok )
    {
        // show your next form in a similar pattern - set up data
        // call form's static method to pass it and then wait for
        // the form to finish and return with updated data.
    }
    

    You’d have to use a similar pattern in your other forms, too. This does require more work since you need to set up a different data object for all the forms but this way you can easily do validation before and after the form is shown (in SetData and GetData). It also encapsulates your program better, since controls are not accessible from the outside.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.