0

In my project (kind of a screen saver) I have a main form and second form (PicView). I want to open many instances of the second form and display a picture in it. I can do that fine, but when I have opened e.g. 4 forms I would like to close the first before opening the next one so that the total number of forms stays at 4. The problem I have is that the closing routine does not know about the form instances and the debugger tells me to create a new instance, but I would like to use the existing ones. How can I make the form names visible to the main routine or do I need to pass reference to the created forms?

Thanks

Private Sub btnTest_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
    gsPic1 = "D:\My Stuff\411CANON\IMG_1161.JPG"
    gsPic2 = "D:\My Stuff\411CANON\IMG_1167.JPG"
    gsPic3 = "D:\My Stuff\411CANON\IMG_1174.JPG"
    gsPic4 = "D:\My Stuff\411CANON\IMG_1178.JPG"


    ScreenSavePicIndex(gsPic1, 1)
    Application.DoEvents()
    ScreenSavePicIndex(gsPic2, 2)
    Application.DoEvents()
    ScreenSavePicIndex(gsPic3, 3)
    Application.DoEvents()
    ScreenSavePicIndex(gsPic4, 4)

    MsgBox("Now Closing 1")
    CloseScreenSaverPic(1)
    Application.DoEvents()
    MsgBox("Now Closing 3")
    CloseScreenSaverPic(3)
    Application.DoEvents()
    MsgBox("Now Closing 4")
    CloseScreenSaverPic(4)
    Application.DoEvents()
End Sub
Private Sub CloseScreenSaverPic(ByVal iIndex As Integer)
    Dim frmPicViewerScrSav(iIndex) As PicView
    frmPicViewerScrSav(iIndex) = New PicView
    frmPicViewerScrSav(iIndex).Close()
End Sub

Private Sub ScreenSavePicIndex(ByVal sFilePathandName As String, iIndex As Integer)


    Dim iTargetHeight As Integer
    Dim iTargetWidth As Integer
    Dim dFactorHeight As Double
    Dim dFactorWidth As Double




    Dim objImage As System.Drawing.Image
    Try
        objImage = System.Drawing.Image.FromFile(sFilePathandName)
    Catch ex As Exception
        objImage = Nothing
    End Try

    Dim frmPicViewerScrSav(iIndex) As PicView
    frmPicViewerScrSav(iIndex) = New PicView
    Dim dFactor As Double
    dFactor = 1

    frmPicViewerScrSav(iIndex).FormBorderStyle = Windows.Forms.FormBorderStyle.None


    iTargetWidth = Screen.PrimaryScreen.Bounds.Width / giScreenSavePicSize
    iTargetHeight = Screen.PrimaryScreen.Bounds.Height / giScreenSavePicSize



    'Check if the pic is bigger than what we want to display

    If objImage.Width > iTargetWidth Then
        'if it is wider then we need to find out how much bigger it is by factor
        dFactorWidth = iTargetWidth / objImage.Width
    End If

    If objImage.Height > iTargetHeight Then
        'if it is higher then we need to find out how much bigger it is by factor
        dFactorHeight = iTargetHeight / objImage.Height
    End If

    If dFactorWidth > dFactorHeight Then
        dFactor = dFactorWidth
    Else
        dFactor = dFactorHeight
    End If

    'Console.WriteLine("Factor is: " & dFactor)

    frmPicViewerScrSav(iIndex).Width = objImage.Width * dFactor
    frmPicViewerScrSav(iIndex).Height = objImage.Height * dFactor
    objImage.Dispose()





    Dim r As New Random()

    Dim x As Integer = r.Next(Screen.PrimaryScreen.Bounds.Width - frmPicViewerScrSav(iIndex).Width)
    Dim y As Integer = r.Next(Screen.PrimaryScreen.Bounds.Height - frmPicViewerScrSav(iIndex).Height)
    Dim p As New Point(x, y)
    'Console.WriteLine("Putting it at x= " & x & ", y= " & y)

    frmPicViewerScrSav(iIndex).Location = p




    frmPicViewerScrSav(iIndex).PictureBox1.Hide()

    frmPicViewerScrSav(iIndex).PictureBox1.ImageLocation = sFilePathandName
    frmPicViewerScrSav(iIndex).PictureBox1.SizeMode = PictureBoxSizeMode.Zoom


    frmPicViewerScrSav(iIndex).PictureBox1.Show()
    frmPicViewerScrSav(iIndex).Show()
End Sub
4

0 回答 0