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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the "correct way" to start a program

Status
Not open for further replies.

JABOSL

Programmer
Jan 25, 2006
35
In all my years of programming in VB4 I always had form1 as the main/startup form. That isn't cutting it in VB .net because other forms can't refer to things on form1 by saying form1.button1 etc. Ruffnekk just suggested in a recent post to me that I should use a Sub Main module as the startup module in the project. I haven't seen this done anywhere before.

Is creating a Sub Main now the "correct" way to start a VB.net program?

Where do I put the code for sub main?

It looks like Ruffnekk created a module--did you go to project/add module/module to do this?
 
It the way I usually start programs, and there are countless examples on this forum of how to do this.


Hope this helps.

[vampire][bat]
 
I'm new to the forum. Can you point me to the examples?
 
I usually just press F5 [thumbsup2]

Sweep
...if it works dont f*** with it
curse.gif
 
Click the search button just above Read New Posts

Keywords: Sub Main
Scope: Visual Basic(Microsoft) -VB.NET Only
Use: Exact Phrase
Area: Forums
Fields: Full Text
When: Any Date

Click Search and ...

Hundreds of records matched your query of Sub Main.



Hope this helps.


[vampire][bat]
 
Two ways that i know of:

easy
1. Sub main procedure.

hard
2. Add that code:

Just add the blue in the constructor; the rest should exist.
Code:
	Public Sub New()
		MyBase.New()
[COLOR=blue]
		If m_vb6FormDefInstance Is Nothing Then
			If m_InitializingDefInstance Then
				m_vb6FormDefInstance = Me
			Else
				Try 
               If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
                  m_vb6FormDefInstance = Me
               End If
            Catch
				End Try
			End If
		End If
[/color]
      InitializeComponent()
	End Sub

Also:

Code:
	Public Shared Property DefInstance() As [b]Form1[/b]
		Get
			If m_vb6FormDefInstance Is Nothing OrElse m_vb6FormDefInstance.IsDisposed Then
				m_InitializingDefInstance = True
				m_vb6FormDefInstance = New Form1()
				m_InitializingDefInstance = False
			End If
			DefInstance = m_vb6FormDefInstance
		End Get
		Set
			m_vb6FormDefInstance = Value
		End Set
	End Property


Now you can do as in vb6.
 
I'm working with some code someone else wrote. They have
Code:
<STAThread()> Public Shared Sub Main(ByVal CmdArgs() As String)
  Application.Run(New Form1)
End Sub
at the end of the Windows Form Designer generated code region of Form1.

I just put
Code:
Module Module1
    Public Sub main()
        Dim mainForm As New Form1
        Application.Run(mainForm)
    End Sub
End Module
in a module.

They don't co-exist.

I'm not sure what their code does but I suspect it can be included in mine. Just don't know what to do. Help!
 
I've used all the above examples, and they all work well. What I've found to work the best is to have a public (sometimes Shared) writeonly property called SetActiveForm. I counter that with a readonly property called GetActiveForm.

In this manner, accessing the calling form is much easier.

Just a thought.

Ron

Ron Repp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top