Login Class Problems!!!

I decided to make a simple to use stand alone dll that would expose one function - AllowAccess(Title, NoAttemps, etc) as boolean. This would be called at the main programs Load event, if it returned False the program would close, if true it would keep going.

Basically the dll has a class full of controls that i have yet to add, and a form class (frmLoginGUI). This is already dimentioned, but not initialised when the function is called. I want the function to run a form, allow the user to exit, or have a set number of goes at the password. i also want the form to be a dialog form!

Basically, i can't show the form and ask for x dialog results as the result is repeated and it just closes, but i don't want to call the show dialog method with each loop, as it flashes - tacky! Any ideas?

PublicFunction AllowAccess(ByVal TitleAsString,ByVal AttemptsAllowedAsInteger)AsBoolean

LoadUsers() ' gets the list of user info from a text/dat file

frmLogin =New frmLoginGUI 'instatializes the frmLogin

Do

If frmLogin.ShowDialog() = Windows.Forms.DialogResult.OKThen

If CorrectInput(strUserName, strPassword) =TrueThen

frmLogin.Close()

ReturnTrue

Else

intAttempts += 1

EndIf

Else

frmLogin.Close()

ReturnFalse

EndIf

LoopWhile intAttempts < AttemptsAllowed

frmLogin.Close()

ReturnFalse

EndFunction

If you can think of a better way to do it, let me know!

[4232 byte] By [Byron_Black] at [2007-12-24]
# 1
I'd put all the logic including the loop in the form itself.
TaDa at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Fair Point! Give me 5 minutes!
Byron_Black at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
How do you mean? If i make the form show itself, and add a load of code in it to control the OK and cancel buttons, won't it get ignored as my AllowAccess function carries on, reuturns the default value and closes the program?
Byron_Black at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Some pseudo code:

Public Sub Main(Args() as string)

Dim Login as new frmLogin(WhatEverArgsYouNeed)

Login.ShowDialog()

if Login.DialogResult = OK then

Dim Main as new frmMain()

Application.Run(Main)

Else

'Whatever here

End

End Sub

Form Code:

Dim Tries as integer =0

Private Sub OK_Click(...)...

if UsernameOK and PasswordOK then

me.DialogResult = OK

else

Tries += 1

if Tries > MaxTries then

me.dialogReqult = Cancel

End If

End If

End Sub

Private Sub Cancel_Click(...)...

me.DialogResult = Cancel

End

TaDa at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
Make AllowAccess and the variables it uses Shared members of the form. I came up with this:

Public Class frmLogin
Private Shared mAttemptCounter As Integer
Private Shared mAttemptMax As Integer

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
mAttemptCounter += 1
If txtUser.Text = "nobugz" And txtPassword.Text = "waz here" Then
Me.DialogResult = Windows.Forms.DialogResult.OK
ElseIf mAttemptCounter > mAttemptMax Then
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Else
Me.txtUser.Text = ""
Me.txtPassword.Text = ""
Me.txtUser.Focus()
End If
End Sub

Public Shared Function AllowAccess(ByVal title As String, ByVal AttemptsAllowed As Integer) As Boolean
mAttemptCounter = 0
mAttemptMax = AttemptsAllowed
Dim f As New frmLogin
f.Text = title
Return f.ShowDialog = DialogResult.OK
End Function

End Class

Call it like this:
frmLogin.AllowAccess("No bugz permitted", 4)

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

I think i get ya point. basically, rather than hooking the dialog result up the buttons, and thereby closing the form automatically, etc, you set the result manually! This gives control and the chance to check the No.Attempts, etc!

Cheers, i'll have a go!

Byron_Black at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7

>> rather than hooking the dialog result up the buttons, and thereby closing the form automatically, etc, you set the result manually!

Yup! The Cancel could be hooked up, but not the OK.

TaDa at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8
You're a genius! Cheers mate it works like a charm!
Byron_Black at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9
I like TaDa's answer the best, but i have to admit that i will definately use some of nobugz code. Makes it more organised and just simpler to use.
Byron_Black at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...