Creating a global object for a zoom function

Posted on 2006-04-14
Last Modified: 2010-04-23
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 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:

Actions|0      1      2      3      4      5      6      7...
Action1|          XXXXXXXXXXX
Action2|                        XXXXXXXXXXXXXXXX
Action2|                                                    XXXXXXXXXXXXXXXXXXXXXXXXX
Question by:tsay
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
        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
    LVL 5

    Author Comment

    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()
        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!
    LVL 85

    Accepted Solution

    Change ShowZoom() to:

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

    You don't need CreateZoom() then...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now