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 4330930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:02:03+00:00 2026-05-21T10:02:03+00:00

I am trying to modify a TextBox that belongs to Form2 from within a

  • 0

I am trying to modify a TextBox that belongs to Form2 from within a WCF object.

namespace server2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private ServiceHost duplex;

        private void Form2_Load(object sender, EventArgs e)     /// once the form loads, create and open a new ServiceEndpoint.
        {
            duplex = new ServiceHost(typeof(ServerClass));
            duplex.AddServiceEndpoint(typeof(IfaceClient2Server), new NetTcpBinding(), "net.tcp://localhost:9080/service");
            duplex.Open();
            this.Text = "SERVER *on-line*";
        }
    }


    class ServerClass : IfaceClient2Server
    {


        IfaceServer2Client callback;

        public ServerClass()
        {
            callback = OperationContext.Current.GetCallbackChannel<IfaceServer2Client>();

        }

        public void StartConnection(string name)
        {
            var myForm = Form.ActiveForm as Form2;
            myForm.textBox1.Text = "Hello world!";  /// <- this one trows “System.NullReferenceException was unhandled”
                                                    /// unless Form2 is selected when this fires.

            callback.Message_Server2Client("Welcome, " + name );
        }


        public void Message_Cleint2Server(string msg)
        {
        }

        public void Message2Client(string msg)
        {
        }

    }




    [ServiceContract(Namespace = "server", CallbackContract = typeof(IfaceServer2Client), SessionMode = SessionMode.Required)]


    public interface IfaceClient2Server           ///// what comes from the client to the server.
    {
        [OperationContract(IsOneWay = true)]
        void StartConnection(string clientName);

        [OperationContract(IsOneWay = true)]
        void Message_Cleint2Server(string msg);
    }


    public interface IfaceServer2Client          ///// what goes from the sertver, to the client.
    {
        [OperationContract(IsOneWay = true)]
        void AcceptConnection();

        [OperationContract(IsOneWay = true)]
        void RejectConnection();

        [OperationContract(IsOneWay = true)]
        void Message_Server2Client(string msg);
    }

}

Yet the “myForm.textBox1.Text = “Hello world!”;” line throws System.NullReferenceException was unhandled”…

Any ideas, thanks!

  • 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-21T10:02:04+00:00Added an answer on May 21, 2026 at 10:02 am

    As discussed in the comments of the initial question, the problem is you’re referring to ActiveForm, when the form you want is not active. Whenever attempting to cast using the as keyword, the result will be null if the cast is invalid. Since you grabbed a form that could not be cast to Form2 (because it was a different kind of form), you correctly received a null reference exception.

    Assuming you have enforced singleton rules on Form2 and you haven’t played with the form’s name, you can access it by way of the Application.OpenForms collection like so:

    (Form2)Application.OpenForms["Form2"];
    

    In your code sample that could look like this:

    public void StartConnection(string name)
    {
        //var myForm = Form.ActiveForm as Form2;
        var myForm = Application.OpenForms["Form2"] as Form2;
        myForm.textBox1.Text = "Hello world!";  /// <- this one trows “System.NullReferenceException was unhandled”
                                                /// unless Form2 is selected when this fires.
    
        callback.Message_Server2Client("Welcome, " + name );
    }
    

    That said, I don’t think I’d give responsibility of modifying form controls to a WCF service. I’d much sooner consume events fired by the service inside my form.

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

Sidebar

Related Questions

Im trying to modify the class below so that the scrolling text appears behind
I'm trying to modify my GreaseMonkey script from firing on window.onload to window.DOMContentLoaded, but
I'm trying to modify the class of an element if an ajax call based
Hi I'm trying to modify a web page so that it loads faster. Since
I am trying to modify the amcap, an application from Windows SDK's example to
I've been trying to modify the AuditTrail code so that it does not copy
I'm trying to modify SOAP body for an outbound ws client SOAP message, from
I'm trying to modify some exports. I understand that this code doesn't work because
i've been trying to modify the program so that it could accept more than
I am trying to modify a table within a database, as follows: using (SqlCeCommand

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.