如何保留循环中的表单值?

时间:2014-02-06 20:32:52

标签: c# winforms excel

我正在创建一个项目,其中有3个表单的循环form1-> form2-> form3-> form1-> form2-> form3 ....并且循环保持到应用程序退出。循环通过按钮进行。在form3中,我使用了interop.excel并将数据从工作表复制到数组,这是通过从列表框中选择excel文件的名称来完成的。

现在的问题是,每次从form3转到form1 form3都会失去焦点,而form2总是会创建一个form3的新实例。因此,即使我以前的form3打开它也会打开新的form3。

我在How can I loop through all the open instances of a particular form?Application.OpenForms.Count = 0 always看到并尝试过解决方案 但它没有帮助。

有没有办法让form2可以检查是否存在form3的任何实例,然后决定创建form3的新实例或将焦点放在以前的form3上。

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

来自This Thread

Function IsUserFormLoaded(ByVal UFName As String) As Boolean 
Dim UForm As Object 

IsUserFormLoaded = False 
For Each UForm In VBA.UserForms 
    If UForm.Name = UFName Then 
        IsUserFormLoaded = True 
        Exit For 
    End If 
Next 
End Function 'IsUserFormLoaded

所以根据您的需要,你可以做到

If IsUserFormLoaded(UserForm3) Then
    'Code if it is open here
Else 
    'Code if it is NOT open here
End If