我的第一个表单是登录表单,当用户正确登录时,我隐藏登录表单并显示第二个表单。现在,我想从第二种形式关闭它,而不是隐藏登录表单。我以为我能够做到这一点,但不知怎的,我遇到了麻烦。
到目前为止,我确实喜欢这个。
我制作了我的FrmLogin实现的界面:
df.loc
接口:
Public Class FrmLogin
Implements ICloseLogin
FrmLogin实现界面:
Public Interface ICloseLogin
Sub Close()
End Interface
现在我将FrmLogin(Me)传递给第二个表单构造函数:
Private Sub ICloseLogin_Close() Implements ICloseLogin.Close
Me.Close()
End Sub
第二种形式:
Private Sub ShowMainForm()
Dim FrmMain As New FrmMainMDI(Me)
FrmMain.IsMdiContainer = True
FrmMain.StartPosition = FormStartPosition.CenterScreen
FrmMain.Show()
'Me.Hide() 'not anymore
End Sub
当我调试时,当涉及到行:closeloginfform.Close()我想只看到FrmLogin被关闭,但所有都以某种方式关闭。为什么呢?
有待进一步讨论:
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
模块程序:
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
进一步讨论2
解决了???:我在CheckAccess()子行中添加了一行,这一行:
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
所以代码的和平只是改变了:
Me.DialogResult = DialogResult.OK
答案 0 :(得分:3)
执行此操作的一种较少涉及的方法是从Sub Main
启动您的应用,并且只有在LogIn正确的情况下才启动应用。为此:
Public Sub Main
Enable Application Framework
Startup Object
,请选择“Sub Main”您实际上可以为模块命名,“程序”是描述性的,并且是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