The wish to pass a value from a (child) class to a (parent) formclass!

  • Thread starter Thread starter H_stone
  • Start date Start date
H

H_stone

I have a MainForm class and a second class which initializes an
instance of a second class which I call CommunicationManager.

This is just a simplified representation of the MainForm class:

partial class MainForm : Form
{
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

// Public accessor
public string textToDisplayBox
{
set { this.textBox4.Text = value; }
}

// CommunicationManager
CommunicationManager comm = new CommunicationManager();

}

The CommunicationManager class has a simple function, i.e. to collect
in a string the output from the comport...

My troubles stem from the fact that I am unable to reference back to
the form which second class is initiated.

Do you have any suggestions on how to achieve this?

Best regards,
H
 
I have a MainForm class and a second class which initializes an
instance of a second class which I call CommunicationManager.

This is just a simplified representation of the MainForm class:

    partial class MainForm : Form
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new MainForm());
        }

        // Public accessor
        public string textToDisplayBox
        {
            set { this.textBox4.Text = value; }
        }

        // CommunicationManager
        CommunicationManager comm = new CommunicationManager();

    }

The CommunicationManager class has a simple function, i.e. to collect
in a string the output from the comport...

My troubles stem from the fact that I am unable to reference back to
the form which second class is initiated.

Do you have any suggestions on how to achieve this?

Best regards,
H

Hi,

It's not very clear from your post where is your problem.
You can pass to the contructor of CommunicationManager a reference to
your form
CommunicationManager comm = new CommunicationManager( this);
 
I have a MainForm class and a second class which initializes an
instance of a second class which I call CommunicationManager.

This is just a simplified representation of the MainForm class:

partial class MainForm : Form
{
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}

// Public accessor
public string textToDisplayBox
{
set { this.textBox4.Text = value; }
}

// CommunicationManager
CommunicationManager comm = new CommunicationManager();

}

The CommunicationManager class has a simple function, i.e. to collect
in a string the output from the comport...

My troubles stem from the fact that I am unable to reference back to
the form which second class is initiated.

Do you have any suggestions on how to achieve this?

Best regards,
H

The best way to do this is to create a public Event in
CommunicationManager and subscribe to that event in the form.

The event should have two arguments, the first of type 'object' that
is a reference to the instance of CommunicationManager that raises the
event, and a second argument that is an instance of a class derived
from System.EventArgs that includes the data that needs to be passed
to the form.
 
Back
Top