Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Unload Event - VB2005

Unload Event - VB2005

Scheduled Pinned Locked Moved Visual Basic
help
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    parth p
    wrote on last edited by
    #1

    Hi, I recently started using VB2005 and i find it very confusing, in VB6 i used to unload form using UNLOAD ME but it doesn't work in 2005, plus i tried Me.Close() and Me.Dispose() as well but it closes my application, rather than form. Here's the code i'm working on. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim usname As String usname = StrConv(TextBox1.Text, VbStrConv.ProperCase) If usname = "Admin" And TextBox2.Text = "pass" Then MsgBox("Welcome to Form two", MsgBoxStyle.Information + MsgBoxStyle.OkOnly) Me.Close() Form2.Show() Else MsgBox("Wrong Login details", MsgBoxStyle.Exclamation + MsgBoxStyle.RetryCancel) If MsgBoxResult.Retry Then TextBox1.Text = "" TextBox2.Text = "" TextBox1.Focus() ElseIf MsgBoxResult.Cancel Then End End If End If End Sub can someone please help me out with this... Thanks. Parth Patel

    D 1 Reply Last reply
    0
    • P parth p

      Hi, I recently started using VB2005 and i find it very confusing, in VB6 i used to unload form using UNLOAD ME but it doesn't work in 2005, plus i tried Me.Close() and Me.Dispose() as well but it closes my application, rather than form. Here's the code i'm working on. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim usname As String usname = StrConv(TextBox1.Text, VbStrConv.ProperCase) If usname = "Admin" And TextBox2.Text = "pass" Then MsgBox("Welcome to Form two", MsgBoxStyle.Information + MsgBoxStyle.OkOnly) Me.Close() Form2.Show() Else MsgBox("Wrong Login details", MsgBoxStyle.Exclamation + MsgBoxStyle.RetryCancel) If MsgBoxResult.Retry Then TextBox1.Text = "" TextBox2.Text = "" TextBox1.Focus() ElseIf MsgBoxResult.Cancel Then End End If End If End Sub can someone please help me out with this... Thanks. Parth Patel

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      VB6 taught you some bad habits you have to break. In VB6, all forms exist all the time. Under true OOP and VB.NET 2005, this is not the case. A form doesn't exist until your code creates an instance of it and shows it. Your code is then responsible for disposing of it when you're done with it. Forms are classes, just like any other class in the .NET Framework you create an instance of. Putting a Me.Close in your startup form will close your entire application because you just killed off the form that's holding your application's message pump. A form should not kill itself off. The code that created and launched it normally would do this. You cannot transfer control from one form to another like you're trying to do. Forms get created and destroyed in a tree-like fashion. Form1 creates Form2 and shows it, which may create Form3 and show it, which may create forms 4, 5, and 6. If the instance of Form2 dies, all the forms that it created and launched, and their child forms, die along with it. The only thing you can do to not show your Form1 and give the illusion that Form2 is now the main form is to Me.Hide(). In the case of a login form, there's a few different ways to do this. The first is to have a Sub Main launch the puts up a form and lets it check the credentials, then returns the login state to Main. Based on that result, Main then created a second form that is the main application form.

      <STAThread()> \_
      Shared Sub Main()
          Using loginForm As New MyLoginForm
              Application.Run(loginForm)
              If loginForm.DialogResult = Windows.Forms.DialogResult.OK Then
                  If loginForm.LoginSuccessful Then
                      Application.Run(New MyMainForm)
                  End If
              End If
          End Using
      End Sub
      

      Of course, LoginSuccessfulwould probably be a public property on the login form that this code checks to see if the credentials were good. It would probably also expose the credentials or some role information as a public property too.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      P 1 Reply Last reply
      0
      • D Dave Kreskowiak

        VB6 taught you some bad habits you have to break. In VB6, all forms exist all the time. Under true OOP and VB.NET 2005, this is not the case. A form doesn't exist until your code creates an instance of it and shows it. Your code is then responsible for disposing of it when you're done with it. Forms are classes, just like any other class in the .NET Framework you create an instance of. Putting a Me.Close in your startup form will close your entire application because you just killed off the form that's holding your application's message pump. A form should not kill itself off. The code that created and launched it normally would do this. You cannot transfer control from one form to another like you're trying to do. Forms get created and destroyed in a tree-like fashion. Form1 creates Form2 and shows it, which may create Form3 and show it, which may create forms 4, 5, and 6. If the instance of Form2 dies, all the forms that it created and launched, and their child forms, die along with it. The only thing you can do to not show your Form1 and give the illusion that Form2 is now the main form is to Me.Hide(). In the case of a login form, there's a few different ways to do this. The first is to have a Sub Main launch the puts up a form and lets it check the credentials, then returns the login state to Main. Based on that result, Main then created a second form that is the main application form.

        <STAThread()> \_
        Shared Sub Main()
            Using loginForm As New MyLoginForm
                Application.Run(loginForm)
                If loginForm.DialogResult = Windows.Forms.DialogResult.OK Then
                    If loginForm.LoginSuccessful Then
                        Application.Run(New MyMainForm)
                    End If
                End If
            End Using
        End Sub
        

        Of course, LoginSuccessfulwould probably be a public property on the login form that this code checks to see if the credentials were good. It would probably also expose the credentials or some role information as a public property too.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        P Offline
        P Offline
        parth p
        wrote on last edited by
        #3

        Dave Kreskowiak wrote:

        _ Shared Sub Main() Using loginForm As New MyLoginForm Application.Run(loginForm) If loginForm.DialogResult = Windows.Forms.DialogResult.OK Then If loginForm.LoginSuccessful Then Application.Run(New MyMainForm) End If End If End Using End Sub

        Thank you very much for your help, but can you please tell me where do i put this code. I'm student learning so i don't really know much about Visual Basic. Thanks Parth Patel

        D 1 Reply Last reply
        0
        • P parth p

          Dave Kreskowiak wrote:

          _ Shared Sub Main() Using loginForm As New MyLoginForm Application.Run(loginForm) If loginForm.DialogResult = Windows.Forms.DialogResult.OK Then If loginForm.LoginSuccessful Then Application.Run(New MyMainForm) End If End If End Using End Sub

          Thank you very much for your help, but can you please tell me where do i put this code. I'm student learning so i don't really know much about Visual Basic. Thanks Parth Patel

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Either in a Module (yuk!) or in your startup form's code. Then you go into your project properties and change the Startup item to Sub Main.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups