Tech Off Thread

11 posts

How to hide or make a vb.net form invisible at the load event of the form

Back to Forum: Tech Off
  • User profile image
    Ehtesham​Siddiqui

    Hi,

    My requirement is as soon as my form get load it should get hidden or invisible.The user should not be able to even get the look of the form.I have tried this at load event but its not working.

     Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    me.hide
    me.visible=false
    End Sub

    I have tried Me.Hide and Me.visible but its not working.Can i do it on load?

    Is there anyother way please suggest.

  • User profile image
    Cybermagell​an

    Why would you want to do this?

    This tactic sounds more like spyware/virus than any feasible reason....but hmm

  • User profile image
    spivonious

    @EhteshamSiddiqui: If the user won't be able to see the form, why make a form at all?

  • User profile image
    itsnotabug

    There may be legit reasons to do this if you want to load forms in a non-modal way while hiding the calling form. For example if you had a log-in form you could do something like (untested)

    Dim myForm as New frmMenu() 'go to the menu after logging in
    Me.Hide() ' temporarily hide the log in form
    myForm.ShowDialog()
    Me.Show()  ' when user closes the menu, show the form again

     

  • User profile image
    itsnotabug

    nevermind i just reread the op. why not just do a console app?

  • User profile image
    evildictait​or

    There are legitimate reasons to do this - for example you can't create a tray icon without having a HWND to attach it to, but you might want your form to be visible when you click the tray icon and be invisible by default.

    Unfortunately you can't set Visible to false during the constructor or the Load() event, but you can do a couple of things:

    * Create a one shot timer for 100ms - start the timer during the Load() event and have the timer make the form invisble.

    * You can bash the CreateParams() method to make the form invisible by default.

    * You can override the form's WndProc and prevent the form from showing during the WM_CREATE command.

  • User profile image
    AndyC

    Visual Basic always shows the main form after completing the Load event (unlike C#), which is why your attempts to hide it go unnoticed. There is another event that fires soon after from what I recall which you can use instead, can't remember what it is off the top of my head though and it's far too late to be digging around in VS.

  • User profile image
    davewill

    @EhteshamSiddiqui: This seems odd.  Why show the form to begin with if it isn't to be seen.  Just create it (i.e. new) but don't show it.  If this is the only form in your project add a Sub main and set it as the startup of the project.  This way in sub main you can control when the form is created and when it is shown.  Beyond that if you really want to hide after showing you could do an addhandler for the onloaded, onactivated, onvisiblechanged, etc. event(s) and rehide depending on the more specifics of just why this scenario came into existence in the first place.

  • User profile image
    TonK

    I use the me.visible property when I create an application which starts minimized in the system tray.

    A simple way is to put the code in the 'shown' event of the main form.

    Example:
    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
    Me.Visible = False
    End Sub

  • User profile image
    nCoded

    This might help:

    Change the WindowState property to Minimized and ShowInTaskbar property to False.
    Add Me.Hide() to the Form_Load sub.

  • User profile image
    figuerres

    Just one more thing that seems to have been overlooked:

    set the location of the form so that even if it's "shown" for a second it's not in the visible area of the screen...    like  -5000,-5000     then later change the location property to a value like 200,200

    just be sure to check the users desktop size and screen count info.... i hate programs that assume all new windows belong inside screen zero when i have two monitors and the app is in screen one.

    if the parent app is in screen one then put new windows in that screen please !!!

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.