Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hey again guys! I have a problem.. When i open my second form where i can add things to a listbox in form1 the second form is closing like this:
 
1. Open the second form
2. Add what i want to add
3. Close...
 
I don't want the form2 to close i want it to stay so i can add more items at same time to the listbox. How can i do this?
Posted 30-Apr-13 8:03am
Comments
ryanb31 at 30-Apr-13 13:20pm
   
You likely have code that is closing it.
ExpertITM at 30-Apr-13 13:28pm
   
How you add two form in one page?
Maciej Los at 30-Apr-13 13:56pm
   
Any error occurs?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

OK, don't close it then. This is normally done be ether removing of the close box, or by capturing the event of closing the form and cancellation of its handling, usually hiding the form instead. To do so, you need to handle the event System.Windows.Forms.Form.FormClosing:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx[^].
 
Or, alternatively, by overriding of the virtual method System.Windows.Forms.Form.OnFormClosing:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx[^].
 
In these cases, fist check up System.Windows.Forms.FormClosingEventArgs.CloseReason. If it is System.Windows.Forms.CloseReason.UserClosing, set System.Windows.Forms.FormClosingEventArgs.Cancel to false. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx[^].
 
I think is should be quite clear now.
 
However, I would not recommend using multiple form (I don't count modal forms, but even the number of modal forms should be very limited). There are more other problems, forget user convenience. It's much better to live with only one, main form, pretty much like Visual Studio does. Instead of forms, you can put content on many panels which you can show and hide, all kind of accordion-style panels, docking panels (advanced design, like in Visual Studio) or, very simple and clear approach, TabControl.
 
—SA
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 CPallini 155
3 PIEBALDconsult 150
4 BillWoodruff 148
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100