Daryl Gill
Daryl Gill

Reputation: 5524

Unable to update main form control from subclass

Attempt #2.

I'm attempting to call functions within two classes. One function to obtain data and call function B in class B to display the data through a Windows Form application.

    //Class B containing function B
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUI_Server
{
    public partial class ChatServer : Form
    {

        private delegate void EnableDelegate(bool enable);
        private static ChatServer CHT = new ChatServer();

        public ChatServer()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ServerBackBone.test();
        }

        public static void TestConnectivity(string text)
        {
            CHT.TestLabel.Text = text;
        }
    }
} // Assume Class B

and the following is Class A and function A

   using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;


namespace GUI_Server
{
    class ServerBackBone
    {


        private static ChatServer CHT = new ChatServer();
        public static void test()
        {
            ChatServer.TestConnectivity("test");


        }


    }
}

So, what is going on here is that the form is being loaded, calling a static function within class A to contact class B > function B to update a label on the main window... The problem lays with updating the label with the new text.

Stepping through the code shows that all functions are successfully called & expected text is passed through to the function.

Although, when pressing F5 to start debugging said application, Diagnostic tools show the following message:

The diagnostic tools failed unexpectedly

The diagnostic hub output shows:

Response status code does not indicate success: 401 (Unauthorized).

General output shows:

The thread 0x20c0 has exited with code 0 (0x0).

The thread 0x1624 has exited with code 0 (0x0).

'GUI Server.vshost.exe' (CLR v4.0.30319: GUI Server.vshost.exe): Loaded 'c:\users\shaiya\documents\visual studio 2015\Projects\GUI Server\GUI Server\bin\Debug\GUI Server.exe'. Symbols loaded.

The thread 0x20fc has exited with code 0 (0x0).

The thread 0xae8 has exited with code 0 (0x0).

Application successfully runs after these messages show. Stepping through shows now errors, nor do breakpoints. Label is not updated in the main control either

Completely new project. Only element on the actual form interface is the label with the property renamed to TestLabel.

 this.TestLabel = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // TestLabel
            // 
            this.TestLabel.AutoSize = true;
            this.TestLabel.Location = new System.Drawing.Point(43, 70);
            this.TestLabel.Name = "TestLabel";
            this.TestLabel.Size = new System.Drawing.Size(35, 13);
            this.TestLabel.TabIndex = 0;
            this.TestLabel.Text = "label1";

Upvotes: 0

Views: 124

Answers (1)

Jonathan
Jonathan

Reputation: 5018

I'm not sure that this in Form1_Load and CHT in TestConnectivity are the same object. They would have to be, because it is this that you are trying to update.

Upvotes: 1

Related Questions