我有一个多项目解决方案,其中一个项目是“主要”,启动项目 从这个项目开始,我在其他项目中启动表单,这些项目都在启动项目中引用。
问题是我可以像这样的实例启动这些表单:
Dim ka As New otherproject.frm_thatform
With ka
.BringToFront()
.Show(Me)
End With
每次都会打开新的“thatform”,但我想每次都像单个实例一样启动“thatform”并且“thatform”出现在前面。
怎么做?
我试着这样:
Dim ka As otherproject.frm_thatform
With ka
.BringToFront()
.Show(Me)
End With
...但是不起作用(对象引用未设置为对象的实例)。
答案 0 :(得分:1)
诀窍是不要将表单变暗并将其用作共享对象。
Otherproject.frm_thatform.Show()
Otherproject.frm_thatform.BringToFront()
只要您不关闭此窗口,就可以像调暗Dimmed对象一样调用它。
而不是你只需输入Otherproject.frm_thatform
一旦关闭窗口,它就会松开其中的所有内容。
编辑:显然这仅适用于项目内的表单。我的坏:(您需要做的是在主项目中保留一个表单列表。
确保为表单命名,当您单击按钮打开表单时,只需循环遍历列表即可找到集名称。
这样的事情:
Private FormList As New List(Of Form)
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim theForm As Form = Nothing
For Each f As Form In FormList
If f.Name = "Selected form name" Then
theForm = f
End If
Next
If theForm Is Nothing Then
theForm = New Otherproject.frm_thatform()
theForm.Name = "Selected form name"
FormList.Add(theForm)
End If
End Sub
答案 1 :(得分:1)
在原始代码中,将表单声明移到class(form)级别:
Private ka As otherproject.frm_thatform = Nothing
然后检查它是否为Nothing或已被处理,并在必要时重新创建它:
If (ka Is Nothing) OrElse ka.IsDisposed Then
ka = New otherproject.frm_thatform
ka.Show(Me)
End If
If ka.WindowState = FormWindowState.Minimized Then
ka.WindowState = FormWindowState.Normal
End If
ka.BringToFront()
答案 2 :(得分:1)
有两个选项 -
Shared form As SingletonformEx.Form1
If form IsNot Nothing Then
form.BringToFront()
Else
form = New SingletonformEx.Form1()
form.Show()
End If
Public Class SingletonForm Inherits Form
Private Shared m_instance As SingletonForm
Private Sub New()
'InitializeComponent();
End Sub
Public Shared ReadOnly Property Instance() As SingletonForm
Get
If m_instance Is Nothing Then
m_instance = New SingletonForm()
End If
m_instance.BringToFront()
Return m_instance
End Get
End Property
End Class
注意:这是转换为vb.net的C#代码所以我不确定这是否完美
醇>