【问题标题】:Preventing dialog on Form1 from blocking interaction with Form2?防止 Form1 上的对话框阻止与 Form2 的交互?
【发布时间】:2018-12-19 15:41:21
【问题描述】:

我正在使用具有两个表单的 WinForm 应用程序。第一种形式是具有所有逻辑的主要形式。第二个表单包含一个浏览器控件并根据从 Form1 传递的数据访问内部网页。然后可以与网页进行交互。当在 Form1 上弹出 MessageBox 时出现问题,Form2 上的交互被阻止。

有没有办法在 MessageBox 被应答之前启用 Form2 的交互?

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

OpenBrowser 子:

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

【问题讨论】:

  • 我们昨天不是已经问过这个了吗?
  • 那是关于试图用“假”对话框替换对话框。这是在寻找不同的方法。
  • 那你为什么在提问之前打开浏览器表单?
  • 在回答问题之前需要审核网络表单中的数据
  • 很久以前,在一个遥远的星系里,你曾经可以设置旧的 MsgBox 的模态。现在唯一的选择是 ApplicationModal。

标签: .net vb.net winforms showdialog


【解决方案1】:

以下示例展示了如何创建不同的 UI 线程并在不同的线程上显示不同的表单。然后模态对话框表单在创建它们的线程中是模态的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    相关资源
    最近更新 更多