Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Hide a Windows form

Reply
 
Old 05-15-2007   #1 (permalink)
Pav


 
 

Hide a Windows form

Hello,

I have a powershell app that i'd like to hide/show with a systray icon
menu. Unfortunately, My script quits as soon as i call the Hide()
method of the form.
I'm not really experienced in .NET windows classes, Perhaps the
problem doesn't involve powershell at all. If so, feel free to
redirect me toward the good group.

Here's the main structure of my script :

----- cut here ------
$form = new-object
System.Windows.Forms.form

$mi = new-object System.Windows.Forms.MenuItem
$mi.Index = 0
$mi.Text = "Hide"
$mi.add_Click({

$form.hide()
})

$cm = new-object System.Windows.Forms.ContextMenu
$form.contextMenu = $cm
$form.contextMenu.MenuItems.AddRange($mi)

$form.ShowInTaskbar = $true
$form.set_autosize($true)
$form.Text = "My Form"

$ni = new-object System.Windows.Forms.NotifyIcon
$ni.Icon = New-object System.Drawing.Icon("icon.ico")
$NI.ContextMenu = $cm
$NI.Text = "Systray Text"
$NI.Visible =
$True;

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 5000

$timer.add_Tick({
#...doing my treatment
})

$timer.Start()

$form.showdialog()
----- cut here ------

Any ideas ?
Thanks,

Pav


My System SpecsSystem Spec
Old 05-15-2007   #2 (permalink)
Don Jones [MVP]


 
 

Re: Hide a Windows form

So, event driven programming can be tricky.

When you call ShowDialog(), you're showing the form modally, which means
PowerShell "waits" until the form closes. Once the form closes, PowerShell
can run again... but that that point, the *form* is no longer "running."
Since ShowDialog() is the last line in your script, once the form is hidden,
your script ends.

You have options. I would recommend simply setting the form's Visible
property. That'll leave your script running, but should control the visible
appearance of the form - while leaving the form "open."

You've got some other things going on. For one, you do know that your timer
isn't being added to the form, right? Not sure if that's your intent or not.

--
Don Jones
Windows PowerShell MVP
Founder: www.ScriptingAnswers.com
Co-Author: "Windows PowerShell: TFM"

"Pav" <npawlaczyk@gmail.com> wrote in message
news:1179242034.407687.323420@n59g2000hsh.googlegroups.com...
> Hello,
>
> I have a powershell app that i'd like to hide/show with a systray icon
> menu. Unfortunately, My script quits as soon as i call the Hide()
> method of the form.
> I'm not really experienced in .NET windows classes, Perhaps the
> problem doesn't involve powershell at all. If so, feel free to
> redirect me toward the good group.
>
> Here's the main structure of my script :
>
> ----- cut here ------
> $form = new-object
> System.Windows.Forms.form
>
> $mi = new-object System.Windows.Forms.MenuItem
> $mi.Index = 0
> $mi.Text = "Hide"
> $mi.add_Click({
>
> $form.hide()
> })
>
> $cm = new-object System.Windows.Forms.ContextMenu
> $form.contextMenu = $cm
> $form.contextMenu.MenuItems.AddRange($mi)
>
> $form.ShowInTaskbar = $true
> $form.set_autosize($true)
> $form.Text = "My Form"
>
> $ni = new-object System.Windows.Forms.NotifyIcon
> $ni.Icon = New-object System.Drawing.Icon("icon.ico")
> $NI.ContextMenu = $cm
> $NI.Text = "Systray Text"
> $NI.Visible =
> $True;
>
> $timer = New-Object System.Windows.Forms.Timer
> $timer.Interval = 5000
>
> $timer.add_Tick({
> #...doing my treatment
> })
>
> $timer.Start()
>
> $form.showdialog()
> ----- cut here ------
>
> Any ideas ?
> Thanks,
>
> Pav
>


My System SpecsSystem Spec
Old 05-16-2007   #3 (permalink)
Pav


 
 

Re: Hide a Windows form

