vb.net startup/form load

B

Brian Fulford

This is my first shot at a Winforms application in dot net. This application
is mainly going to run unattended but is using a form to display current
activity of the background processing. In VB6, I could set the startup to be
Sub Main and then call frmMain.Show and the program would continue
processing; however, after instantiating the from in VB.Net - dim frmMain as
new frmMain, frmMain.show shows the form then closes the form to continue
processing - which is counter to my purposes... so I tried
Application.Run(frmMain). This stops program execution until the used does
something.

SO... any help with that? Also, I looked to see if there was a
frmMain_loaded event to call a public sub but I did not see any event that
is fired after the form is loaded and shown.

Any insight would be appreciated.

Brian
 
B

Bernie Yaeger

Hi Brian,

What code closes the form? If you call the form and nothing in the form's
load event closes it, it should remain open. Can we see some code?

Bernie Yaeger
 
B

Brian Fulford

The form should remain open until someone closes it with the 'X' or until an
error causes an unrecoverable exception.

I have the following code in a module
*****************************************

Public WithEvents frmMain As New frmMain
Sub Main()

Application.Run(frmMain)

startConfiguration()

startExecution()

End Sub

*****************************************

The FORM is basically a txtbox/treeview that will contain error messages or
the contents of the threads that are currently working. There is a close
button on the form so that the program execution can be interrupted and
processing cautiously shut down via thread aborts etc. At least that is the
plan thus far!

I want the form to be shown, and then have processing continue in the sub
main().
 
B

Brian Fulford

What would actually be preferable would be to have the FORM be the startup
object.. and after the form is loaded.. to call startConfiguration with no
user click event... how do you know when the form is finished loading in
VB.Net?
 
B

Bernie Yaeger

Hi Brian,

You can make the form's vb code the startup routine - go into the project
menu, [project name] properties etc. A form is loaded when the load event
concludes. I still don't see enough code to explain what is happening.

Bernie Yaeger
 
P

Peter Huang

Hi Brian,

I think we can run a invisible winform first to enable the message pump
which will drive the application code, which is different from VB6. Because
VB.NET is based on .NET framework which has the different behavior from VB6.

For your scenario, we can use the code as below.
[Module]
Module Module1
Sub Main()
Dim fm As New Form1
fm.Visible = False
fm.WindowState = Windows.Forms.FormWindowState.Minimized
System.Windows.Forms.Application.Run(fm) 'Enable the message pump,
so that the thread will not exit after the other form is closed.
End Sub
End Module

[Form1]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Visible = False
Dim fm As New Form2
fm.Show()
MsgBox("Hello")
End Sub

[Form2]
Private Sub Form2_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Closed
System.Windows.Forms.Application.Exit()
End Sub

You may have a try to let me know if that works for you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Larry Serflaten

Brian Fulford said:
The form should remain open until someone closes it with the 'X' or until an
error causes an unrecoverable exception.
I want the form to be shown, and then have processing continue in the sub
main().


See if this helps, in a new poject, add a module and paste in the code below
then, set the project Startup object to the Sub Main routine. As shown, the
module processing updates the form's caption with the current time. If
you don't see the time displayed, then you forgot to set the Startup object.

LFS


' [ FORM1 CODE ]
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
Done = True
End Sub

Private Sub Form1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Click
' Unhandled exception (Divide by 0)
Dim a, b, c As Integer
a = b \ c

End Sub





' [ MODULE1 CODE ]

Public Done As Boolean
Public Display As Form1

<STAThread()> Sub Main()
Display = New Form1
Display.Show()

Do While Not Done
Processing()
Try
Application.DoEvents()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error Caught!")
End Try
Loop

' Clean up code
MsgBox("Clean up complete")

End Sub

Private Sub Processing()
' Update display
Display.Text = Now.ToLongTimeString
System.Threading.Thread.Sleep(250)
End Sub
 
B

Brian Fulford

Larry, thanks for the heads up on this.I suppose I just needed that DO ...
Loop with the public boolean to make it work like I wanted.

Sub Main()

MainFormDisplay = New frmMain

MainFormDisplay.Show()

Try

If startConfiguration() Then

startExecution()

Do While Not doExit

Application.DoEvents()

Loop

Else

Do While Not doExit

Application.DoEvents()

MainFormDisplay.txtDisplay.Text = lastError

System.Threading.Thread.Sleep(25)

Loop

End If

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error Caught!")

End Try

End Sub



Larry Serflaten said:
Brian Fulford said:
The form should remain open until someone closes it with the 'X' or until an
error causes an unrecoverable exception.
I want the form to be shown, and then have processing continue in the sub
main().


See if this helps, in a new poject, add a module and paste in the code below
then, set the project Startup object to the Sub Main routine. As shown, the
module processing updates the form's caption with the current time. If
you don't see the time displayed, then you forgot to set the Startup object.

LFS


' [ FORM1 CODE ]
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As
System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
Done = True
End Sub

Private Sub Form1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Click
' Unhandled exception (Divide by 0)
Dim a, b, c As Integer
a = b \ c

End Sub





' [ MODULE1 CODE ]

Public Done As Boolean
Public Display As Form1

<STAThread()> Sub Main()
Display = New Form1
Display.Show()

Do While Not Done
Processing()
Try
Application.DoEvents()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OKOnly, "Error Caught!")
End Try
Loop

' Clean up code
MsgBox("Clean up complete")

End Sub

Private Sub Processing()
' Update display
Display.Text = Now.ToLongTimeString
System.Threading.Thread.Sleep(250)
End Sub
 
P

Peter Huang

Hi Brian,

If you still have any concern on this issue please feel free to post here.
Also have you tried my suggestion? Do you have any concern?


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top