简体   繁体   English

如何关闭表单(由代码创建)

[英]how to close a form (created by code)

i am creating a form by codes. 我正在通过代码创建表格。

i am using this code 我正在使用此代码

Dim frmNew As New Form2
    If frmNew.ShowInTaskbar = True Then
        frmNew.Close()
    End If
    Dim b As Button = DirectCast(sender, Button)
    frmNew.StartPosition = FormStartPosition.CenterScreen
    frmNew.Name = b.Name
    frmNew.Text = b.Text
    Try
        frmNew.Show()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

i have a design in my form2 so this is the form that im using in the declaration of frmnew after clicking the button it shows my new form but when i click again the button, it generates a new form the same in the first one. 我在form2中有一个设计,因此这是im在单击按钮后在frmnew声明中使用的表单,它显示了我的新表单,但是当我再次单击该按钮时,它将在第一个表单中生成一个新表单。 i want to close first the form before generating a new form. 我想在生成新表格之前先关闭表格。

i am using this code in my Multiple user LAN CHAT. 我在我的多用户LAN CHAT中使用此代码。

Thanks for the help. 谢谢您的帮助。

To find if another instance of the same form is currently open, you could search the collection Application.OpenForms and check if it contains a form with the same name of your Form2. 要查找当前是否打开了具有相同表单的另一个实例,可以搜索集合Application.OpenForms并检查它是否包含与您的Form2具有相同名称的表单。 Of course you should avoid to have two unrelated forms with the same name. 当然,您应该避免具有相同名称的两个不相关的表单。

Dim k = Application.OpenForms.Cast(Of Form).Where(Function (x) x.Name = "yourFormName").SingleOrDefault() 

if k IsNot Nothing Then
   k.Close()
End If

Dim frmNew As Form2
frmNew = new Form2
Dim b As Button = DirectCast(sender, Button)
frmNew.StartPosition = FormStartPosition.CenterScreen
frmNew.Name = b.Name
frmNew.Text = b.Text

Try
    frmNew.Show()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

This approach avoids a global variable to keep track of the previous instance. 这种方法避免了使用全局变量来跟踪先前的实例。

After a quick check I think that the search code could be reduced to 经过快速检查,我认为搜索代码可以简化为

Dim k = Application.OpenForms.Cast(Of Form2).SingleOrDefault() 

And this will avoid also a possible name conflict with an unrelated form with the same name 这样也可以避免名称不相关的表单之间发生名称冲突

You should declare frmNew globally inside you class. 您应该在类内部全局声明frmNew This has the advantage that you don't have to worry about closing other forms at all. 这样的好处是您根本不必担心关闭其他表单。

So, declare it globally (outside of any method): 因此,全局声明(任何方法之外):

Dim frmNew as Form2

And inside your method: 在您的方法内部:

If frmNew IsNot Nothing Then
    frmNew.Close()
frmNew = New Form2
Dim b As Button = DirectCast(sender, Button)
frmNew.StartPosition = FormStartPosition.CenterScreen
frmNew.Name = b.Name
frmNew.Text = b.Text
Try
    frmNew.Show()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

Now each time the method is called, the same frmNew will be (re-)initialized, ie you're only working with a single Form2 at all times. 现在,每次调用该方法时,相同的frmNew将被(重新)初始化,即,您始终只使用一个Form2。

I think this will work 我认为这会起作用

Global declaration variable, inside class 全局声明变量,内部类

Dim frmNew as New Form2

And inside your method: 在您的方法内部:

If frmNew.ShowInTaskbar = True Then
    frmNew.Close()
End If
frmNew = New Form2
Dim b As Button = DirectCast(sender, Button)
frmNew.StartPosition = FormStartPosition.CenterScreen
frmNew.Name = b.Name
frmNew.Text = b.Text
Try
    frmNew.Show()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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