简体   繁体   中英

Preventing dialog on Form1 from blocking interaction with Form2?

I am working with a WinForm app that has two forms. The first form is the main form with all the logic. The second form holds a browser control and accesses an internal web page based on data passed from Form1. The web page can then be interacted with. The problem arises when a MessageBox is popped on Form1 the interaction is blocked on Form2.

Is there a way to enable interaction of Form2 before the MessageBox is answered?

OpenBrowser(docIDs, txtID.Text)
 Me.Activate()
 resultYESNO = MessageBox.Show(Me, questionText, "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
         If resultYESNO = DialogResult.Yes Then
           columnValue = "Y"
          ElseIf resultYESNO = DialogResult.No Then
           columnValue = "N"
           End If

The OpenBrowser Sub:

Private Sub OpenBrowser(ByVal docIDs As List(Of String), ByVal ID As String)
    If Not Application.OpenForms().OfType(Of Browser).Any Then
        Dim browser = New Browser()
    End If
    Dim encodeIDs As String
    encodeIDs = String.Join(",", docIDs.ToArray())
    Dim barray As Byte() = System.Text.Encoding.UTF8.GetBytes(encodeIDs)
    Dim encodedIDs = System.Convert.ToBase64String(barray)
    Dim url = ConfigurationManager.AppSettings("MyBrowserPath")
    Browser.WebBrowser1.Url = New Uri(url & encodedIDs)
    Dim area = Screen.PrimaryScreen.WorkingArea
    Dim width = CInt(area.Width / 2)
    Dim height = CInt(area.Height)
    Browser.Width = width
    Browser.Height = 800
    Browser.SetDesktopLocation(width, 0)
    Browser.Show()
    Browser.BringToFront()
    Browser.Activate()
End Sub

The following example shows how you can create different UI threads and show different forms on different threads. Then modal dialog forms are modal in the thread which has created them:

Imports System.Threading
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i = 1 To 2
            Dim index = i
            Dim t = New Thread(
                Sub()
                    Dim f = New Form With {.Text = $"Form {index}"}
                    Dim b = New Button With {.Text = "Click Me."}
                    AddHandler b.Click,
                        Sub()
                            Using d As New Form()
                                d.StartPosition = FormStartPosition.CenterParent
                                d.Size = New Drawing.Size(100, 100)
                                d.ShowDialog()
                            End Using
                        End Sub
                    f.Controls.Add(b)
                    Application.Run(f)
                End Sub)
            t.SetApartmentState(ApartmentState.STA)
            t.IsBackground=True
            t.Start()
        Next
    End Sub
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM