Creating a global object for a zoom function

I have a form which displays actions and a timeline for each action. On the timeline there can be a block, marking from when to when a certain action has taken place.

Now I want to create a zoom form that will pop-up when the user doubleclicks on the timeline. As you can see, the user can either doubleclick on the timeline or on a block. The zoom form showS the exact same information as the timeline, only bigger. The problem is that I need some kind of global object for this zoom form. As you have probably guessed, on the mouse_move event of the timeline and that of the block, a bunch of setfuctions have to be called on the zoomwindow.

What I do know is I create a zoomform when I create my mainform, but I don't call the show method. Then when a user doubleclicks I call the zoomform.show method. Problem is that when the user closes this form and then doubleclicks again I get an error telling me I can't access disposed objects.

Any help would be appreciated!

This is a drawing of my application:

                  TIMELINE
Actions|0      1      2      3      4      5      6      7...
------------------------------------------------------------------------------------------------
Action1|          XXXXXXXXXXX
Action2|                        XXXXXXXXXXXXXXXX
Action2|                                                    XXXXXXXXXXXXXXXXXXXXXXXXX
LVL 5
tsayAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Change ShowZoom() to:

    Public Sub ShowZoom()
        If Me.frmZoom Is Nothing Then
            Me.frmZoom = New ZoomWindow
        End If
        Me.frmZoom.Show()
    End Sub

You don't need CreateZoom() then...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is a generic answer to your problem.  Handle the Closing() event and set your global variable to Nothing:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents f2 As Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If f2 Is Nothing Then
            f2 = New Form2
        End If
        f2.Show()
    End Sub

    Private Sub f2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles f2.Closing
        f2 = Nothing
    End Sub

End Class
0
 
tsayAuthor Commented:
Hey Idle Mind,

I tried your solution but the same problem remains. Here's what happens:

I have a global class, called zoomfunctions and looks like this:

Public Class ZoomFunctions
    Private WithEvents frmZoom As ZoomWindow
    Public Sub CreateZoom()
        If Me.frmZoom Is Nothing Then
            Me.frmZoom = New ZoomWindow
        End If
    End Sub

    Public Sub ShowZoom()
        Me.frmZoom.Show()
    End Sub

    Public Sub SetDayToShow(ByRef plDay As Day)
        If Me.frmZoom Is Nothing Then
            MsgBox("frmzoom is nothing :(") '<= second time I get this message
        End If
        Me.frmZoom.ZoomDetail.DayToShow = plDay
    End Sub

    Public Sub SetHour(ByVal piHour As Integer)
        Me.frmZoom.ZoomDetail.Hour = piHour
    End Sub

    Private Sub frmZoom_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles frmZoom.Closing
        Me.frmZoom = Nothing
    End Sub
End Class

The CreateZoom is called when my main form loads.
The Showzoom is called when I doubleclick the object I want to zoom.
The SetDayToShow is called on the MouseOver of the object that potentially can be zoomed.
First time everything works, second time I get the Frmzoom is nothing :( message.

Tnx for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.