Results 1 to 9 of 9

Thread: [RESOLVED] How to track open winforms and notify main form when closing child form?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    3

    Resolved [RESOLVED] How to track open winforms and notify main form when closing child form?

    Hello everyone. I am new to Visual Basic. I am starting with a Address Book program. Keep a list of contacts, birthdays, phone numbers, emails, etc. My main form (OverviewWin) has two panels. The first panel has 5 Buttons to open other windows (PeopleWin, CalendarWin, NotesWin, etc). I have it set so one instance of each form can only be opened one at a time. So there can not be two open instances of CalendarWin but all child windows can be opened at the same time.

    I am trying to figure out how to keep track of what windows are open. When I click the Button on OverviewWin to open CalendarWin, I change the font on that button to Bold. When I close the CalendarWin form, I would like to change the font on that Button back to Regular. OverviewWin will not always be the Active form when I close a window. For example, I could be writing something on NotesWin, then click the X on CalendarWin to close it. I'm using Windows 10 so I could also close CalendarWin from the Taskbar.

    What would be the best way to track what forms are open or closed. Also how would I go about letting OverviewWin know to change the text on a Button when another form is closed?

    I am new to this so please understand if this is a beginner question. I am not sure what code I should include to show what I am trying to do. If I need to provide more information or some code please let me know. If you can help please keep in mind my newness to this

    Thank you.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to track open winforms and notify main form when closing child form?

    Simplest way, in my opinion, would be to add a module and add some public variables to keep track of the windows.
    When a window is close, you could update the shared variable in the closing window's Form_Closing event.

    If you wanted something where you didn't need to add unique code to each window (i.e. hardcoded variable access to each form), you might want to update the form's New method to pass a parameter to the form, i.e. tagging it in some way and when the form closes it calls a known shared method passing the tag back.

    There are a lot of other options, probably better than the two above, but those are the first two that come to mind for me.

    {p.s. Tested a third variation in the next post}
    Last edited by passel; Jun 19th, 2018 at 11:38 AM.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to track open winforms and notify main form when closing child form?

    Just as a quick test out of curiosity, I added two buttons to a form, and a little bit of code in a Module.
    The first button just launches another instance of the first form.
    You can launch additional instance from the original form, or the other forms, doesn't really matter.

    If you click the second button it will print in the debug window whether you have open forms or not.
    Be careful to not close the original form. If you close all the additional forms, you should see it prints False when you call the areFormsOpen function.

    Form Code
    Code:
    Public Class Form1
    
      Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Remove(Me)
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form1
        Launch(frm)
      End Sub
    
      Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Debug.Print("Forms Open = {0}", areFormsOpen.ToString)
      End Sub
    End Class
    Module Code
    Code:
    Module Module1
      Private ListOfOpenForms As New List(Of Form)
    
      Public Function areFormsOpen() As Boolean
        If ListOfOpenForms.Count > 0 Then
          Return True
        Else
          Return False
        End If
      End Function
    
      Public Sub Launch(frm As Form)
        ListOfOpenForms.Add(frm)
        frm.Show()
      End Sub
    
      Public Sub Remove(frm As Form)
        ListOfOpenForms.Remove(frm)
      End Sub
    End Module

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    3

    Re: How to track open winforms and notify main form when closing child form?

    passel - Thank you for the example code and explanation. It looks like exactly what I need. I will test when I get home tonight. That module is beyond my understanding at this point but it gives me things to search about and hopefully from which to learn.

    Thank you again.

    Quote Originally Posted by passel View Post
    Just as a quick test out of curiosity, I added two buttons to a form, and a little bit of code in a Module.
    The first button just launches another instance of the first form.
    You can launch additional instance from the original form, or the other forms, doesn't really matter.

    If you click the second button it will print in the debug window whether you have open forms or not.
    Be careful to not close the original form. If you close all the additional forms, you should see it prints False when you call the areFormsOpen function.

    Form Code
    Code:
    Public Class Form1
    
      Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Remove(Me)
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim frm As New Form1
        Launch(frm)
      End Sub
    
      Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Debug.Print("Forms Open = {0}", areFormsOpen.ToString)
      End Sub
    End Class
    Module Code
    Code:
    Module Module1
      Private ListOfOpenForms As New List(Of Form)
    
      Public Function areFormsOpen() As Boolean
        If ListOfOpenForms.Count > 0 Then
          Return True
        Else
          Return False
        End If
      End Function
    
      Public Sub Launch(frm As Form)
        ListOfOpenForms.Add(frm)
        frm.Show()
      End Sub
    
      Public Sub Remove(frm As Form)
        ListOfOpenForms.Remove(frm)
      End Sub
    End Module

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How to track open winforms and notify main form when closing child form?

    I'd use the Form's FormClosed Handlers, like this...

    Code:
    Public Class OverviewWin
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'contacts
            RemoveHandler PeopleWin.FormClosed, AddressOf PeopleWin_FormClosed
            PeopleWin.Show()
            Button1.Font = New Font(Button1.Font, Button1.Font.Style Or FontStyle.Bold)
            AddHandler PeopleWin.FormClosed, AddressOf PeopleWin_FormClosed
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'birthdays
            RemoveHandler CalendarWin.FormClosed, AddressOf CalendarWin_FormClosed
            CalendarWin.Show()
            Button2.Font = New Font(Button2.Font, Button2.Font.Style Or FontStyle.Bold)
            AddHandler CalendarWin.FormClosed, AddressOf CalendarWin_FormClosed
        End Sub
    
        Private Sub PeopleWin_FormClosed(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Button1.Font = New Font(Button1.Font, Button1.Font.Style And Not FontStyle.Bold)
        End Sub
    
        Private Sub CalendarWin_FormClosed(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Button2.Font = New Font(Button2.Font, Button2.Font.Style And Not FontStyle.Bold)
        End Sub
    
    End Class
    Last edited by .paul.; Jun 19th, 2018 at 03:09 PM.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How to track open winforms and notify main form when closing child form?

    @Passel

    Why not use Application.OpenForms?

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to track open winforms and notify main form when closing child form?

    Quote Originally Posted by .paul. View Post
    @Passel

    Why not use Application.OpenForms?
    Quote Originally Posted by passel View Post
    ...
    There are a lot of other options, probably better than the two above, but those are the first two that come to mind for me.
    ...
    I didn't do any searching, and I know I don't know what I don't know, so tried to leave myself an out.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: How to track open winforms and notify main form when closing child form?

    Quote Originally Posted by passel View Post
    I didn't do any searching, and I know I don't know what I don't know, so tried to leave myself an out.
    Well played. We all know there's more to know. Personally i wouldn't bother with either Application.OpenForms or a list of forms, if it's just a case of catching FormClosed

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2018
    Posts
    3

    Re: How to track open winforms and notify main form when closing child form?

    passel and .paul -

    Thank you both for your help. Both approaches made me do some work to try (note the word *try*) to figure out what you were doing. Excellent learning for a beginner such as myself. Using the FormClosed worked and allowed me to set the buttons the way I was hoping. I know I will be changing the layout eventually but each step forward is a good thing.

    Thank you both for your help and for the examples. Very much helpful and appreciated.

Posting Permissions

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



Click Here to Expand Forum to Full Width