Starting the Application

  • Thread starter Thread starter littledump
  • Start date Start date
L

littledump

Guest
Ok i think i know what my problem is & how to fix it if someone just answers this one question. How do i start an app so that no form is specified as the startup form, but i can show a form right after its started.

Heres what I have right now but doesnt work

Code:
Module Hi
    Public Sub Main()
           System.Windows.Forms.Application.Run(Myform)
    End Sub
End Module


Im sure this is probably an easy question & anyone who helps, thank you.
 
Try this instead:

Code:
Module Hi
    Public Sub Main()
           Application.Run(New Myform())
    End Sub
End Module

The key is the New keyword. After all, you want an instance of the form, not the name of it.
 
Forgot to mention

I forgot to add something to the top of that


Code:
Module Hi
    
     Public Myform as New Form1

     Public Sub Main()
            System.Windows.Forms.Application.Run(Myform)
     End Sub

End Hi

the problem is whatever form i put in the Run() method, i can never hide that form. it comes back. i can hide any other form though. thats why i want to know other ways of starting in sub main().
 
'Myform.ShowDialog()'

*Should Work*

The Application.Run(FormName)

Is a pretty messy way to run forms, because you can only have one open that way @ a time, or you can use that to start then use the ShowDialog to open the rest, but remember again you can only have one Run per thread :)

Hope ThiS hElPs :)

Any more questions AiM me, I am still learning to so i like to seach out others problems :)
 
Ok. is that all i put in Main() and nothing else? Will i then be able to hide that form and show another?
 
Ok I think this part of VB.Net needs some explaining due to the overwhelming amounts of posts in numerous forums to be geared towards this problem.

*Note: I would like to say that I am leaving my self out to be criticized by all my friends/co-workers, but since I am off work now I can take more time and explain this to you, which will probably be overkill :)*

When you call a program to start in 'Sub Main' its starts off as normal but you have to remember that VB.NET is now single threaded unless other wise called, so if you use

Code:
Dim oSplash as New SplashScreen

Sub Main
    Call oSplash.Show()
End Sub

The program runs, shows the form for a split second (or how ever long it takes to load your form), then it comes back to 'Sub Main' at the end of the 'Call' and continues till 'End Sub' is reached and thus your program has ended so VB.NET cleans up all the garbage and "unloads" your program.

Our Next Example is with the run command

Code:
Imports System.Windows.Form.Application
(...)

Dim oSplash as New SplashScreen

Sub Main
    Call Run(oSplash)
End Sub

Now this one runs just fine to people who are loading just one form, or people who will never de-allocated the memory that is used in oSplash. The run command does not leave a return memory call on the stack, so that once your program has terminated there is actually an error. That error doesn’t actually show up on the screen because it is taken care of in the clean up, this error comes from the fact that Windows does not know where in the code you want to return to, thus it becomes confused and faults, UNLESS you tell your code where to go when the memory has been de-allocated.


Code:
Dim oSplash as New SplashScreen

Sub Main
    Call oSplash.ShowDialog()
End Sub

This one will load the form, and leave the memory return on the stack. So this one will run the form and as long as you don’t de-allocated the memory of the form it will continue to run, and once the memory is de-allocated it will return to the point just below the line that made the call.

There also seems to be some confusion in what the '.Hide' actually does in your eyes. '.Hide' actually de-allocated the memory. There for when you are using this call it comes out of the 'Run' with an error and it cleans up after its self. What you actually want to use is the '.Visible' property that “hides” the form from the users eye. This allows you to keep control over the program and do what you wish while having your main form invisible.

Hope This Helps

--Perl
-- LbS @ http://www.PcTechForums.com
 
Actually changing the visible property does the same thing. What the problem is, is when I try to set the visible property of the form that i use Run() on, as soon as the Sub that I change the property in exits the form comes back onto the screen. This only happens for the form that I use Run() on. Try this in VB.NET and suggest how without making major changes I can make it run correctly.

Code:
Module VarsDimsEtc

     Public SplashForm as New Form1()
     Public MainForm as New Form2()
     
     Public Sub Main()
         System.Windows.Forms.Application.Run(SplashForm)
     End Sub

End Module

Public Class Form1
      Inherits System.Windows.Forms.Form
     
     +/Windows Form Generator Code\

     Private Sub Frm_Load (sender as System.Object, e as System.EventArgs) Handles MyBase.Load
          SplashForm.Visible = True
          [STATEMENT BLOCK]
          MainForm.Visible = True
          SplashForm.Visible = False
     End Sub
End Class

Public Class Form2
     Inherits System.Windows.Forms.Form

     +/Windows Form Generator Code\

     Private Sub Frm_Load (sender as System.Object, e as System.EventArgs) Handles MyBase.Load
               [STATEMENT BLOCK]
     End Sub
End Class

I know there are some obvious things that you think may work but most likely I have tried them. So please set up a program like this and try it.
 
Code:
*

     Public SplashForm as New Form1()
     Public MainForm as New Form2()
     
     Public Sub Main()
         System.Windows.Forms.Application.Run(SplashForm)
     End Sub

*

Like i said before try not using the Run Command... turn it into...

Code:
*

     Public SplashForm as New Form1()
     Public MainForm as New Form2()
     
     Public Sub Main()
          Call SplashForm.ShowDialog()
          Call MainForm.ShowDialog()
     End Sub
*

This will wait till you have completed what you need to do in the 'SplashForm' then once it is complete it will Run Your 'MainForm'
 
this doesnt work. i still cant get the splash form to hide at the point i want it. i did once but then when i close the main form it comes back. when i use showdialog, the splash shows but wont hide then the main shows on top of the splash

the splash wont hide
 
I tried the code above (with ShowDialog) and it works perfectly. Perhaps, I misunderstand what you are trying to do?
 
About 'Call'...

You are right there is no need to have it, you just have to understand the job i am @ and how pickey they are, call makes it known that it is a function call. that is why they like us using it :p any ways its good format practice :)
 
nope dont need splash later but ive tried both .close & .dispose & its still there. mebbe if u pm me ur email ill email u the actual prog i am working on & you can take a look. i just wanna be able to hide :(
 
Back
Top