Link to home
Start Free TrialLog in
Avatar of RobertFromSecretWeapons
RobertFromSecretWeapons

asked on

Me.Visible AND Me.WIndowState = WindowState.Normal doesn't work

Hi, damn this is irritating - please provide relief..LOL.

1 - I want my form to start up in the invisible state.  So, in the form_load event, I have Me.Visible = False (this does'nt work in the form_load event) (Please don't suggest Opacity as a solution as this doesn't disable the controls on the form face)  I am using a timer and making it invisible there, but there is a small slice of time where the initial load of the form will show the screen.

2 - My form is normally invisible, so when a certain event fires on the form, I need to make the form visible, so I make it visible, Me.Visible = True, this works, but if the form was previously minimized by the user, I cannot restore it to the normal state.  The following simply doesn't work - > Me.WIndowState = FormWindowState.Normal doesn't work  
How do I force a restore of the form regardless of previous state.

Some sanity greatly appreciated...

Avatar of geowrian
geowrian
Flag of United States of America image

Did you try Me.Activate?
Avatar of RobertFromSecretWeapons
RobertFromSecretWeapons

ASKER

When, and for which issue above...
I've done the hiding by capturing the sizechanged event of the form like this...

    Dim firstRun As Boolean = True
    Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
            If firstRun Then
                firstRun = False
                Me.Hide()
            End If
    End Sub

If you also set ShowInTaskbar to false and then later do Me.ShowInTaskbar = True before you show the form, that will eliminate a flicker of the form being shown in the taskbar.

To restore the form I use:

        Me.ShowInTaskbar = True
        If Me.Visible = False Then
            Me.Show()
        End If
        If Me.WindowState = FormWindowState.Minimized Then
            Me.WindowState = FormWindowState.Normal
        End If

Not sure why that wouldn't work for you.
Hi, let's work on the second issue first as it is a show stopper for me at this time.

I was doing the following in my main form timer event, (and it doesn't work)
If TimeForAction = True Then
If Me.WindowState <> FormWindowState.Normal Then
            Me.WindowState = FormWindowState.Normal
End If
End If
I know this block of code is executing because of a print statement I have. As a matter of fact, I have another print statement after this code and it still shows that the window state is STILL minimized.
I've seen some threads on other sites out there and other people have come across this problem as well.  Somebody else even resorted to using a windows message to send a WM_Restore message or something like that to provoke the action.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I didnt say opacity didn't work for me.  Just that I dont want to use it because the form controls are still exposed even though invisible.  Someone can click on them by accident not knowing the form is even there.

What can I do to force a form back to the normal state regardless of it's current state.  Like I said, the following DOESNT work:  In one of my previous statements, after I tried setting the form back to the normal state, I checked it's state and it was minimized.

If Me.WindowState <> FormWindowState.Normal Then
            Me.WindowState = FormWindowState.Normal
End If

So, maybe it boils down to this, it won't work unless the form is activated.  But - if that's the case, makes no sense at all.  I don't think I even want to activate the form, because that would steal focus away from another app that is currently in use.

If I can't figure this out, I am probably going to have to call a User32 or WIn32 function directly and do it there.  I am hoping I don't have to resort to that because I can't figure this out.
No...a form with 0 (zero) opacity cannot be interacted with by the user...how are you coming to this conclusion?

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Idle_Mind - Thx for the correction / clarification for the Opacity issue.  It seems that the Opacity method is the only way to start a form up invisible.  Setting the forms visible property at any time before or during a form_load doesn't work.

me655321 - We are setting up a test to see how the loigc behaves in a button event vs a timer event that I currently use.

Can anyone tell me why - under what conditions TheForm.WindowState = FormWindowState.Normal would not work at all.  In other words, if the form is in a certain state, that the above line would not restore the form.
I think WindowState property is only used at the startup of the form. Did you try using the TheForm.Activate method instead?
When you show and then try to restore the form, is it showing in the taskbar? And if so, can you manually restore it by clicking it in the taskbar?
I'm still doing some testing, but one of the things I may have come across is:

You need to set Form.Visible = True FIRST, before setting WIndowState.

It looks like unless the form is visible, that setting its windowstate may not actually have a reliable effect.
I made a simplified test app to test WIndowState and it always works.
I will get back to you with a final outcome.
Ok,
It does appear that if you are going to manipulate WindowState, you better do it with Visible=True first.
Once the order was changed, then it worked Ok.  In some spots, Visible was first, and in other spots, visible was after windowstate.  This is why there were odd results.