Hello Don, Thanks for your reply !

Apparently, setting the Visible property to $false close the form as
well.
I know that my timer isn't added to my form (i don't know the correct
syntax to do it), but will it change anything to the global behavior
of the script ?

I'll try to find a way to have the script working. However, if someone
knows how to do it or has a script that does the same thing, I'll be
glad to hear the solution.

Thanks,

Pav


On May 15, 11:30 pm, "Don Jones [MVP]" <d...@sapien.com> wrote:
> So, event driven programming can be tricky.
>
> When you call ShowDialog(), you're showing the form modally, which means
> PowerShell "waits" until the form closes. Once the form closes, PowerShell
> can run again... but that that point, the *form* is no longer "running."
> Since ShowDialog() is the last line in your script, once the form is hidden,
> your script ends.
>
> You have options. I would recommend simply setting the form's Visible
> property. That'll leave your script running, but should control the visible
> appearance of the form - while leaving the form "open."
>
> You've got some other things going on. For one, you do know that your timer
> isn't being added to the form, right? Not sure if that's your intent or not.
>
> --
> Don Jones
> Windows PowerShell MVP
> Founder:www.ScriptingAnswers.com
> Co-Author: "Windows PowerShell: TFM"
>
> "Pav" <npawlac...@gmail.com> wrote in message
>
> news:1179242034.407687.323420@n59g2000hsh.googlegroups.com...
>
> > Hello,

>
> > I have a powershell app that i'd like to hide/show with a systray icon
> > menu. Unfortunately, My script quits as soon as i call the Hide()
> > method of the form.
> > I'm not really experienced in .NET windows classes, Perhaps the
> > problem doesn't involve powershell at all. If so, feel free to
> > redirect me toward the good group.

>
> > Here's the main structure of my script :

>
> > ----- cut here ------
> > $form = new-object
> > System.Windows.Forms.form

>
> > $mi = new-object System.Windows.Forms.MenuItem
> > $mi.Index = 0
> > $mi.Text = "Hide"
> > $mi.add_Click({

>
> > $form.hide()
> > })

>
> > $cm = new-object System.Windows.Forms.ContextMenu
> > $form.contextMenu = $cm
> > $form.contextMenu.MenuItems.AddRange($mi)

>
> > $form.ShowInTaskbar = $true
> > $form.set_autosize($true)
> > $form.Text = "My Form"

>
> > $ni = new-object System.Windows.Forms.NotifyIcon
> > $ni.Icon = New-object System.Drawing.Icon("icon.ico")
> > $NI.ContextMenu = $cm
> > $NI.Text = "Systray Text"
> > $NI.Visible =
> > $True;

>
> > $timer = New-Object System.Windows.Forms.Timer
> > $timer.Interval = 5000

>
> > $timer.add_Tick({
> > #...doing my treatment
> > })

>
> > $timer.Start()

>
> > $form.showdialog()
> > ----- cut here ------

>
> > Any ideas ?
> > Thanks,

>
> > Pav



My System SpecsSystem Spec
Old 05-17-2007   #4 (permalink)
Oisin Grehan


 
 

Re: Hide a Windows form

Don, unfortunately it ain't that easy:

a) Powershell does not support event handling natively.

Yes, it will let you assign scriptblocks as handlers for events via
the add_ and remove_ syntax, but when it comes to execute the
scriptblock, it will fail because of a lack of a context runspace.
This is something half-baked in powershell, and will probably be fixed
for vNext. In the meantime, you can bind to events and handle them in
a pseudo-realtime fashion using http://www.codeplex.com/PSEventing

b) Windows forms programming is highly unstable in powershell due to
the multithreaded nature of the runspace/pipeline architecture.

Windows Forms components demand that the same thread that created them
be the one that manipulates them. Since every pipeline is assigned to
a different worker thread under powershell, thread affinity is not
gauranteed when playing around like this. Results vary from "it works
sometimes" to "oops, powershell has locked up."

