Question How call methods from child window of another Form( Not a Child/Parent Window)

MediaHub

New member
Joined
Dec 8, 2016
Messages
2
Programming Experience
Beginner
In My Projects i Have 3 Forms:
Form1: MDIParent
Form2: MDIChild
Form3: Normal Form

i want to access data and call methods of Form3 from Child Window.

I am new to C# can any one solve my problem.
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,720
Location
Sydney, Australia
Programming Experience
10+
In VB, the easy way to do this is to use the default instance of Form3. If only one Form3 instance will ever be displayed at a time then you can use the default instance to display and access that instance from anywhere at any time. You could simulate that in C# with a singleton. That would allow you to access the current instance of Form3 via a static property from anywhere at any time in much the same way.

That's not really the "proper" way to do it though. The proper way is to let the main form handle the communication between the other two forms. The main form knows about both the other two so it should communicate with both and act as broker for communication between the two. If the MDI child wants to execute a method in the top-level form then it should raise an event to notify the MDI parent and then that parent calls the method in the other form that it presumably created. Here's a code example where you can type text into a TextBox on an MDI child and have it display in a Label on separate form:

Form2:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        // Expose the Text of the TextBox on the form.
        public string AvailableText => textBox1.Text;

        // Indicates that text is available to be used as desired.
        public event EventHandler TextAvailable;

        protected virtual void OnTextAvailable()
        {
            // If the TextAvailable event is being handled, raise it.
            TextAvailable?.Invoke(this, EventArgs.Empty);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Raise the TextAvaialble event.
            OnTextAvailable();
        }
    }
}

Form3:
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        public void DisplayText(string text)
        {
            // Display the specified text on the Label.
            label1.Text = text;
        }
    }
}

Form1:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Form2 f2;
        private Form3 f3;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Display an MDI child that can notify of text available.
            f2 = new Form2 {MdiParent = this};
            f2.TextAvailable += F2_TextAvailable;
            f2.FormClosed += F2_FormClosed;
            f2.Show();

            // Display a separate form that can display specified text.
            f3 = new Form3();
            f3.Show();
        }

        private void F2_TextAvailable(object sender, EventArgs e)
        {
            // The MDI child has text available so get it and send it to the separate form.
            f3.DisplayText(f2.AvailableText);
        }

        private void F2_FormClosed(object sender, FormClosedEventArgs e)
        {
            // Remove the event handlers from the form when it closes.
            f2.TextAvailable -= F2_TextAvailable;
            f2.FormClosed -= F2_FormClosed;
        }
    }
}

To run that code you will need a project with three forms. Form1 will need its IsMdiParent property set to true, Form2 will a TextBox and a Button and Form3 will need a Label. If you copy and paste the code, remember to attach the handler for the Click event of the Button in Form2. You can then run the project, type into the TextBox and click the Button in the MDI child and you'll see the text you typed appear on the Label in the separate form.

Note that the critical thing here is that Form1 knows about the Form2 and Form3 instances because it creates both of them but they don't know about each other and it should stay that way. Form1 has a reference to each of them so it can communicate with both.
 

MediaHub

New member
Joined
Dec 8, 2016
Messages
2
Programming Experience
Beginner
yes similar to this, but i a trying to access procedure of Form 3 from MDIChid, there is nothing to do with Parent form in this case. Any Way i found the solution on Microsoft Forum. Thank you for response.


C#:
var iForm3= Application.OpenForms.OfType<Form3>().Single();

iForm3.MethodOnForm3();
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,720
Location
Sydney, Australia
Programming Experience
10+
yes similar to this, but i a trying to access procedure of Form 3 from MDIChid, there is nothing to do with Parent form in this case. Any Way i found the solution on Microsoft Forum. Thank you for response.


C#:
var iForm3= Application.OpenForms.OfType<Form3>().Single();

iForm3.MethodOnForm3();

It's like you didn't even read what I posted. There IS something to do with the main parent form because it is the one that knows about the other two. I said that there's the proper way to do it and there's other ways. You chose another way. It's certainly possible, simple and it works in particular cases but it is not what would be considered best practice. If you want to write good code, you'll avoid things like that.
 
Top Bottom