Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have made a application on windows forms and made mdi form and child forms. I have a reader and its connection has to be closed on form close. I have managed it for Form CLosed Event. But When its closed from mdi form its not.
I want to code on the event of close when closed from the mdi main form. Ihave used this code in mdi :

C#
private Form lastForm;

        private void showForm(Form frm)
        {
            frm.FormClosed += (sender, ea) =>
            {
                if (object.ReferenceEquals(lastForm, sender)) lastForm = null;
            };
            frm.MdiParent = this;
            frm.Show();
            if (lastForm != null) lastForm.Close();
            lastForm = frm;
        }

        private void AddTrayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForm(new Add_Tray());
        }

        private void TrayWiseInvenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForm(new InventoryTrayWise());
        }


Where to put my code for connection close so that when the the form is closed by the mdi that event performs.
Posted
Comments
Gaurav Makwana 18-Dec-13 4:14am    
just see the click event of menustrip then write this code its will work
kool15th 18-Dec-13 12:35pm    
On click event only I have written the codes to show next form But The Closing of reader is not working their which has to be closed on every form close called.
Gaurav Makwana 18-Dec-13 23:48pm    
use the model and modelless dailogue
showdailouge for it may be its usefull
BillWoodruff 19-Dec-13 19:04pm    
To respond to your question, I'd have to know where the 'reader is defined, what type of 'reader it is, and how it's used.
kool15th 21-Jan-14 9:30am    
For reader i have made classes and currently the reader connection variables can't be accessed from other forms. So I can't code on menu strip's click event.

1 solution

Sergey Alexandrovich Kryukov wrote:
Here is the best way of using MDI child: never using MDI.


Have a look at His past answers: who needs MDI, ever? Why torturing yourself and scaring off your users?[^]
 
Share this answer
 
Comments
BillWoodruff 19-Dec-13 19:02pm    
Hi Maciej, I find MDI apps butt-ugly :), but the fact is many programmers have to work on them; and many students are assigned to write apps using MDI.
Maciej Los 20-Dec-13 1:49am    
OK. So, what you suggest?
;)
BillWoodruff 20-Dec-13 9:44am    
Since suicide is not an option for most people :), I'd say that we ... you and I ... make our own personal choice about whether or not to respond to questions about MDI. My choice is to respond ... sometimes ... depending on the question.
Maciej Los 21-Dec-13 16:34pm    
That was my first answer about MDI... and it's probably not the last one :laugh:
We have many options chossing when to answer or not, but some students not ;)
I have no idea why some professors still tortuew their students force them to use MDI.

Cheers Bill!
Have a Happy Christmas and - as we say in Poland - rich Santa Claus!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900