Exit from form and continue the rest of program

kaveh rahimi 61 Reputation points
2021-05-23T21:11:47.187+00:00

Hi ,how can I get out of form and go on to the program?
Please explain me by example.
Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,612 questions
{count} votes

2 answers

Sort by: Most helpful
  1. JasonDaurison 116 Reputation points
    2021-05-24T01:50:02.38+00:00

    Try this:

    Private Sub btnLoadForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadForm.Click
            Dim Form2 As New Form
    
            Form2.ShowDialog()
            'Code placed after this will be executed after the form is closed.
        End Sub
    

    Or if you want to show the form and also execute code at the same time, try this:

    Private Sub btnLoadForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadForm.Click
                Dim Form2 As New Form
    
                Form2.Show()
                'Code placed after this will be executed while the form is still there. 
        End Sub
    
    0 comments No comments

  2. Peter Fleischer (former MVP) 19,306 Reputation points
    2021-05-24T13:21:49.927+00:00

    Hi,
    you can use console application and show Form (eg. Form1) and after closing form continue with next code like in following demo:

    Imports System.Windows.Forms
    
    Module Module1
      Sub Main()
        Try
          Dim c As New Demo
          c.Execute()
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
        Console.WriteLine("Continue enter key")
        Console.ReadKey()
      End Sub
      Friend Class Demo
        Friend Sub Execute()
          Application.EnableVisualStyles()
          Application.SetCompatibleTextRenderingDefault(False)
          Application.DoEvents()
          Application.Run(New Form1())
          '
          ' next code after closing Form1
          Console.WriteLine("next code")
        End Sub
      End Class
    
      ''' <summary>
      ''' Form1 for demo purposes
      ''' </summary>
      Friend Class Form1
        Inherits Form
        Private Label1 As New Label() With {.Text = "Form 1 loaded"}
        Public Sub New()
          AddHandler Me.Load, Sub()
                                Me.Controls.Add(Label1)
                              End Sub
        End Sub
      End Class
    
    End Module
    
    0 comments No comments