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.