【问题标题】:Having trouble with show/hide/visible in VB .net It only works the first time在 VB .net 中显示/隐藏/可见时遇到问题 它只在第一次工作
【发布时间】:2018-10-31 02:24:19
【问题描述】:

我有一个多表单应用程序即将完成,但我遇到了一个大问题。 我需要保持一个主窗体打开,基本上是一个通信任务,它打开其他显示一般状态并要求各种用户输入的窗体。问题是,当任何表单被来自通信引擎的状态自动请求或通过用户按钮按下时。第一次请求表单时没有问题,但是第二次请求任何给定表单时,它会显示在 Windows 任务栏上,但不会在屏幕上打开。此应用程序中的所有表单切换都由同一例程处理。 注意:名为“WEDGE_MENU_SCREEN”的表单是保持打开和活动状态的表单,任何时候都应该只有一个其他表单处于活动状态。 这是代码。

 Public Function GoToForm(Requested_form As Form) As Boolean
    'in a multi form app, we only want ONE form active on screen at any given time.
    'all screen change requests should come from the same operating thread
    Dim Not_found_form As Boolean = True
    '        Dim frm As Form
    Dim formNames As New List(Of String)
    Dim xformNames As New List(Of String)

    'get a list of active forms running under this main thread LESS the static one
    For Each currentForm As Form In Application.OpenForms
        If currentForm.Name <> "WEDGE_MENU_SCREEN" Then
            formNames.Add(currentForm.Name)
        End If
    Next
    'from that list create another less the requested in the case that the requested form is already up
    For Each currentFormName As String In formNames
        If currentFormName <> Requested_form.Name Then
            xformNames.Add(currentFormName)
        End If
    Next
    'if the second list is not empty
    If xformNames.Count > 0 Then
        'hide all active forms in the second list
        For Each currentFormName As String In xformNames
            Application.OpenForms(currentFormName).Visible = False
            Application.OpenForms(currentFormName).Opacity() = 0
        Next
    End If

    'then activate the requested form
    Requested_form.Visible = True
    Requested_form.Opacity = 1
    Return True
End Function

进一步说明:我也尝试过以下方法 Application.OpenForms(currentFormName).Hide() 与 Requested_form.Show() 还 Application.OpenForms(currentFormName).Close() 使用 Requested_form.Show()

任何帮助将不胜感激。 -吉姆

【问题讨论】:

  • 我建议将表单存储在Dictionary(Of String, Form) 中,名称为键,表单为值。这样你就不需要每次想要显示一个表单时都迭代所有表单。您也可以每次都使用 Form.Show() 而不是更改可见性。

标签: vb.net forms


【解决方案1】:

也许如果你让逻辑更简单一点,它会有所帮助。 3个循环可以合二为一。

For Each currentForm As Form In Application.OpenForms
    If currentForm.Name <> "WEDGE_MENU_SCREEN" AndAlso currentForm.Name <> Requested_form.Name Then
        currentForm.Visible = False
        currentForm.Opacity() = 0
    End If
Next

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    相关资源
    最近更新 更多