While pseventing can partially solve problem a), it can do nothing to
help with part b).

- Oisin

p.s. you're the vbscript guy, right?

On May 15, 5:30 pm, "Don Jones [MVP]" <d...@sapien.com> wrote:
> So, event driven programming can be tricky.
>
> When you call ShowDialog(), you're showing the form modally, which means
> PowerShell "waits" until the form closes. Once the form closes, PowerShell
> can run again... but that that point, the *form* is no longer "running."
> Since ShowDialog() is the last line in your script, once the form is hidden,
> your script ends.
>
> You have options. I would recommend simply setting the form's Visible
> property. That'll leave your script running, but should control the visible
> appearance of the form - while leaving the form "open."
>
> You've got some other things going on. For one, you do know that your timer
> isn't being added to the form, right? Not sure if that's your intent or not.
>
> --
> Don Jones
> Windows PowerShell MVP
> Founder:www.ScriptingAnswers.com
> Co-Author: "Windows PowerShell: TFM"
>
> "Pav" <npawlac...@gmail.com> wrote in message
>
> news:1179242034.407687.323420@n59g2000hsh.googlegroups.com...
>
>
>
> > Hello,

>
> > I have a powershell app that i'd like to hide/show with a systray icon
> > menu. Unfortunately, My script quits as soon as i call the Hide()
> > method of the form.
> > I'm not really experienced in .NET windows classes, Perhaps the
> > problem doesn't involve powershell at all. If so, feel free to
> > redirect me toward the good group.

>
> > Here's the main structure of my script :

>
> > ----- cut here ------
> > $form = new-object
> > System.Windows.Forms.form

>
> > $mi = new-object System.Windows.Forms.MenuItem
> > $mi.Index = 0
> > $mi.Text = "Hide"
> > $mi.add_Click({

>
> > $form.hide()
> > })

>
> > $cm = new-object System.Windows.Forms.ContextMenu
> > $form.contextMenu = $cm
> > $form.contextMenu.MenuItems.AddRange($mi)

>
> > $form.ShowInTaskbar = $true
> > $form.set_autosize($true)
> > $form.Text = "My Form"

>
> > $ni = new-object System.Windows.Forms.NotifyIcon
> > $ni.Icon = New-object System.Drawing.Icon("icon.ico")
> > $NI.ContextMenu = $cm
> > $NI.Text = "Systray Text"
> > $NI.Visible =
> > $True;

>
> > $timer = New-Object System.Windows.Forms.Timer
> > $timer.Interval = 5000

>
> > $timer.add_Tick({
> > #...doing my treatment
> > })

>
> > $timer.Start()

>
> > $form.showdialog()
> > ----- cut here ------

>
> > Any ideas ?
> > Thanks,

>
> > Pav- Hide quoted text -

>
> - Show quoted text -



My System SpecsSystem Spec
Old 05-23-2007   #5 (permalink)
Don Jones [MVP]


 
 

Re: Hide a Windows form

I dunno that I'm "the" VBScript "guy," but I certainly play with VBScript.
Although perhaps I play a bit more with PowerShell, these days.

I think we were coming at the problem from different directions and you were
being clearer - I was proposing setting the form's Visible property *from
within an event handler,* which I've had some success with. I understand
about Windows Forms being picky about threading, as you outlined. I also
know that timers in particular can be tricky. Generally, anytime I'm working
with Windows Forms, I'm doing so with ShowDialog so that it's blocking and
keeping the thread running.

Thanks!

--
Don Jones
Windows PowerShell MVP
Founder: www.ScriptingAnswers.com
Co-Author: "Windows PowerShell: TFM"

