Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sub Main and opening forms

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
0
0
US
I am attempting (LOL) to create an application where I open a form from Sub Main in a module. This form opens another form then closes/hides itself. When the second form is finished, or user presses button, then it closes and the first form opens itself.

I have not got this to work, so I created the project in VB6. It worked. I upgraded it in the wizzard and it no longer works. What am I doing wrong? This is a simple project!


It tells me when I click on the link that module main will exit and the program exits too. Ok. My question is how are you all making this work?
Do I start with the main form and not sum main in the module? Do I want to use a modal form?
Both forms can not be visible at the same time.

Help please. Or a link?
An example would be better then chocolate. :)


VB6 Project:
modMain.bas

Public Sub Main()
frmMain.Show
End Sub

frmMain.frm
Private Sub btnShowFormNext_Click()
frmNext.Show
Unload Me
End Sub

frmNext.frm
Private Sub btnShowFormMain_Click()
frmMain.Show
Unload Me
End Sub


VB.NET Project
Option Strict Off
Option Explicit On
Module modMain
'UPGRADE_WARNING: Application will terminate when Sub Main() finishes. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1047"'

Public Sub Main()
frmMain.DefInstance.Show()
End Sub
End Module

Option Strict Off
Option Explicit On
Friend Class frmMain
Inherits System.Windows.Forms.Form
#Region "Windows Form Designer generated code "
Public Sub New()
MyBase.New()
If m_vb6FormDefInstance Is Nothing Then
If m_InitializingDefInstance Then
m_vb6FormDefInstance = Me
Else
Try
'For the start-up form, the first instance created is the default instance.
If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
m_vb6FormDefInstance = Me
End If
Catch
End Try
End If
End If
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents btnShowFormNext As System.Windows.Forms.Button
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmMain))
Me.components = New System.ComponentModel.Container()
Me.ToolTip1 = New System.Windows.Forms.ToolTip(components)
Me.ToolTip1.Active = True
Me.btnShowFormNext = New System.Windows.Forms.Button
Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
Me.Text = "Form1"
Me.ClientSize = New System.Drawing.Size(382, 243)
Me.Location = New System.Drawing.Point(161, 169)
Me.Font = New System.Drawing.Font("Arial", 8!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.Control
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable
Me.ControlBox = True
Me.Enabled = True
Me.KeyPreview = False
Me.MaximizeBox = True
Me.MinimizeBox = True
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.ShowInTaskbar = True
Me.HelpButton = False
Me.WindowState = System.Windows.Forms.FormWindowState.Normal
Me.Name = "frmMain"
Me.btnShowFormNext.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.btnShowFormNext.Text = "Show Form Next"
Me.btnShowFormNext.Size = New System.Drawing.Size(101, 33)
Me.btnShowFormNext.Location = New System.Drawing.Point(24, 20)
Me.btnShowFormNext.TabIndex = 0
Me.btnShowFormNext.Font = New System.Drawing.Font("Arial", 8!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnShowFormNext.BackColor = System.Drawing.SystemColors.Control
Me.btnShowFormNext.CausesValidation = True
Me.btnShowFormNext.Enabled = True
Me.btnShowFormNext.ForeColor = System.Drawing.SystemColors.ControlText
Me.btnShowFormNext.Cursor = System.Windows.Forms.Cursors.Default
Me.btnShowFormNext.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.btnShowFormNext.TabStop = True
Me.btnShowFormNext.Name = "btnShowFormNext"
Me.Controls.Add(btnShowFormNext)
End Sub
#End Region
#Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As frmMain
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As frmMain
Get
If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New frmMain()
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set
m_vb6FormDefInstance = Value
End Set
End Property
#End Region
Private Sub btnShowFormNext_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles btnShowFormNext.Click

frmNext.DefInstance.Show()

Me.Close()
End Sub
End Class

 
The simplest thing is to make frmMain the Startup object in the project properties and get rid of Sub Main.

What's happening is that Sub Main does show the form but only for an instant because it then immediately exits because it will carry on the the next statement as you didn't use the ShowDialog method to open frmMain.

 
I have gotten a vast education since this morning. VB.net is NOTHING like VB6 except for some of the syntax.

Thanks for the reply.
 
Look into the Application.Run and Application.Exit statements. They helped me a lot in terms of program flow. I also came from the VB6 world.

-Elijah
 
This is working for me:

Dim Excep As Exception
Public frm As frmMAIN

Sub Main()
Try

frm = New frmMAIN
Application.Run(frm)

Catch Excep As Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End Try
End Sub
 
Does Application.Run(frm) start a new thread?
Does it still share global variables?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top