0

I have 2 Mdi Child forms (form2 and form3) that opens on a panel tool in parent (form1), on one child(form3) I have a UDP socket , when I open the other child(form2) from parent the UDP socket remains open ,"on form closing" event handler does not trigger, since I haven't properly exited the child, So when I reopen it I get Error "Only one usage of each socket address (protocol/network address/port) is normally permitted " Even if Parent may detect that child is closing, How can it close the socket (udpClient.Close(); ) that's only available on child(form3).

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (ActiveMdiChild != null)
            ActiveMdiChild.Close();
        panel1.Controls.Clear();
        panel1.Invalidate();

        Form2 newMDIChild = new Form2();

        newMDIChild.TopLevel = false;
        newMDIChild.AutoScroll = true;
        this.panel1.Controls.Add(newMDIChild);
        newMDIChild.Show();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (ActiveMdiChild != null)
            ActiveMdiChild.Close();
        panel1.Controls.Clear();
        panel1.Invalidate();
        Form3 newMDIChild = new Form3();
        newMDIChild.TopLevel = false;
        newMDIChild.AutoScroll = true;
        this.panel1.Controls.Add(newMDIChild);
        newMDIChild.Show();
        Form3 mdiChild = new Form3();
   }
}

the above code only clears the panel from the previous child but not whatever socket or com port happened to be opened at the particular child, before opening a new child on the panel.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
yonas mekonen
  • 51
  • 1
  • 7
  • 2
    "OnFormClosing is not triggered" or "udpClient.Close() not working" ? If you can also provide your code, it would be better. –  Jul 01 '16 at 00:01
  • WinForms? WPF? ASP.NET? – Uwe Keim Jul 01 '16 at 14:48
  • 1
    Why are you calling your forms MDI when you are using them as UserControls? `.Controls.Clear();` does not dispose of those controls that are cleared. – LarsTech Jul 01 '16 at 14:55
  • It's Winforms app. I have just used panel1.Controls.Clear(); If it probably helps ,but apparently it isn't .The catch though is simply finding a way to close sockets on udp ports ,or generally to call some kind of a event handler before leaving a child , while another child is being called by button click on parent. – yonas mekonen Jul 01 '16 at 15:47
  • Forms don't have closing events when they are UserControls. When you set TopLevel = false, you effectively made your forms UserControls. – LarsTech Jul 01 '16 at 15:54
  • They don't have closing event ,but I need my child forms in a parent panel ,that's my problem ,well is there any kind of custom event handler like this one "http://stackoverflow.com/questions/33942937/how-do-i-pass-data-from-child-of-child-form-to-parent-form-in-c?rq=1" – yonas mekonen Jul 01 '16 at 16:30
  • The parent container has a ControlRemoved event. – LarsTech Jul 01 '16 at 16:34

0 Answers0