Tech Off Thread

19 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Hide Startup Form On Load - VB.NET

Back to Forum: Tech Off
  • User profile image
    anthq11

    Im using VB 2005, and I have a setting in an INI file (actually BLS, but it has the INI structure) which tells the application whether to show the configuration (startup) form when the application is started, or to just show the tray icon.  It correctly returns the values, but it WILL NOT close the form when putting it in the Form_Load Event Handler.

    Thanks!

    Would creating a sub in a class help, and just calling the sub from Form_Load help?

  • User profile image
    W3bbo

    I'd create the Main() function in a module then read the configuration file before calling Application.Run()

  • User profile image
    Sven Groot

    W3bbo wrote:
    I'd create the Main() function in a module then read the configuration file before calling Application.Run()

    That's the proper way to do it if you don't want to create the form at all, if you want to create it but keep it hidden it's not.

    Also, if you're using the application framework in VB2005 defining your own Sub Main can lose you a lot of functionality.

  • User profile image
    anthq11

    I checked out the ApplicationEvents.vb and even on startup it didnt work.  What do I name the module: Im using this in a module called Main:

    Module Main

    Dim ini_io As New IniFile(CurDir() & "\cs.bls")

    Sub Main()

    With ini_io

    If .GetString("Application", "Show Configuration On Startup", "") = "True" Then

    mainfrm.rstsas.Checked = True

    mainfrm.Show()

    Else

    mainfrm.rstsas.Checked = False

    mainfrm.ni.Visible = True

    mainfrm.ni.ShowBalloonTip(3000)

    mainfrm.Visible = False

    mainfrm.Tag = "0"

    End If

    MsgBox(.GetString("Application", "Show Configuration On Startup", ""))

    End With

    End Sub

    End Module

    Why dont I see the Module in the Project Properties?

  • User profile image
    W3bbo

    ...how can you have a private variable inside a module? Morerather, how can you initialise it? modules don't have constructors.

  • User profile image
    anthq11

    W3bbo wrote:
    ...how can you have a private variable inside a module?


    Is "Sub Main()" intialized as a "Private Sub Main()"?

    W3bbo wrote:
    Morerather, how can you initialise it?


    My question to you!

    W3bbo wrote:
    modules don't have constructors.


    What constructors do I have?

    I havent really used the Sub Main stuff, only VB6.

    ______

    Another question W3bbo -> people mentioned WEFT in the post I talked with you before on - I wont work with me...(http://www.q-kid.net/greco) All text is supposed to be "Micro" font. (http://www.q-kid.net/greco/MICRO0.EOF)

  • User profile image
    W3bbo

    anthq11 wrote:
    W3bbo wrote:...how can you have a private variable inside a module?


    Is "Sub Main()" intialized as a "Private Sub Main()"?


    No, a VB "module" is just a bunch of static functions that are available globally. When this is compiled it gets turned into a sealed abstract class with static methods, references to the functions within are converted accordingly.

    My question was that you had a private variable inside the module, now that I remember better, I recall that this becomes a static field available to the static functions inside the module.

    anthq11 wrote:


    W3bbo wrote:Morerather, how can you initialise it?


    My question to you!


    I don't know the specifics, but IIRC the "New" statement is processed when the application first runs.


    anthq11 wrote:

    W3bbo wrote:modules don't have constructors.


    What constructors do I have?

    I havent really used the Sub Main stuff, only VB6.


    VB6 supported Sub Main.

    anthq11 wrote:

    ______

    Another question W3bbo -> people mentioned WEFT in the post I talked with you before on - I wont work with me...(http://www.q-kid.net/greco) All text is supposed to be "Micro" font. (http://www.q-kid.net/greco/MICRO0.EOF)


    Huh?

  • User profile image
    dotnetjunkie

    Why make it so difficult?

    Just do

    Me.Visible = False

    in the Form_Load !!

  • User profile image
    Sven Groot

    W3bbo wrote:
    anthq11 wrote:
    W3bbo wrote: ...how can you have a private variable inside a module?


    Is "Sub Main()" intialized as a "Private Sub Main()"?


    No, a VB "module" is just a bunch of static functions that are available globally. When this is compiled it gets turned into a sealed abstract class with static methods, references to the functions within are converted accordingly.

    You can have a private method in a module. It's similar to having a private shared method in a class; it's accessible only by other methods in the module.

    W3bbo wrote:
    My question was that you had a private variable inside the module, now that I remember better, I recall that this becomes a static field available to the static functions inside the module.

    Indeed. And if you define a public field in the module, this is pretty much the equivalent of a global variable.

    W3bbo wrote:

    anthq11 wrote:

    W3bbo wrote: Morerather, how can you initialise it?


    My question to you!


    I don't know the specifics, but IIRC the "New" statement is processed when the application first runs.

    Static member fields (which includes members of modules in VB) are initialized when a types static constructor is executed. When this happens depends on whether or not the beforefieldinit attribute is present on the type. If the attribute is not present, the static constructor will be invoked at a precise moment: before the first access to a static member of the type, or the creation of the first instance. This leads to the JIT compiler adding static constructor checks to each and every way to access the type, which can induce a runtime overhead. When beforefieldinit is present, the JIT compiler can execute the static constructor whenever it wants (which means it can optimize aggressively) as long as it's before the first use of static data contained in the type. The static constructor checks are not necessary in this case.

    All MS compilers for .Net will add the beforefieldinit attribute to a class if it doesn't define an explicit static constructor (ie. all static member initializations are inline). If you do define an explicit static constructor (in VB this is Shared Sub New or just Sub New in a module) beforefieldinit is not added.

    Anthq11, in reference to your problems with Sub Main. The name of the module it is in doesn't matter (I usually call it Program, but use whatever you like). However, you must in the project properties in Visual Studio indicate you want to use it for it to be activated. On the Application tab of the project properties in VS2005 you see a dropdown labeled "startup object". Set this to "Sub Main" (note that you need to uncheck "Enable application framework" for Sub Main to appear in the dropdown).

    EDIT:
    dotnetjunkie wrote:

    Why make it so difficult?

    Just do

    Me.Visible = False

    in the Form_Load !!


    That doesn't work. When you create a new form and call ShowDialog or Show for the first time (this also happens when using Application.Run), Form_Load is executed first and then the form is shown, causing Visible to be set to true after Form_Load executes regardless of what you set it to there.

  • User profile image
    anthq11

    Try it with VB 2005, it doesnt hide...

    And with VB6 i mean Sub Mains in VB6...

    Whast the bad variable i got?

    W3bbo wrote:


    anthq11 wrote:
    ______

    Another question W3bbo -> people mentioned WEFT in the post I talked with you before on - I wont work with me...(http://www.q-kid.net/greco) All text is supposed to be "Micro" font. (http://www.q-kid.net/greco/MICRO0.EOF)


    Huh?


    http://channel9.msdn.com/ShowPost.aspx?PostID=168203#168203

  • User profile image
    Khamul

    Sven Groot wrote:

    dotnetjunkie wrote:

    Why make it so difficult?

    Just do

    Me.Visible = False

    in the Form_Load !!


    That doesn't work. When you create a new form and call ShowDialog or Show for the first time (this also happens when using Application.Run), Form_Load is executed first and then the form is shown, causing Visible to be set to true after Form_Load executes regardless of what you set it to there.



    Do the wrong thing! Use a timer, then blank out the form!!!

    On second thoughts... Tongue Out


    This is why I use C#, incidently. I'd say hack around in a custom Sub Main().

  • User profile image
    Sven Groot

    anthq11 wrote:
    Whast the bad variable i got?

    None. W3bbo was simply confused about the ini_io variable being in module scope, but it's perfectly legal.

  • User profile image
    Sven Groot

    Khamul wrote:
    This is why I use C#, incidently.

    What you suggested also doesn't work in C#. It's a Windows Forms thing, not a language specific thing.

  • User profile image
    Khamul

    Sven Groot wrote:
    Khamul wrote: This is why I use C#, incidently.

    What you suggested also doesn't work in C#. It's a Windows Forms thing, not a language specific thing.


    I wasn't referring to my suggestion (which is, incidently, the absolute worst way to go about it!) - I was referring to the inherent ease in C# to reprogram Sub Main() (or Void Main(), whatever the case may be).

    In C# I'd just change it to:

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    bool booleanValue;

    //get value of booleanValue

    if (booleanValue)
    {
        Form1 f1 = new Form1();
        Application.Run();
    }
    else
    {
        Application.Run(new Form1());
    }

    But:

    Sven Groot wrote:


    Also, if you're using the application framework in VB2005 defining your own Sub Main can lose you a lot of functionality.


    I used to use VB.Net all the time. Moving to C# was like seeing the light - VB.Net reminds me of Pseudo Code.

  • User profile image
    AndyC

    Putting it in the Shown event seems to work, although you get a nasty flicker because the form gets drawn briefly. I think personally I'd be inclined to tweak Main to get it to work cleanly.

  • User profile image
    anthq11

    Thanks for all the quick replies!

    The timer and Show thing would be about the same, with the flicker.

    Im in a pinch for time so Ill post back later about what happens!
    Thanks!

  • User profile image
    Mauricio Feijo

    Alternatively, you can set transparency to 100%, or set coordinates to 0,0,0,0.  The actual problem is that you chose a windows app. I remember having that trouble and found a way out, but can't remember what it was. as I do I will post.
    Thanks.
    M

  • User profile image
    rickstrand

    Heya,

    try this, it works for me,

    public Form1()

    {

    InitializeComponent();

    this.ShowInTaskbar = false;

    this.WindowState = FormWindowState.Minimized;

    }

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.