问题描述
在VB.Net中,我想检查是否打开了当前表单以访问具有表单的功能
我给编码
In VB.Net I want to check whether the current form is opened or not for access the function with the form
i give the coding
Invoice.Showgrip()
有时,如果表单处于非活动状态,则会发生未将对象引用设置为对象实例错误的错误,因此如何检查表单是可见的还是活动的.
请帮我
Somtimes if the form is not active the object reference not set to an instance of an object error will occured so how to check whether the form is visible or active.
help me please
推荐答案
For Each form In My.Application.OpenForms
If (form.name = yourForm.name) Then
'form is loaded so can do work
'if you need to check whether it is actually visible
If form.Visible Then
'do work when visible
End If
End If
Next
public class MyForm : Form {
public override void OnFormClosing(FormClosingEventArgs eventArgs) {
base.OnFormClosing(eventArgs);
if (eventArgs.ClosegReason == System.Windows.Forms.CloseReason.UserClosing) {
eventArgs.Cancel = true;
this.Hide();
}
}
//...
} //class MyForm
另外,如果操作正确,表单操作也不应取决于其状态.例如,Form.Show
会显示该表单,无论它是隐藏的还是显示在调用之前.
请参阅 http://msdn.microsoft.com/en-us/library/system .windows.forms.form.aspx [ ^ ].
如果您仍然想知道表单已关闭,则可以在表单类的字段中添加一些布尔值标志,覆盖OnFormClosed
并将此标志设置为true,这将指示表单已关闭.同样,我认为您将不需要它,也不会找到很好的使用方法.我添加此建议只是为了正式回答您的问题.
Also, if you do it right, form operations should not depend on its status. For example, Form.Show
shows the form no matter if it was hidden or shown before the call.
See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^].
If you still want to know of the form was closed, you can add some Boolean flag as a field of the form class, override OnFormClosed
and set this flag to true, which would indicate the form was closed. Again, I think you won''t need it and won''t find a good use of it; I added this advice only to formally answer your question.
这篇关于如何检查表格是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!