如何防止表单的多个实例?

时间:2011-06-22 04:16:34

标签: vb.net

我正在开展一个项目,我有一个家庭表格,其中有两个按钮用于登录员工和管理员。点击按钮登录表单后会打开,但我希望登录窗口打开后d必须是以前的家庭表格关闭或隐藏..还有主页登录页面上的链接,但一旦用户点击主页链接新的主页实例打开,我想停止多次打开form.i尝试close()和hide()但没有用.... form1的代码,即家庭形式;

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Georgia", 12.0!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(136, 184)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(100, 40)
        Me.Label1.TabIndex = 8
        Me.Label1.Text = "Employee Login"
        '
        'Label4
        '
        Me.Label4.Font = New System.Drawing.Font("Georgia", 12.0!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label4.Location = New System.Drawing.Point(288, 184)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(136, 48)
        Me.Label4.TabIndex = 12
        Me.Label4.Text = "Administrator Login"
        '
        'Button1
        '
        Me.Button1.BackgroundImage = CType(resources.GetObject("Button1.BackgroundImage"), System.Drawing.Image)
        Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.Button1.Location = New System.Drawing.Point(152, 104)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(64, 64)
        Me.Button1.TabIndex = 16
        '
        'Button2
        '
        Me.Button2.BackColor = System.Drawing.Color.Transparent
        Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup
        Me.Button2.Image = CType(resources.GetObject("Button2.Image"), System.Drawing.Image)
        Me.Button2.Location = New System.Drawing.Point(312, 104)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(64, 64)
        Me.Button2.TabIndex = 17
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(7, 15)
        Me.BackColor = System.Drawing.Color.White
        Me.ClientSize = New System.Drawing.Size(496, 341)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label1)
        Me.Font = New System.Drawing.Font("Georgia", 9.75!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "Form1"
        Me.Text = "Employee Management System"
        Me.ResumeLayout(False)

    End Sub

#End Region




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



    End Sub




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frmDialogue As New Form5

        frmDialogue.ShowDialog()


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim frmDialogue As New Form7

        frmDialogue.ShowDialog()

    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

您应该能够在button_Click事件期间关闭或隐藏当前表单。听起来用户将在登录后查看主页表单,因此最好隐藏主页表单。

为了重新显示主页表单,Login表单必须具有对它的引用。这可以通过将其传递给构造函数来完成。

(这段代码在C#中,抱歉我不懂VB。但是应该以同样的方式工作。)

// Login Button Click Event
private void button1_Click(object sender, EventArgs e)
{
   LoginForm loginForm = new LoginForm();
   loginForm.Show();
   this.Hide();
}

// Login Form Constructor
public LoginForm(HomeForm homeForm)
{
   this._homeForm = homeForm;
}

// Home Button Click Event
private void btnHome_Click(object sender, EventArgs e)
{
   this._homeForm.Show();
   this.Hide();
}

答案 1 :(得分:0)

Eric的C#答案的VB.NET版本......

Public Class homeForm

  ''' <summary>
  ''' Login button click event
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  ''' <remarks></remarks>
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim loginForm As New loginForm(Me)
    loginForm.Show()
    Me.Hide()
  End Sub
End Class

Public Class loginForm
  Private _homeForm As homeForm
  ''' <summary>
  ''' Login form constructor
  ''' </summary>
  ''' <param name="homeForm"></param>
  ''' <remarks></remarks>
  Sub New(ByVal homeForm As homeForm)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me._homeForm = homeForm
  End Sub
  ''' <summary>
  ''' Home button click event
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  ''' <remarks></remarks>
  Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
    Me._homeForm.Show()
    Me.Hide()
  End Sub
End Class