A: 

Have you tried accessing the Controls property?

var controls = dockRounds.Controls;
JaredPar
thanks Jared - yes, I can access the controls from the main form, but how do I, for example, with the click event of a button on the dockToolbox form, change a value in a textbox on the dockRounds form? Is it possible to code this in design time?Thanks again, leddy
leddy
dockRounds and dockToolbox are siblings - it sounds like you need a static event.
Philip Wallace
+3  A: 

There are several strategies you can use to achieve communication/linkage between objects on different Forms. Note : My reply here is not going to address any issues specifically related to DockPanelSuite, and is not going to consider the difference between the "secondary" forms being "independent" (i.e., they are not "owned" by the MainForm) or being made child Forms of the MainForm. This is a conscious choice made on the basis of believing that what you are asking about is independent of those possible variations in implementation.

  1. the simplest strategy (if tedious for a lot of controls) is to declare Public Properties in your secondary Forms that expose the controls you want to manipulate from your Main Form. For example, let's say Form2 has a button, and you want to handle its click event on your main form :

In Form2 define a property like :

public Button form2Button
{
    get { return button1; }
}

Now in the Load event of your Main Form, assuming that's where an instance of Form2 is created, you can subscribe to the Click event of the Button on Form2 :

Form2 myForm2;
Form3 myForm3;

private void Form1_Load(object sender, EventArgs e)
{
   myForm2 = new Form2();
   myForm2.form2Button.Click += new EventHandler(form2Button_Click);

   myForm3 = new Form3();
}

And you can easily imagine that in Form3 you have a TextBox that you have exposed with a Public Property in the same way you exposed the Button on Form2.

So you can implement the MainForm's event handler like this for the Button click on Form2 :

public void form2Button_Click(object sender, EventArgs e)
{
    // do something here with the TextBox on Form3 
    // myForm3.theTextBox.Text = 
}

... other strategies ...

  1. in your secondary form, for example, a button press can raise a Public Event which the Main Form (or any other entity to which Form2 is exposed) could subscribe to and then dispatch the appropriate whatever to the appropriate target.

  2. you can abstract message-passing in general at a higher level into a single (perhaps static) class where publishers send messages, and the messages are dispatched to registered listeners.

Finally, the discussion here may be of interest to you :

http://stackoverflow.com/questions/1736444/using-the-controls-of-one-form-into-another/1736710#1736710

best,

BillW
thanks Bill, I've tested this and your first suggestion works great - however, there are quite a few controls over the two forms - is there any way I can put the event code for those controls in a separate class so as to not clutter up the main form code, but that can still refer to the controls? Thanks again
leddy
Note in .NET FrameWork 3.5, C# 3.0, you have "automatic properties" which can save you lots of typing. A quick "once-over" by MS's Dan Whalin : http://weblogs.asp.net/dwahlin/archive/2007/12/04/c-3-0-features-automatic-properties.aspx More details see : http://msdn.microsoft.com/en-us/library/bb384054.aspx For the broader question you raise, imho we'd need to know more information about your application : is there a pattern of object connections, for example. I'm into using a single static class that serves as a "dispatch" center for synchronizing events/content across forms. best,
BillW
+1  A: 

Your classes, dockRounds and dockToolbox should expose any properties/events that you want to access. So if you want to hook up to a control's event, route it to a public event.

TheSean