Jack Daniels
Jack Daniels

Reputation: 78

Access child form group control elements from mdi parent

Main form used as MDI parent form and it has got DevExpress.XtraBars.Bar and it contains a DevExpress.XtraBars.BarSubItem as a menu. When I click menu item, child form mounts this main form, an open file dialog is shown, selected an XML file and datas from XML file fill textbox controls. These textbox controls from child form are located in group control box.

I tried too many trials like this:

private void bbiHakimIsListesiBilgileri_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    if (ActiveMdiChild != null && ActiveMdiChild.Name == "_child_form")
    {
        var h = Control; // I don't know how I access GroupBox Control where located from child form.
        GetXMLDatas(h);
    }
    else
    {
        var frm = new _child_form { MdiParent = this, Dock = DockStyle.Fill };
        frm.Show();

        var h = Control; // I don't know how I access GroupBox Control where located from child form.
        GetXMLDatas(h);
    }
}

Here is the GetXMLDatas method:

private void GetXMLDatas(Control k)
{
    ofd.Title = @"Select an XML file.";
    ofd.Filter = @"(*.xml)|*.xml|All files(*.*)|*.*";
    ofd.FilterIndex = 1;
    ofd.InitialDirectory = Tools.documents;
    ofd.Multiselect = false;
    ofd.ShowDialog();
    if (string.IsNullOrEmpty(ofd.FileName)) return;
    var data = XElement.Load(ofd.FileName).Descendants("field");
    foreach (var f in Fields(k))
    {
        var value = data.FirstOrDefault(v => v.Attribute("key")?.Value == f.Name);
        if (value != null) f.Text = value.Attribute("value")?.Value;
    }
}

I don't know how I access GroupBox Control where located from child form in main form.

EDIT - 1: As a result, I want to find a groupbox control on an mdi child form from code on the mdi parent form. I have 3 mdi child form and two child forms have a groupbox. I want to reach these. Because if I manage to reach these two, I think, I can reach under these groupbox

EDIT - 2: After GuidoG's answer, I tried these:

MDI Child form name ise FormMDIChild_1. I added this code to FormMDIChild_1 text:

public GroupBox GetGroupBox()
{
    return groupBox1;
}

Later, I added this code to mdi parent form called main form:

        if (ActiveMdiChild is FormMDIChild_1)
        {
            GroupBox myGroupBox = (FormMDIChild_1)GetGroupBox();
        }

But it gives errors like the screenhot:

Screenshot - 1

Screenshot - 2

Screenshot - 3

Upvotes: 1

Views: 923

Answers (1)

GuidoG
GuidoG

Reputation: 12059

quick and dirty method :

create a method on each MDI Child form like this :

// suppose this mdi child is called FormMDIChild_1

public GroupBox GetGroupBox()
{
   return Groupbox1;
}

in MDI Parent do this :

if (ActiveMdiChild is FormMDIChild_1)
{
    GroupBox myGroupBox = ((FormMDIChild_1)ActiveMdiChild).GetGroupBox();
}

Better solution :

Create an MDI Child and call it for example FormBaseMDIChild
On this FormBaseMDIChild create a virtual method

public virtual GroupBox GetGroupBox()

inherit all other MDI Childs forms from FormBaseMDICHild and override the method GetGroupBox()

In MDI Parent do this

myGroupBox = ((FormBaseMDICHild)ActiveMdiChild).GetGroupBox();

Upvotes: 1

Related Questions