2015-10-27 102 views
0

我的第一種形式是登錄表單,當用戶正確登錄時,我隱藏登錄表單並顯示第二種形式。現在,而不是隱藏登錄表單,我想從第二種形式關閉它。我以爲我會做到這一點,但不知何故,我遇到了麻煩。第二種形式的關閉表格

到目前爲止,我確實喜歡下面的內容。

我做了界面,我FrmLogin實現:

Public Class FrmLogin 
     Implements ICloseLogin 

接口:

Public Interface ICloseLogin 
    Sub Close() 
End Interface 

FrmLogin實現接口:

Private Sub ICloseLogin_Close() Implements ICloseLogin.Close 
     Me.Close() 
    End Sub 

現在我路過FrmLogin(Me)升級到第二種形式構造函數:

Private Sub ShowMainForm() 
     Dim FrmMain As New FrmMainMDI(Me) 
     FrmMain.IsMdiContainer = True 
     FrmMain.StartPosition = FormStartPosition.CenterScreen 
     FrmMain.Show() 
     'Me.Hide() 'not anymore 
    End Sub 

第二種形式:

Public Class FrmMainMDI 

    Private closeloginfform As ICloseLogin 

Sub New(frmlogin As ICloseLogin) 

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

     ' Add any initialization after the InitializeComponent() call. 
     closeloginfform = frmlogin 
    End Sub 

    Private Sub FrmMainMDI_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown 
     closeloginfform.Close() 
    End Sub 

,當我調試,當它涉及到行:closeloginfform.Close()我想只看到FrmLogin被關閉,但都被莫名其妙地關閉。爲什麼?

爲了進一步討論:

Imports DataAccessLayer 
     Imports FormsUtils 
     Imports BusinessLayer 
     Imports System.Data.SqlClient 
     Imports System.Configuration 
     Imports System.Reflection 
     Imports System.IO 
     Imports Microsoft.WindowsAPICodePack.Dialogs 
     Imports Probix 

     Public Class FrmLogin 

      Private Property Form As New FormUtils 
      Private Property DB As New Procs 
      Private Property _login As String 
      Private Property _password As String 

      Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click 
       CheckAccess() 
      End Sub 


      Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click 
       Me.Close() 
      End Sub 

      Private Sub FrmLogin_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
       Try 

        DB.OpenConn() 
        If DB.conn.State = ConnectionState.Open Then 
         Call Form.InitCombo(CboLogin, "SELECT * from tbLogin", DB.conn, "Login", "Login") 
         Call Form.InitCombo(CboLanguage, "SELECT * from tbLanguage where Active = 1", DB.conn, "Language", "Id") 
         Lang.name = DirectCast([Enum].Parse(GetType(Lang.LangShortcut), CboLanguage.GetItemText(CboLanguage.SelectedItem)), Lang.LangShortcut) 
        Else 
         Logger.LogIt(Modules.FrmLogin.ToString & ": " & Methods.FrmLogin_Load.ToString, WriteMsg.Write(IssueCode.SqlServerConnectionError_pl), Application.StartupPath & "\log.txt", False) 
         Application.Exit() 
        End If 

       Catch ex As Exception 
        Logger.LogIt(ex.tostring) 

        Application.Exit() 
       Finally 
        DB.CloseConn() 
       End Try 
      End Sub 

      Private Sub CheckAccess() 
    Try 
     _login = CboLogin.SelectedValue 
     _password = txtPassword.Text 
     If Not String.IsNullOrEmpty(txtPassword.Text) And Form.WybranoCombo(CboLogin, "Zaznacz login") Then 
      Dim strcon = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString() 
      Using con As New SqlConnection(strcon) 
       Using cmd As New SqlCommand("Select COUNT(*) FROM tbLogin WHERE Login = @Login And Password = @Password", con) 
        cmd.CommandType = CommandType.Text 
        cmd.Parameters.AddWithValue("@Login", _login) 
        cmd.Parameters.AddWithValue("@Password", _password) 
        con.Open() 
        Dim o As Integer = cmd.ExecuteScalar() 

        '--CREDENTIALS OK 
        If o > 0 Then 
         CboLogin.Hide() 
         txtPassword.Hide() 
         btnLogin.Hide() 
         Label1.Hide() 
         Label2.Hide() 
         Try 
         Catch ex As Exception 
          MsgBox(ex.ToString) 
          End 
         End Try 
         '--CREDENTIALS NOT OK !! 
        Else 
         MsgBox("Wrong credentials") 
        End If 

       End Using 
      End Using 
     Else 
      MsgBox("Write some password !!") 
     End If 
    Catch ex As Exception 
     Logger.LogIt(Modules.FrmLogin.ToString & ":" & Methods.FrmLogin_Load.ToString, WriteMsg.Write(IssueCode.Unknown_pl) & " ------> EX-MESSAGE: " & ex.ToString, Application.StartupPath & " \log.txt", False) 
     Return 
    End Try 
