How to make my LoginForm1 and Form1 better ?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 2375 Views - Last Post: 02 June 2013 - 03:05 PM Rate Topic: -----

#1 Gober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 30-May 13

How to make my LoginForm1 and Form1 better ?

Posted 31 May 2013 - 02:04 PM

I am creating a program.

A friend saw that the code is too long and unstable.

So I want to know how to make it shorter and more stable.

This is Form1 ( main form ) code :

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Button1.BackgroundImageLayout = ImageLayout.Stretch
        Button2.BackgroundImageLayout = ImageLayout.Stretch

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If CPW = True Then

            ShowForm2()

        Else

            ShowLoginForm()

        End If

        CPWFalse()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        If CPW = True Then

            ShowForm3()

        Else

            ShowLoginForm()

        End If

        CPWFalse()

    End Sub

    Private Sub ShowLoginForm()

        If CPW = False Then

            LoginForm1.ShowDialog()

        End If

    End Sub

    Private Sub CPWFalse()
        CPW = False
    End Sub

    ' ShowLoginForm from Label1

    Private Sub Label1_DragEnter(sender As Object, e As DragEventArgs) Handles Label1.DragEnter
        ShowLoginForm()
    End Sub

    Private Sub Label1_Leave(sender As Object, e As EventArgs) Handles Label1.Leave
        ShowLoginForm()
    End Sub

    Private Sub Label1_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
        ShowLoginForm()
    End Sub

    Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
        ShowLoginForm()
    End Sub

    ' ShowLoginForm from Button1

    Private Sub Button1_DragEnter(sender As Object, e As DragEventArgs) Handles Button1.DragEnter
        ShowLoginForm()
    End Sub

    Private Sub Button1_Leave(sender As Object, e As EventArgs) Handles Button1.Leave
        ShowLoginForm()
    End Sub

    Private Sub Button1_KeyDown(sender As Object, e As KeyEventArgs) Handles Button1.KeyDown
        ShowLoginForm()
    End Sub

    Private Sub Button1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Button1.KeyPress
        ShowLoginForm()
    End Sub

    Private Sub Button1_KeyUp(sender As Object, e As KeyEventArgs) Handles Button1.KeyUp
        ShowLoginForm()
    End Sub

    Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
        ShowLoginForm()
    End Sub

    Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) Handles Button1.MouseEnter
        ShowLoginForm()
    End Sub

    ' ShowLoginForm from Button2

    Private Sub Button2_DragEnter(sender As Object, e As DragEventArgs) Handles Button2.DragEnter
        ShowLoginForm()
    End Sub

    Private Sub Button2_Leave(sender As Object, e As EventArgs) Handles Button2.Leave
        ShowLoginForm()
    End Sub

    Private Sub Button2_KeyDown(sender As Object, e As KeyEventArgs) Handles Button2.KeyDown
        ShowLoginForm()
    End Sub

    Private Sub Button2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Button2.KeyPress
        ShowLoginForm()
    End Sub

    Private Sub Button2_KeyUp(sender As Object, e As KeyEventArgs) Handles Button2.KeyUp
        ShowLoginForm()
    End Sub

    Private Sub Button2_MouseDown(sender As Object, e As MouseEventArgs) Handles Button2.MouseDown
        ShowLoginForm()
    End Sub

    Private Sub Button2_MouseEnter(sender As Object, e As EventArgs) Handles Button2.MouseEnter
        ShowLoginForm()
    End Sub

End Class



And this is LoginForm1 code :

