如何检查表格是否打开 [英] how to check if the form is opened or not

查看:143
本文介绍了如何检查表格是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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

推荐答案

您需要循环浏览所有表格,并检查是否确实存在所需的表格
You need to cycle through all forms and check if the one you want is indeed there
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


"可能需要检查您的表单是否已实例化

如果发票不是空的,则
发票.Showgrip
如果
''probably need to check if your form is instantiated

If Invoice isnot nothing then
invoice.Showgrip
End If


空对象引用与表单状态无关.它可以关闭,激活-其类的实例仍然有效.我不知道为什么您需要检查表格是否关闭.我非常确定您永远不需要它.一些建议和示例:

如果要再次显示表单,请不要关闭它,而要隐藏它.方法如下:

Null object reference has nothing to do with form status. It can be closed, activated — the instance of its class remains valid. I have no idea why would you need to check up if the form is closed. I''m pretty much sure you never need it. Some advice and example:

If you want to show a form again, never close it but hide it instead. Here is how:

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,这将指示表单已关闭.同样,我认为您将不需要它,也不会找到很好的使用方法.我添加此建议只是为了正式回答您的问题.

—SA



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.

—SA


这篇关于如何检查表格是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