"Oisin Grehan" <oising@gmail.com> wrote in message
news:1179438630.283274.174620@q75g2000hsh.googlegroups.com...
> Don, unfortunately it ain't that easy:
>
> a) Powershell does not support event handling natively.
>
> Yes, it will let you assign scriptblocks as handlers for events via
> the add_ and remove_ syntax, but when it comes to execute the
> scriptblock, it will fail because of a lack of a context runspace.
> This is something half-baked in powershell, and will probably be fixed
> for vNext. In the meantime, you can bind to events and handle them in
> a pseudo-realtime fashion using http://www.codeplex.com/PSEventing
>
> b) Windows forms programming is highly unstable in powershell due to
> the multithreaded nature of the runspace/pipeline architecture.
>
> Windows Forms components demand that the same thread that created them
> be the one that manipulates them. Since every pipeline is assigned to
> a different worker thread under powershell, thread affinity is not
> gauranteed when playing around like this. Results vary from "it works
> sometimes" to "oops, powershell has locked up."
>
> While pseventing can partially solve problem a), it can do nothing to
> help with part b).
>
> - Oisin
>
> p.s. you're the vbscript guy, right?
>
> On May 15, 5:30 pm, "Don Jones [MVP]" <d...@sapien.com> wrote:
>> So, event driven programming can be tricky.
>>
>> When you call ShowDialog(), you're showing the form modally, which means
>> PowerShell "waits" until the form closes. Once the form closes,
>> PowerShell
>> can run again... but that that point, the *form* is no longer "running."
>> Since ShowDialog() is the last line in your script, once the form is
>> hidden,
>> your script ends.
>>
>> You have options. I would recommend simply setting the form's Visible
>> property. That'll leave your script running, but should control the
>> visible
>> appearance of the form - while leaving the form "open."
>>
>> You've got some other things going on. For one, you do know that your
>> timer
>> isn't being added to the form, right? Not sure if that's your intent or
>> not.
>>
>> --
>> Don Jones
>> Windows PowerShell MVP
>> Founder:www.ScriptingAnswers.com
>> Co-Author: "Windows PowerShell: TFM"
>>
>> "Pav" <npawlac...@gmail.com> wrote in message
>>
>> news:1179242034.407687.323420@n59g2000hsh.googlegroups.com...
>>
>>
>>
>> > Hello,

>>
>> > I have a powershell app that i'd like to hide/show with a systray icon
>> > menu. Unfortunately, My script quits as soon as i call the Hide()
>> > method of the form.
>> > I'm not really experienced in .NET windows classes, Perhaps the
>> > problem doesn't involve powershell at all. If so, feel free to
>> > redirect me toward the good group.

>>
>> > Here's the main structure of my script :

>>
>> > ----- cut here ------
>> > $form = new-object
>> > System.Windows.Forms.form

>>
>> > $mi = new-object System.Windows.Forms.MenuItem
>> > $mi.Index = 0
>> > $mi.Text = "Hide"
>> > $mi.add_Click({

>>
>> > $form.hide()
>> > })

>>
>> > $cm = new-object System.Windows.Forms.ContextMenu
>> > $form.contextMenu = $cm
>> > $form.contextMenu.MenuItems.AddRange($mi)

>>
>> > $form.ShowInTaskbar = $true
>> > $form.set_autosize($true)
>> > $form.Text = "My Form"

>>
>> > $ni = new-object System.Windows.Forms.NotifyIcon
>> > $ni.Icon = New-object System.Drawing.Icon("icon.ico")
>> > $NI.ContextMenu = $cm
>> > $NI.Text = "Systray Text"
>> > $NI.Visible =
>> > $True;

>>
>> > $timer = New-Object System.Windows.Forms.Timer
>> > $timer.Interval = 5000

>>
>> > $timer.add_Tick({
>> > #...doing my treatment
>> > })

>>
>> > $timer.Start()

>>
>> > $form.showdialog()
>> > ----- cut here ------

>>
>> > Any ideas ?
>> > Thanks,

>>
>> > Pav- Hide quoted text -

>>
>> - Show quoted text -

>
>


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Auto-Hide toolbar won't hide? Vista General
How can I get a form to gradually fade rather than just hide .NET General
add browse button to windows form PowerShell
Out alignment on UI windows form using .net 1.1 .NET General
Hide Vista Taskbar (without using auto-hide)? Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46