how to prevent multiple instances of a form?
I am working on a project where i have a home form which has two buttons for login for employee and administrator.after clicking on button login form will open,but i want as soon as login window opens d previous home form must be closed or hide..also there is link on login page for home,but as soon as user click on home link new instance of home form is opening,hnce i want to stop dis multiple opening of the form.i tried close() and hide() but no use.... code for form1 ie home form;
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(B开发者_开发百科yVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim frmDialogue As New Form7
frmDialogue.ShowDialog()
End Sub
End Class
You should be able to close or hide the current form during the button_Click event. It sounds like the users are going to be viewing the home form after they have logged in, so it is probably better to just hide the home form.
In order to reshow the home form, the Login form must have a reference to it. This can be done by passing it through the constructor.
(This code is in C#, sorry I don't know VB. Should work the same way, though.)
// 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();
}
VB.NET version of Eric's C# answer...
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
精彩评论