Windows重复打开

时间:2014-04-02 19:02:29

标签: vb.net winforms

Visual Studio 2010,Visual Basic .NET

我有一个表单(frmImages),我正在使用它作为一个类。 所以我使用frmImages类创建一个新表单,我得到了重复的窗口。 不知道为什么表单会打开两次。

Dim images As New frmImages
images.wbImages.Navigate(New Uri("path here"))
images.Text = "Form Title"
images.Show()

更新 我已确定此问题是由于鼠标按下事件造成的。不知道如何纠正这个问题,因为我需要鼠标按下事件来获取基于鼠标位置的html元素。以下是此部分的所有部分。

Private Sub webBrowser1_DocumentCompleted(sender As System.Object, e As System.EventArgs) Handles wbContent.DocumentCompleted

        AddHandler wbContent.Document.MouseDown, AddressOf eventSub
End Sub

Private Sub eventSub(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs)

   Dim event_html As New HtmlElementEventHandler(AddressOf wbMouseDown)
   event_html.Invoke(sender, e)

End Sub

Public Sub wbMouseDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)

    If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Left Then

        bookmarkElement = wbContent.Document.GetElementFromPoint(e.ClientMousePosition)
        If bookmarkElement.GetAttribute("className").ToString().IndexOf("figureLink ") <> -1 Then
            Dim images As New frmImages
            images.wbImages.Navigate(New Uri(bookmarkElement.GetAttribute("src").ToString()))
            images.Text = "Form Title"
            images.Show()
         End If
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

您正在打开该表单的新实例。 VB并不关心;你可能有一百万个表格的实例,你仍然可以打开更多(不是一百万,但你得到我的观点)。当你创建一个新的实例时,它就是一个新的实例。

答案 1 :(得分:0)

尝试添加此项以防止其打开其他表单

Public Class frmImages
    Inherits Form
    Private _instance As frmImages = Nothing
    Private _lock As New Object()

    Private Sub New()
    End Sub


    Public Shared ReadOnly Property Instance() As frmImages
        Get
            If _instance Is Nothing Then
                SyncLock _lock
                    If _instance Is Nothing Then
                        _instance = New frmImages()
                    End If
                End SyncLock
            End If
            Return _instance
        End Get
    End Property
End Class