Unloading all Forms in VB.NET-VBForums
Results 1 to 6 of 6

Thread: Unloading all Forms in VB.NET

  1. #1

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,140

    Exclamation Unloading all Forms in VB.NET

    a bit of messing and i created this simple code to allow you to unload all open forms before closing the App's main form in VB.NET
    in a Module :
    VB Code:
    1. Module Module1
    2.  
    3.     Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    4.     Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    5.     Private Const WM_CLOSE As Integer = CInt(&H10)
    6.  
    7.     Public Function CloseForms(ByVal forms() As String) As Boolean
    8.         Dim x As Integer
    9.         Dim i As Integer
    10.         For i = LBound(forms) To UBound(forms)
    11.             x = FindWindow(vbNullString, forms(i))
    12.             If Not x = 0 Then
    13.                 PostMessage(x, WM_CLOSE, 0, 0) '/// close the Form.
    14.             End If
    15.         Next
    16.         Return True
    17.  
    18.     End Function
    19.  
    20. End Module

    in your Main Form ( eg: Form1 ) :
    VB Code:
    1. Private frms() As Form = {New Form2(), New Form3()} '/// all forms in the Application apart from Form1.
    2.     Private frmNames(1) As String '/// this will house the window caption of the forms.
    3.  
    4.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         Dim i As Integer
    6.  
    7.         For i = LBound(frms) To UBound(frms)
    8.             frmNames(i) = frms(i).Text
    9.         Next
    10.     End Sub
    11.  
    12.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    13.         If CloseForms(frmNames) = True Then
    14.             Me.Close()
    15.         End If
    16.  
    17.     End Sub
    18.  
    19.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.         frms(0).Show()
    21.         frms(1).Show()
    22.         MyBase.BringToFront()
    23.     End Sub

    i've included a demo sample project ...
    Attached Files Attached Files
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  2. #2
    Lively Member
    Join Date
    Jun 2001
    Posts
    107
    Does the same logic apply to disabling all of the controls on a form?

    Thanks,
    Mike

  3. #3

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,140

    Exclamation

    sorry for the delay , nope it's not for controls , just unloading forms.
    anyway , i have created a much better method for unloading all open forms ...
    VB Code:
    1. [color=blue]Private Declare Function[/color] FindWindow [color=blue]Lib[/color] "user32.dll" Alias "FindWindowA" ([color=blue]ByVal[/color] lpClassName [color=blue]As String[/color], [color=blue]ByVal[/color] lpWindowName [color=blue]As String[/color]) [color=blue]As Integer[/color]
    2. [color=blue]Private Declare Function[/color] PostMessage [color=blue]Lib[/color] "user32.dll" Alias "PostMessageA" ([color=blue]ByVal[/color] hwnd [color=blue]As Integer[/color], [color=blue]ByVal[/color] wMsg [color=blue]As Integer[/color], [color=blue]ByVal[/color] wParam [color=blue]As Integer[/color], [color=blue]ByVal[/color] lParam [color=blue]As Integer[/color]) [color=blue]As Integer[/color]
    3.  
    4. [color=blue]Private[/color] frm2 [color=blue]As New[/color] Form2()
    5.  
    6. [color=blue]Private Sub[/color] Form1_Load([color=blue]ByVal[/color] sender [color=blue]As Object[/color], [color=blue]ByVal[/color] e [color=blue]As[/color] System.EventArgs) [color=blue]Handles MyBase[/color].Load
    7.     frm2.Show()
    8.     [color=blue]Me[/color].BringToFront()
    9. [color=blue]End Sub[/color]
    10.  
    11. [color=blue]Private Sub[/color] Button1_Click([color=blue]ByVal[/color] sender [color=blue]As[/color] System.Object, [color=blue]ByVal[/color] e [color=blue]As[/color] System.EventArgs) [color=blue]Handles[/color] Button1.Click
    12. [color=blue]Dim[/color] objType [color=blue]As[/color] Type() = Reflection.Assembly.GetExecutingAssembly.GetTypes()
    13. [color=blue]Dim[/color] x [color=blue]As Integer[/color], i [color=blue]As Integer[/color]
    14.     [color=blue]Try[/color]
    15.         [color=blue]For[/color] x = [color=blue]LBound[/color](objType) [color=blue]To UBound[/color](objType)
    16.             [color=blue]If Not[/color] objType(x).Name = [color=blue]Me[/color].Name [color=blue]Then[/color] '/// make sure you dont unload this form yet.
    17.                 i = FindWindow([color=blue]vbNullString[/color], objType(x).Name)
    18.                 PostMessage(i, [color=blue]CInt[/color](&H10), [color=blue]vbNullString[/color], [color=blue]vbNullString[/color])
    19.             [color=blue]End If[/color]
    20.         [color=blue]Next[/color]
    21.     [color=blue]Catch[/color] ex [color=blue]As[/color] Exception
    22.         MessageBox.Show("Oops, the following error occured:" & ex.Message)
    23.     [color=blue]Finally[/color]
    24.         [color=blue]MyBase[/color].Close() '/// now that all the other forms are closed , unload this one.
    25.     [color=blue]End Try[/color]
    26. [color=blue]End Sub[/color]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,140
    Oops forgot to add the attachment sample code .zip
    Attached Files Attached Files
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5
    Banned
    Join Date
    Jul 2003
    Location
    New delhi
    Posts
    143
    to unload all aplication form in vb.net just use


    application.exit()


    das

  6. #6

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,140
    Application.Exit() does NOT raise the Form.Close() event and can leave processes of a Form running in the memory ( thus is classed as unsafe ) , before running Application.Exit() you must use Form.Close() on each Form. here's a quote direct from MSDN ...
    CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.
    it's bad practise in VB.NET to just use Application.Exit() or End.
    but if you wish to use unsafe methods which are NOT recommended by microsoft , who am i to argue
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.