单例形式 [英] Singleton form

查看:100
本文介绍了单例形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下的.如何确保在任何时候都不会打开一个以上的表单实例?

I am using a from as below. How can I ensure there is never more than one instance of form open at any time?

Using y As frmAddProducts = New frmAddProducts()
    y.Show()
End Using

谢谢

致谢

推荐答案

实施Singleton模式,或编写代码,这样就永远不会打开多个实例:

implement the Singleton pattern, or write your code so, that you never open more than one instance:

https://en.wikipedia.org/wiki/Singleton_pattern

https://en.wikipedia.org/wiki/Singleton_pattern

关于"表单(名为Form5(haha))的代码示例:

code example of an "about"-form (named Form5 (haha)):

[使用Form5.GetInstance()显示表单]

[use Form5.GetInstance() to show the form]

Option Strict On
Imports System.Windows.Forms

Partial Public Class Form5
    Inherits Form
    Private Shared instance As Form5 = Nothing

    Public Shared Function GetInstance() As Form5
        If instance Is Nothing OrElse instance.IsDisposed Then
            instance = New Form5()
        End If
        Return instance
    End Function

    Private Sub New()
        InitializeComponent()
    End Sub

    Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
        Me.Close()
    End Sub
End Class

此致

 托尔斯滕


这篇关于单例形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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