2011-06-22 48 views
1

我正在一個項目中,我有一個家庭窗體,其中有兩個按鈕用於登錄員工和administrator.after點擊按鈕登錄窗體將打開,但我想只要登錄窗口打開d以前的家庭窗體必須關閉或隱藏..也有在家裏的登錄頁面上的鏈接,但只要用戶點擊家庭鏈接家庭窗體的新實例打開,我希望停止dis多次打開form.i試着關閉()和隱藏()但沒有用.... 代碼爲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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     Dim frmDialogue As New Form7 

     frmDialogue.ShowDialog() 

    End Sub 
End Class 

回答

0

您應該能夠在button_Click事件期間關閉或隱藏當前窗體。這聽起來像用戶在登錄後將要查看家庭表單,因此隱藏家庭表單可能會更好。

爲了重新顯示住宅窗體,登錄窗體必須有一個對它的引用。這可以通過將其傳遞給構造函數來完成。

(此代碼是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(); 
} 
0

埃裏克的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