在程序中重复使用相同的winform

时间:2013-08-06 16:12:21

标签: vb.net

如果我的问题标题有点模糊,我道歉。我有以下程序调用表单。超链接调用相同的表单,通过sub。一切正常,问题是如果我单击一个链接然后另一个链接,表单将打开两次,这是应该发生的,因为我将表单实例化为New。

我想要做的只是打开相同的表单,这样如果用户点击链接,那么只打开一个表单而不是几个。

    Private Sub dsbPositionBoard_FollowHyperlink(Target As Microsoft.Office.Interop.Excel.Hyperlink) Handles Me.FollowHyperlink

    'This procedure runs when any of the hyperlinked cells in the position dashboard are clicked
    'The hyperlinks open the frmDefinition on the assigned defintion. The procedure calls
    'the function. 

    'The hyperlinked cells are assigned under the ThisWorkbook/Open event.

    Dim definitionForm As New frmDefinitions

    Select Case Target.TextToDisplay

        Case "Exempt"
            definitionForm.tmr_out.Enabled = True
            sheetView.exemptDefinition()

        Case "Employee Pay Distribution for Ranges", "Low Third", "Upper Third"
            definitionForm.tmr_out.Enabled = True
            sheetView.lowerThirdDefinition()

        Case "Market Percentiles"
            definitionForm.tmr_out.Enabled = True
            sheetView.marketPercentileDefinition()

        Case "Min", "Mid", "Max", "Salary Range to Mkt"
            definitionForm.tmr_out.Enabled = True
            sheetView.payGradeWidthDefintion()

        Case "Total Cash Compensation Data"
            definitionForm.tmr_out.Enabled = True
            sheetView.totalCashCompDefition()

        Case "Compa-Ratio"
            definitionForm.tmr_out.Enabled = True
            sheetView.compaRatioDefinition()

    End Select

End Sub

1 个答案:

答案 0 :(得分:1)

您有两种选择:全局定义给定的frmDefinitions变量,并在需要时关闭/打开它;或者将它作为参数传递给函数。

我认为你的情况的最佳选择(通过假设典型条件)是全局定义。下面我提供了一个基于标准Form的小代码,您不应该发现任何问题以适应您的特定frmDefinitions类:

Public Class Form1
    Dim definitionForm As New Form

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        definitionForm.Close()

        definitionForm = New Form
        definitionForm.Show()

    End Sub
End Class

正如您所看到的,当您点击Button1时,表格definitionForm会被反复使用(之前的实例已关闭且新实例已创建并已打开)。