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

  • SEARCH
  • Home
  • 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 9118211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:05:29+00:00 2026-06-17T05:05:29+00:00

I have a main form with some buttons, textboxes, labels, etc. On a second

  • 0

I have a main form with some buttons, textboxes, labels, etc.
On a second form I would like to copy the text from the main forms textbox onto the second form.

Have tried:

var form = new MainScreen();
TextBox tb= form.Controls["textboxMain"] as TextBox;
textboxSecond.Text = tb.Text;

But it just causes an exception. The main screen textbox is initialised and contains text.
When I hover over form I can see all the controls are there.

What am I doing wrong?

  • 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-06-17T05:05:31+00:00Added an answer on June 17, 2026 at 5:05 am

    Looking at the original code, there are two potential reasons for the NullReferenceException you are getting. First, tb is not defined in the code you provide so I am not sure what that is.

    Secondly, TextBox textbox = form.Controls["textboxMain"] as TextBox can return null if the control is not found or is not a TextBox. Controls, by default, are marked with the private accessor, which leads me to suspect that form.Controls[...] will return null for private members.

    While marking the controls as internal will potentially fix this issue, it’s really not the best way to tackle this situation and will only lead to poor coding habits in the future. private accessors on controls are perfectly fine.

    A better way to share the data between the forms would be with public properties. For example, let’s say you have a TextBox on your main screen called usernameTextBox and want to expose it publicly to other forms:

    public string Username
    {
      get { return usernameTextBox.Text; }
      set { usernameTextBox.Text = value; }
    }
    

    Then all you would have to do in your code is:

    var form = new MainForm();
    myTextBox.Text = form.Username; // Get the username TextBox value
    form.Username = myTextBox.Text; // Set the username TextBox value
    

    The great part about this solution is that you have better control of how data is stored via properties. Your get and set actions can contain logic, set multiple values, perform validation, and various other functionality.

    If you are using WPF I would recommend looking up the MVVM pattern as it allows you to do similar with object states.

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

Sidebar

Related Questions

I have a main form (frmMain) , with some buttons (button1, button2...) . then
I have a simple form containing a main view and also some text boxes
I have a Main form.I want to launch another form from it and launch
I my Main Form, there are two buttons that will display other Forms (each
I have a main Form. There is a linklabel. If i click on it,
I have a main form, and a floating child form that is non-modal. The
I have a main form and non-modal autocomplete form. How can I prevent the
I have a main form with a tab control containing multiple subforms. I need
i have a main form that contains an inner form (that inner form appears
I have WPF Application where I have One main form and other user controls

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.