[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1865
  • Last Modified:

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...

0
RobertFromSecretWeapons
Asked:
RobertFromSecretWeapons
  • 6
  • 3
  • 2
  • +2
2 Solutions
 
geowrianCommented:
Did you try Me.Activate?
0
 
RobertFromSecretWeaponsAuthor Commented:
When, and for which issue above...
0
 
me655321Commented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RobertFromSecretWeaponsAuthor Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure why Opacity() isn't working for you...

At design-time, I set Opacity to 0 (zero) and ShowInTaskBar to False and the form is COMPLETELY invisible on start.

I added a Timer to the Form just to make it pop back up:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 5000
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        ShowForm(True)
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            ShowForm(False)
            Me.Timer1.Start() ' just to make it pop back up after 5 seconds...
        End If
    End Sub

    Private Sub ShowForm(ByVal MakeVisible As Boolean)
        If MakeVisible Then
            Me.Show()
            If Me.WindowState = FormWindowState.Minimized Then
                Me.WindowState = FormWindowState.Normal
            End If
            Me.ShowInTaskbar = True
            Me.Opacity = 1
        Else
            Me.ShowInTaskbar = False
            Me.Hide()
            Me.Opacity = 0
        End If
    End Sub

End Class

Open in new window

0
 
RobertFromSecretWeaponsAuthor Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No...a form with 0 (zero) opacity cannot be interacted with by the user...how are you coming to this conclusion?

0
 
me655321Commented:
I set up a test project with 2 forms. Form2 has 2 buttons to show/hide Form1...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Visible = True
        Form1.WindowState = FormWindowState.Normal
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form1.Visible = False
    End Sub

Even when I minimize Form1 before hiding, the show function still works perfectly. Is this different from what you're doing because it seems to work. I'm using vs2008, maybe there is a glitch in some other version? Maybe try this test to see if it works.
0
 
RobertFromSecretWeaponsAuthor Commented:
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.
0
 
CodeCruiserCommented:
I think WindowState property is only used at the startup of the form. Did you try using the TheForm.Activate method instead?
0
 
me655321Commented:
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?
0
 
RobertFromSecretWeaponsAuthor Commented:
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.
0
 
RobertFromSecretWeaponsAuthor Commented:
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.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now