none
refer to MdiParent by name

    Question

  • Hi,

    I am using this to refer to the MdiParent:

    BrowsePatientForm.MdiParent = this;

    is it possible to to refer to specific form instead so it will be something like this:

    BrowsePatientForm.MdiParent = frmMain;

    Jassim Rahma

    Tuesday, March 20, 2012 11:10 AM

Answers

  • Jassim,

    While this would be technically feasable...

    // in FrmMain.cs;
    FrmFind frmFind = new FrmFind();
    frmFind.ShowDialog(this); // pass frmMain as Owner
    // in FrmFind.cs
    BrowsePatientForm browsePatientForm =  new BrowsePatientForm();
    browsePatientForm.MdiParent = this.Owner;
    browsePatientForm.Show();

    ... why would you want to do something like this?

    When you close your find form, you would leak resources. Because FrmMain now holds a reference to the browsePatientForm instantiated in you find form, the find form would possibly never get garbage collected. That wouldn't be such a pain, however, if you would just hide the find form, instead of closing it and would cache its instance in frmMain for later use.

    The much easier variant would be to wait for frmFind.ShowDialog() to close with a DialogResult.OK and then instantiate the BrowsePatientForm within your main form.

    FrmFind frmFind = new FrmFind();
    if (frmFind.ShowDialog(this) == DialogResult.OK)
    { 
        int patientID = frmFind.SelectedPatientID;
        BrowsePatientForm childForm = new BrowsePatientForm(patientID);
        childForm.MdiParent = this;
        childForm.Show();
    }

    Instead of passing the data throught the constructor, you could also set a property etc.

    One other way:

    MdiChildForm childForm = new MdiChildForm();
    childForm.MdiParent = Application.OpenForms["FrmMain"];
    childForm.Show();
    If you use VB.NET, you could also use WindowsFormsApplicationBase.MainForm.

    Marcel


    Tuesday, March 20, 2012 2:02 PM

All replies

  • Looks like you are launching BrowsePatientForm from Mdi Form... Right? In that case, the answer is NO. Because, you can't refer the current object by any other name except 'this'.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, March 20, 2012 12:07 PM
  • Hi

    you can use any form insance at the MdiParent property :)

    Regards Muli


    If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved or at least vote as helpful when done...
    Contact:
    @Twitter
    Blogger

    Tuesday, March 20, 2012 12:21 PM
  • Jassim could actually write something like:

    FrmMain frmMain = this;
    BrowsePatientForm.MdiParent = frmMain;
    ...

    provided that this refers to an instance of FrmMain.
    But this wouldn't make much sense I'm afraid, since this already refers to the instance.

    As long as the value passed to the setter of MdiParent is of type Form, the assignment will succeed. I think that Jassim will need to clarify *why* he doesn't want to set the MdiParent to this.

    Marcel

    Tuesday, March 20, 2012 12:50 PM
  • frmmain is my main form.

    I am opening frmFind as Dialog box then when user click find button I want to display BrowsePatientForm as MdiChild withing the frmmain


    Jassim Rahma

    Tuesday, March 20, 2012 1:17 PM
  • Hi Jassim,

    You should just instantiate your form, and write something like this:

    frmFind form = new frmFind();

    form.MdiParent = this;

    form.Show(); //tip: ShowDialog will not work since it is a child window

    Hope it helps

    Regards Muli


    If some code doesn't work, don't worry help is on the way.. don't forget to mark your thread as solved or at least vote as helpful when done...
    Contact:
    @Twitter
    Blogger

    Tuesday, March 20, 2012 1:28 PM
  • Jassim,

    While this would be technically feasable...

    // in FrmMain.cs;
    FrmFind frmFind = new FrmFind();
    frmFind.ShowDialog(this); // pass frmMain as Owner
    // in FrmFind.cs
    BrowsePatientForm browsePatientForm =  new BrowsePatientForm();
    browsePatientForm.MdiParent = this.Owner;
    browsePatientForm.Show();

    ... why would you want to do something like this?

    When you close your find form, you would leak resources. Because FrmMain now holds a reference to the browsePatientForm instantiated in you find form, the find form would possibly never get garbage collected. That wouldn't be such a pain, however, if you would just hide the find form, instead of closing it and would cache its instance in frmMain for later use.

    The much easier variant would be to wait for frmFind.ShowDialog() to close with a DialogResult.OK and then instantiate the BrowsePatientForm within your main form.

    FrmFind frmFind = new FrmFind();
    if (frmFind.ShowDialog(this) == DialogResult.OK)
    { 
        int patientID = frmFind.SelectedPatientID;
        BrowsePatientForm childForm = new BrowsePatientForm(patientID);
        childForm.MdiParent = this;
        childForm.Show();
    }

    Instead of passing the data throught the constructor, you could also set a property etc.

    One other way:

    MdiChildForm childForm = new MdiChildForm();
    childForm.MdiParent = Application.OpenForms["FrmMain"];
    childForm.Show();
    If you use VB.NET, you could also use WindowsFormsApplicationBase.MainForm.

    Marcel


    Tuesday, March 20, 2012 2:02 PM