Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm writing a small program to communicate toa plc. I have the comms working fine on the main form.
Hi have created a user control which is used a few times on the main form. What i'm trying to do is use the comms method on the main form for comms on the user controls.
I know its a bit of a newby question.
Any ideas would be great

Chris
Posted

Adorn the MainForm with an interface, for example:

C#
public partial class MainForm : Form, ISomeInterface
{
    public MainForm()
    {
        InitializeComponent();
    }
    public void SendText(object sender, string text)
    {
        // Use the data from the caller here on the main form somehow
    }
}


Then, to allow the UserControls to call that code they need to be aware of the MainForm instance, typically this can be done by exposing a property on the UserControl:
C#
public partial class SomeUserControl : UserControl
{
    public SomeUserControl()
    {
        InitializeComponent();
    }
    public ISomeInterface SomeInstance { get; set; }
    private void DoSomething()
    {
        if (SomeInstance != null)
            SomeInstance.SendText(this, "Hello, world!");
    }
}


In order to hook that up, you can (for example) iterate through the controls of MainForm in it's constructor:

C#
public MainForm()
{
    InitializeComponent();
    foreach (Control control in Controls)
    {
        if (control is SomeUserControl)
            ((SomeUserControl)control).SomeInstance = this;
    }
}
 
Share this answer
 
Comments
Dalek Dave 13-Sep-10 7:09am    
Nice Answer.
Thanks for that but is there a using namespace required before creating the "ISomeInterface" interface?
 
Share this answer
 
Comments
Fredrik Bornander 13-Sep-10 9:22am    
My code isn't a working example, it just shows a way to solve your problem.
I've excluded things like namespaces and using statements.
Just create your MainForm like you'd normally do and add the interface declaration just above it, that should do the trick.
And also, you should Accept the answers to the questions you post if you like them.
chrisclarke11 14-Sep-10 6:06am    
Hi Fredrik

Thanks i've got a bit further but still cant access the methods from the main form.

Chris
chrisclarke11 15-Sep-10 6:00am    
Spent a bit more time at it and got my head round it. Thanks again for all the help. Got it working now

Chris
I think I have a faster way to do -- just inject the control with a reference to the main form

C#
public partial class SomeControl: UserControl
{
    MainForm mainForm;
    // Other members go here
}



On the main form do this:
C#
public partial class MainForm : Form, ISomeInterface
{
    public MainForm()
    {
        InitializeComponent();
        someControl1.mainForm = this;
    }
    public void SendText(object sender, string text)
    {
        // Use the data from the caller here on the main form somehow
    }


}

Basically, you can access the control from main and the main from the control
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900