1

I'm using the DockPanel Suite in my winforms app. The DockContent class is derived from System.Windows.Forms.Form class and my two forms, dockRounds and dockToolbox, inherit from the DockContent class.

This is the first time I've done this and this is probably a stupid question, but in runtime, how do I access the controls of the dockRounds and dockToolbox forms?

Here is how I load the two forms when the app first runs:

public partial class frmMainNew : Form

    clsMWDockPanel mapPanel;
    dockToolbox dockT = new dockToolbox();
    dockRounds dockR = new dockRounds();

    public frmMainNew()
    {
        InitializeComponent();

        dockPanel = new DockPanel();

        SuspendLayout();

        dockPanel.Parent = panelMain;
        dockPanel.Dock = DockStyle.Fill;
        dockPanel.DefaultFloatWindowSize = new Size(108, 528);

        dockPanel.BringToFront();
        dockPanel.BackColor = Color.Transparent;
        dockPanel.DocumentStyle = DocumentStyle.DockingSdi;

        ResumeLayout();

        string error = "Errors:\r\n";
        try
        {
            loadRounds();
            loadToolbox();
        }
        catch (Exception)
        {
            error = error + "The Toolbox and/or Rounds menu could not be created\r\n";
        }

    }

    public void loadToolbox()
    {
        dockT.CloseButton = false;
        dockT.ShowHint = DockState.Float;
        dockT.Text = "Toolbox";
        dockT.BackColor = Color.WhiteSmoke;
        dockT.Icon = this.Icon;
        dockT.Show(dockPanel);
    }

    public void loadRounds()
    {
        if (mapPanel == null)
        {
            CreateMapPanel().Show(dockPanel, DockState.Document);
        }

        mapMain.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
        //mapMain.BringToFront();

        dockR.CloseButton = false;
        dockR.ShowHint = DockState.DockRightAutoHide;
        dockR.Text = "Rounds Menu";
        dockR.BackColor = Color.WhiteSmoke;
        dockR.Icon = this.Icon;
        dockR.Show(dockPanel);
    }

    DockContent CreateMapPanel()
    {
        mapPanel = new clsMWDockPanel();
        mapPanel.ShowHint = DockState.Document;
        mapPanel.Controls.Add(mapMain);

        return mapPanel;
    }

Many thanks in advance

leddy

leddy
  • 531
  • 3
  • 13
  • 31

4 Answers4

4

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 :

Using The Controls Of One Form Into Another

best,

Community
  • 1
  • 1
BillW
  • 3,415
  • 4
  • 27
  • 46
  • 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 Nov 18 '09 at 12:24
  • 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 Nov 18 '09 at 14:55
1

You can set the access modifier on a control to make it as accessible as you like. The default is "Private" which is why you can't access the controls from the main form.

In Visual Studio, on the Properties tab, there is a Modifiers property which sets the access modifier that is used in the generated designer file.

Set this to "Public" and you be able to access the control from the main form.

I've just used this when I inherited one form from another. By setting the modifier to "Protected" (or "Protected Internal") I was able to access the controls defined in the base class.

Gav
  • 11
  • 1
1

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
  • 4,516
  • 7
  • 40
  • 50
0

Have you tried accessing the Controls property?

var controls = dockRounds.Controls;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 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 Nov 17 '09 at 17:10
  • dockRounds and dockToolbox are siblings - it sounds like you need a static event. – Philip Wallace Nov 17 '09 at 18:21