How to access controls on an inherited form?

7k views Asked by At

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

4

There are 4 answers

2
BillW On BEST ANSWER

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,

2
JaredPar On

Have you tried accessing the Controls property?

var controls = dockRounds.Controls;
0
TheSean On

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.

0
Gav On

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.