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!

Form is closing automatically

Status
Not open for further replies.

dg043

Programmer
Apr 22, 2002
52
0
0
GB
I have a simple Windows Forms based application which I am trying to upgrade from Visual Basic 6.0 to Visual Basic .NET. At the moment, all I am trying to do is open a form from a module. That is, I have a form called frmProgress and a module called modBas. In modBas is the following code:

[tt]Module modBas
Public Sub Main()
Dim myform As New frmProgress

myform.Show()
End Sub
End Class[/tt]

This does indeed show an instance of the frmProgress form but it then closes it again and stops the project!? In Visual Basic 6.0, it would leave the form open (thus the project would still be running, even though it wasn't doing anything).
Is this behaviour by design? Is there any way I can force the form to stay open? I don't want to use the .ShowDialog function because then the code doesn't move onto the next line.

Dan Griffiths
Software Analyst
National Grid plc
 
The form is closed because it goes out of scope with the Sub Main. Because the variable myform is defined inside the Main Sub, when the Main Sub completes (goes out of scope), any variables in it will go out of scope with it. In this case, that closes the form.

You might try this instead:
Code:
Module modBas
    Public myform As New frmProgress
    Public Sub Main()
        myform.Show()
    End Sub
End Class

I am still not sure that will work (typed, not tested) but I thik it should. This defines the variales myform OUTSIDE of the scope of the Main Sub. It is scoped with the MODULE...which should be scoped with your actual application.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thanks for your suggestion, but I have already tried this and the same thing happens. It would appear that starting a windows forms application of this type through Sub Main is the wrong way to go ...

Dan Griffiths
Software Analyst
National Grid plc
 
use myform.ShowDialog() instead of myform.Show()


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
try

Code:
<STAThread()> _
Shared Sub Main()     
   ' Starts the application.
   Application.Run(New Form1())
End Sub

Sub button1_Click(sender As object, e As System.EventArgs)
   ' Populates a list box with three numbers.
   Dim i As Integer = 3
   Dim j As Integer
   For j = 1 To i - 1
      listBox1.Items.Add(j)
   Next

   ' Checks to see whether the user wants to exit the application.
   ' If not, adds another number to the list box.
   While (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) = _ 
      DialogResult.No)
      ' Increments the counter and adds the number to the list box.
      i = i + 1
      listBox1.Items.Add(i)
   End While

   ' The user wants to exit the application. Close everything down.
   Application.Exit()
End Sub

from here


Christiaan Baes
Belgium

My Blog
 
Thanks for your suggestions. They have started me thinking in another direction. It would appear that Application.Run performs exactly the same as myform.ShowDialog. That said, you can achieve further processing after this by putting code into the _Activate event of the form.
The answer appears to be a combination of all your suggestions. The modBas module now contains:

Code:
Module modBas
    Dim myForm As New frmProgress

    Sub main()

        myForm.ShowDialog()

    End Sub

    Public Sub SendToMIPI()

        myForm.TextBox1.Text = "Hello"

    End Sub
End Module

and the form frmProgress contains:

Code:
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated

        Me.TextBox1.Text = "Hello"

    End Sub

The module shows the form at application start, the form then calls a subroutine in the module.

Dan Griffiths
Software Analyst
National Grid plc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top