End Sub 

      Public Sub taskDialog_Opened(sender As Object, e As EventArgs) 
       Dim taskDialog As TaskDialog = TryCast(sender, TaskDialog) 
       taskDialog.Icon = taskDialog.Icon 
       If Not taskDialog.FooterIcon = TaskDialogStandardIcon.None Then 
        taskDialog.FooterIcon = taskDialog.FooterIcon 
       End If 
       taskDialog.InstructionText = taskDialog.InstructionText 
      End Sub 

     End Class 

模塊程序:

Module Program 
    Public Sub main() 
     Application.EnableVisualStyles() 

     Dim result As DialogResult 
     Using frmL As New FrmLogin 
      result = frmL.ShowDialog 
     End Using 

     If result = DialogResult.OK Then 
      Dim FrmMainMDI As New FrmMainMDI() 
      Application.Run(FrmMain) 
     End If 
    End Sub 
End Module 

進一步討論2

解決???:我內的checkAccess(添加的行)子,這行:

Me.DialogResult = DialogResult.OK 

所以這個代碼的和平已經不僅改變:

... 
    '--CREDENTIALS OK 
         If o > 0 Then 
          CboLogin.Hide() 
          txtPassword.Hide() 
          btnLogin.Hide() 
          Label1.Hide() 
          Label2.Hide() 
          Try 
           '************************************* 
           Me.DialogResult = DialogResult.OK  '<========================================= 
           '************************************* 
          Catch ex As Exception 
           MsgBox(ex.ToString) 
           End 
          End Try 
          '--CREDENTIALS NOT OK !! 
         Else 
          MsgBox("Wrong credentials") 
         End If 

... 
+1

爲什麼你不設置'FrmMainMDI'作爲啓動窗體,然後扔在登錄提示了當它第一次打開或者如果用戶沒有登錄?我想你會以錯誤的方式去做。 – Fred

+0

首先你能解釋爲什麼我的方法不工作,然後你能否解釋你的輸入,我沒有把它說實話。 – Arie

+2

'但是所有的都以某種方式關閉'如果FrmLogin是啓動形式,默認設置將在該窗體關閉時結束應用程序。使MDI成爲主窗體,然後像正常一樣顯示/放棄登錄窗體。或者改變你的應用程序從Sub Main開始 – Plutonix

回答

3

一個較少參與的方式來做到這一點是從Sub Main啓動您的應用程序,只如果登錄正確啓動應用程序。對於此:

  1. 將一個模塊添加到您的應用程序,並將其命名爲Program。添加Public Sub Main
  2. 轉到項目屬性,並取消現在Enable Application Framework
  3. ,爲Startup Object,選擇「子主」

實際上,你可以將該模塊命名爲任何東西,「計劃」是描述和C#中使用的慣例。然後Sub Main代碼:

Public Sub Main() 

    Application.EnableVisualStyles() 

    Dim result As DialogResult 
    Using frmL As New frmLogin 
     result = frmL.ShowDialog 
    End Using 

    If result = DialogResult.OK Then 
     frmMain = New MainFrm() 
     Application.Run(frmMain) 
    End If 

End Sub 

現在,你的兩種形式甚至不必知道彼此。如果/當登錄失敗時,MainForm將不存在。無論開銷/加載MainForm的開銷是否與延遲相關(並且除非)登錄通過。


您的登錄按鈕會是這樣的(這取決於失敗的嘗試數量上限是多少):

Private Sub btnLogIn_Click(sender As Object, e As EventArgs) Handles btnLogIn.Click 
    If IsValidUser(tbName.Text, tbPW.Text) Then 
     DialogResult = Windows.Forms.DialogResult.OK 
    Else 
     If tries >= 3 Then 
      DialogResult = Windows.Forms.DialogResult.Cancel 
     Else 
      tries += 1 
      Exit Sub 
     End If 
    End If 
    Me.Hide() 
End Sub 
+0

該bahaviour是,當用戶正確登錄然後FrmMainMDI不顯示和FrmLogin保持可見和clocing。請看看主帖(進一步的討論是我的代碼) – Arie

+0

我想我從上面的評論中克服了問題我在CheckAccess方法中添加了一行Me.DialogResult = DialogResult.OK(檢查主帖 - 「進一步討論2」)。這是否正確,可能是這樣? – Arie

+0

Plutonix你能檢查嗎? – Arie

相關問題