Public Class LoginForm1

    Dim FL As Boolean = False
    Dim R As DialogResult

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

        If UN = "" And PW = "" And FL = True Then

            If UsernameTextBox.Text = "" Or PasswordTextBox.Text = "" Then

                R = MessageBox.Show("This is your first login so you must type a user name and a password", Me.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

                Incorrect()

            Else

                UN = UsernameTextBox.Text
                PW = PasswordTextBox.Text
                StoreUNAndPW()
                MessageBox.Show("You have successfully created a user name and a password", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)

                Correct()

            End If

        Else

            Select Case LA

                Case 1 To 5

                    If UsernameTextBox.Text = UN And PasswordTextBox.Text = PW Then

                        MessageBox.Show("Correct user name and password", Me.Text)

                        Correct()

                    Else

                        Select Case LA

                            Case 4

                                MessageBox.Show("Incorrect user name and/or password. Only 1 attempt remaining", Me.Text)
                            Case Else

                                MessageBox.Show("Incorrect user name and/or password. Only " & 5 - LA & " attempts remaining", Me.Text)

                        End Select

                        Incorrect()

                    End If

                Case Else

                    MessageBox.Show("Incorrect user name and/or password. You have no attempts remaining", Me.Text)

                    Incorrect()


            End Select

            LA = LA + 1

        End If

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click

        If UN = "" And PW = "" And FL = True Then

            R = MessageBox.Show("This is your first login so you must type a user name and a password", Me.Text)

        Else

            R = MessageBox.Show("You must type a user name and a password to enter", Me.Text, MessageBoxButtons.OKCancel)

            LA = LA + 1

        End If

        Incorrect()

    End Sub

    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        LogoPictureBox.BackgroundImageLayout = ImageLayout.Stretch

        If UN = "" And PW = "" Then

            Me.Text = "First login"

            MessageBox.Show("This is your first login so you will create a user name and a password", Me.Text)

            FL = True

        End If

        Timer1.Start()

    End Sub

    Private Sub ShowMainForm()

        Me.Hide()
        Form1.Show()

    End Sub

    Private Sub Correct()

        CPW = True
        ShowMainForm()

    End Sub

    Private Sub Incorrect()

        If R = Windows.Forms.DialogResult.Cancel Then

            EndAll()

        End If

        UsernameTextBox.Clear()
        PasswordTextBox.Clear()
        UsernameTextBox.Focus()

        CPW = False

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        TooManyLA()

    End Sub
End Class




Finally , this is Module1 code :


Module Module1

    Public UN As String = My.Settings.UN
    Public PW As String = My.Settings.PW
    Public CPW As Boolean = False
    Public LA As Integer = 1

    Public Sub ShowForm2()

        Form1.Hide()
        Form3.Hide()
        Form2.Show()

    End Sub

    Public Sub ShowForm3()

        Form1.Hide()
        Form2.Hide()
        Form3.Show()

    End Sub

    Public Sub StoreUNAndPW()

        My.Settings.UN = UN
        My.Settings.PW = PW
        My.Settings.Save()

    End Sub

    Public Sub TooManyLA()

        If LA >= 7 Then EndAll()

    End Sub

    Public Sub EndAll()

        End

    End Sub

End Module



My question is how to make my code better and shorter ?!

This post has been edited by AdamSpeight2008: 01 June 2013 - 09:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to make my LoginForm1 and Form1 better ?

#2 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: How to make my LoginForm1 and Form1 better ?

Posted 31 May 2013 - 02:34 PM

There is hardly any actual code here, it is mainly the opening and closing of event handlers.

The best way to improve it would be to rename your forms and controls. Form1, Form2, Button2, etc., are not helpful, especially as the application grows.

You also seem to have lots of forms and lots of events that do the same thing. I haven't studied your code but I would assume that these could be reduced.

If you want more detail in the answer then you'll need to ask a more specific question.
Was This Post Helpful? 0
  • +
  • -

#3 Aboch  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 28-April 09

Re: How to make my LoginForm1 and Form1 better ?

Posted 31 May 2013 - 02:47 PM

It may help if you comment your code to help people understand what you are wanting to do. Also consider reading up on subs and functions to save on the repetitive coding.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: How to make my LoginForm1 and Form1 better ?

Posted 31 May 2013 - 07:37 PM

I see a bad design. Why would you ShowLoginForm() in all those events? You have a Sub that changes a variable to false - how about just setting the variable directly.


Code efficiency:

If [boolean variable] = True Then ... 'cost more than 
If [boolean variable] Then ...
 'or 
If Not [boolean variable] Then ...


Was This Post Helpful? 0
  • +
  • -

#5 Gober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 30-May 13

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 04:08 AM

View Postandrewsw, on 31 May 2013 - 02:34 PM, said:

There is hardly any actual code here, it is mainly the opening and closing of event handlers.

The best way to improve it would be to rename your forms and controls. Form1, Form2, Button2, etc., are not helpful, especially as the application grows.


I renamed them :

Form1 is now MainForm.
Button1 is now BSF2.
Button2 is now BSF3.

View Postandrewsw, on 31 May 2013 - 02:34 PM, said:

You also seem to have lots of forms and lots of events that do the same thing. I haven't studied your code but I would assume that these could be reduced.

If you want more detail in the answer then you'll need to ask a more specific question.


I ask you now how can I reduce the call of ShowLoginForm() ?!
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 08:35 AM

Quote

I ask you now how can I reduce the call of ShowLoginForm() ?!


Well think of ways you want to show the login form. Button, menu item? Surely you don't need to call it with a leave event or dragDrop event - right? Never end your application with End use Me.Close when in a form, or Application.Exit. I also don't recommend using default instances of forms - create new instances and use events to pass data as needed.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 08:48 AM

BSF2 and BSF3 don't mean jack all to me.

Make the control names (or any other identifier) contentful and meaningful, so you can infer its usage from the code. I'm looking at a text source code don't want to be break my flow my having to break of to refer back to a graphical GUI designer (to look at the layout)
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 09:00 AM

View PostGober, on 01 June 2013 - 11:08 AM, said:

Form1 is now MainForm.
Button1 is now BSF2.
Button2 is now BSF3.

MainForm is a slight improvement, although I prefer frmMain. But BSF2? If you do this you might as well leave it as Button1 :whistling:. I prefer btnSomethingMeaningful. (I don't tend to use so many characters, but even if you do, Visual Studio will auto-complete it for you.)

This post has been edited by andrewsw: 01 June 2013 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#9 Gober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 30-May 13

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 09:20 AM

View Post_HAWK_, on 01 June 2013 - 08:35 AM, said:

Quote

I ask you now how can I reduce the call of ShowLoginForm() ?!


Well think of ways you want to show the login form. Button, menu item? Surely you don't need to call it with a leave event or dragDrop event - right?


OK. I considered your advice.

MainForm code is now :


Public Class MainForm

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        BSF2.BackgroundImageLayout = ImageLayout.Stretch
        BSF3.BackgroundImageLayout = ImageLayout.Stretch

    End Sub

    Private Sub BSF2_Click(sender As Object, e As EventArgs) Handles BSF2.Click

        If CPW = True Then

            ShowForm2()

        Else

            ShowLoginForm()

        End If

        CPWFalse()

    End Sub

    Private Sub BSF3_Click(sender As Object, e As EventArgs) Handles BSF3.Click

        If CPW = True Then

            ShowForm3()

        Else

            ShowLoginForm()

        End If

        CPWFalse()

    End Sub

    Private Sub ShowLoginForm()

        If CPW = False Then

            LoginForm1.ShowDialog()

        End If

    End Sub

    Private Sub CPWFalse()
        CPW = False
    End Sub

    ' ShowLoginForm from Label1

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
        ShowLoginForm()
    End Sub

    ' ShowLoginForm from Button1

    Private Sub Button1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles BSF2.KeyPress
        ShowLoginForm()
    End Sub

    ' ShowLoginForm from Button2

    Private Sub Button2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles BSF3.KeyPress
        ShowLoginForm()
    End Sub

End Class


My question again is how to make it better ?!


View Post_HAWK_, on 01 June 2013 - 08:35 AM, said:

Never end your application with End use Me.Close when in a form, or Application.Exit.


Tell me what is the difference between End , Me.Close and Application.Exit.

View Post_HAWK_, on 01 June 2013 - 08:35 AM, said:

I also don't recommend using default instances of forms - create new instances and use events to pass data as needed.


I don't understand this sentence.
Was This Post Helpful? 0
  • +
  • -

#10 Gober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 30-May 13

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 09:26 AM

View PostAdamSpeight2008, on 01 June 2013 - 08:48 AM, said:

BSF2 and BSF3 don't mean jack all to me.

Make the control names (or any other identifier) contentful and meaningful, so you can infer its usage from the code. I'm looking at a text source code don't want to be break my flow my having to break of to refer back to a graphical GUI designer (to look at the layout)


View Postandrewsw, on 01 June 2013 - 09:00 AM, said:

View PostGober, on 01 June 2013 - 11:08 AM, said:

Form1 is now MainForm.
Button1 is now BSF2.
Button2 is now BSF3.

MainForm is a slight improvement, although I prefer frmMain. But BSF2? If you do this you might as well leave it as Button1 :whistling:/>/>. I prefer btnSomethingMeaningful. (I don't tend to use so many characters, but even if you do, Visual Studio will auto-complete it for you.)


BSF2 = Button [that] Shows Form 2.
BSF3 = Button [that] Shows Form 3.

What better names do you suggest ?

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 09:36 AM

Form2? Still meaningless to me.

What is the purpose or intention of the form. RoomBookingForm.

It that was opened by a button, I may useOpenBookingForm_But.

Imagine some validation code that uses it
 If {venue selected} Then
   OpenBookingForm_But.Enable = True
 Else
   '' etc '
 End If


Was This Post Helpful? 0
  • +
  • -

#12 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 10:15 AM

Gober said:

View Post_HAWK_, on 01 June 2013 - 08:35 AM, said:

Never end your application with End use Me.Close when in a form, or Application.Exit.


Tell me what is the difference between End , Me.Close and Application.Exit.


Me.Close inside of a form tell the application that form is closing. Application.Exit can be called from anywhere in the app to shut it down regardless of open forms. End is not good practice for shutting down.

Gober said:

View Post_HAWK_, on 01 June 2013 - 08:35 AM, said:

I also don't recommend using default instances of forms - create new instances and use events to pass data as needed.


Quote

I don't understand this sentence.


Say your login form was named LoginForm.
Dim _loginForm As New LoginForm 'instance of form
_loginForm.show

LoginForm.Show                  'default instance

This post has been edited by _HAWK_: 01 June 2013 - 10:19 AM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 10:30 AM

Quote

The End statement stops code execution abruptly, without invoking the Finalize method or any other Visual Basic code.

Reference

So use Application.Exit. However, in a properly designed application, using Form.Close on the last form that remains open (which should be the main form, ideally) will cause a natural exit to your application.

The ability to open and manipulate the static instance of a form is a hangover from VB6. It is not a good practice: it is confusing and can cause some problems. The proper OOP way to do this is to create a New instance of your form, and then Show it. This also has the advantage that you can produce more than one instance of the form (if this is useful to you).

This post has been edited by andrewsw: 01 June 2013 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#14 Gober  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 30-May 13

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 01:47 PM

View Post_HAWK_, on 01 June 2013 - 10:15 AM, said:

Say your login form was named LoginForm.
Dim _loginForm As New LoginForm 'instance of form
_loginForm.show

LoginForm.Show                  'default instance


What am I going to gain from this ?
Creating forms that look like LoginForm ?!

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is offline

  • say what now
  • member icon

Reputation: 6410
  • View blog
  • Posts: 25,910
  • Joined: 12-December 12

Re: How to make my LoginForm1 and Form1 better ?

Posted 01 June 2013 - 02:04 PM

It is the proper way to work with forms, and ALL (sensible) help pages, reference material and tutorials will do it this way. That is, to create a new instance of the form.

The ability to use the default (static) instance of a form was retained to ease former VB6 programmers into VB.NET. In order to enable this feature a lot of background-code is created by the compiler (I believe).

Doing it properly will also prevent (or at least, defer ;)) the mockery of other more OOP-aware programmers.

This post has been edited by andrewsw: 01 June 2013 - 02:09 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2