|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Close all forms but MainMy application has a main form from which the user can use the Menu to open
other forms. Each form opens in a separate window while the main form remains open all the time. Is their any code I can place in Form_Load for all my other forms (besides Main) that will close all other open forms (if any exist) except for the Main form? Something like For Each Form in Windows.Forms?? What's the code?' Thanks, Ryan Check out the Application.OpenForms Property.
Tony Show quoteHide quote "Ryan" wrote: > My application has a main form from which the user can use the Menu to open > other forms. Each form opens in a separate window while the main form > remains open all the time. Is their any code I can place in Form_Load for > all my other forms (besides Main) that will close all other open forms (if > any exist) except for the Main form? Something like For Each Form in > Windows.Forms?? What's the code?' > > Thanks, > Ryan > > > Hi Ryan,
Yes, you can go through the Application.OpenForms collection and close all of them, except the main form. You can check the Form.Text property for the main form. Kevin Yu Microsoft Online Community Support ============================================================================ ========================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ============================================================================ ========================== (This posting is provided "AS IS", with no warranties, and confers no rights.) I tried this code and I get an error everytime a Form closes because the
objects in My.Application.OpenForms change within the for loop. Private Sub Application_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each f As Form In My.Application.OpenForms If f.Name <> "MainForm" Then f.Close() End If Next End Sub Show quoteHide quote "Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message news:vT5P1tjnGHA.4612@TK2MSFTNGXA01.phx.gbl... > Hi Ryan, > > Yes, you can go through the Application.OpenForms collection and close all > of them, except the main form. You can check the Form.Text property for > the > main form. > > Kevin Yu > Microsoft Online Community Support > > ============================================================================ > ========================== > When responding to posts, please "Reply to Group" via your newsreader so > that others may learn and benefit from your issue. > ============================================================================ > ========================== > > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > Hi Ryan,
You can take advantage of GoTo and Try/Catch block. Not very graceful, but it does work. :-) Try a: For Each f As Form In My.Application.OpenForms If f.Name <> "Form1" Then f.Close() End If Next Catch GoTo a End Try Kevin Yu Microsoft Online Community Support ============================================================================ ========================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ============================================================================ ========================== (This posting is provided "AS IS", with no warranties, and confers no rights.) Dump the goto:
dim Success as Boolean do success = true try for each f as form in my.application.openforms if f.name <> "Form1" then f.close next f catch ex as exception success = false end try loop until success With structured exception handling, Goto should generate a compiler warning "Warning: Speghetti Code using Goto". Since you can jump an arbritrary number of levels up via try ... catch blocks, there is no longer any need in the VB language for Goto. Mike Ober. Show quoteHide quote "Kevin Yu [MSFT]" <v-k***@online.microsoft.com> wrote in message news:N8sDvOJpGHA.2024@TK2MSFTNGXA01.phx.gbl... > Hi Ryan, > > You can take advantage of GoTo and Try/Catch block. Not very graceful, but > it does work. :-) > > Try > a: > For Each f As Form In My.Application.OpenForms > > If f.Name <> "Form1" Then > > f.Close() > > End If > > Next > > Catch > GoTo a > End Try > > Kevin Yu > Microsoft Online Community Support > > ============================================================================ > ========================== > When responding to posts, please "Reply to Group" via your newsreader so > that others may learn and benefit from your issue. > ============================================================================ > ========================== > > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > > Thanks, Michael! Looks better.
Kevin Yu Microsoft Online Community Support ============================================================================ ========================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ============================================================================ ========================== (This posting is provided "AS IS", with no warranties, and confers no rights.) |
|||||||||||||||||||||||