Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Hide a Windows form

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 05-15-2007   #1 (permalink)
Pav
Guest


 

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]
Guest


 

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
Guest


 

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
Guest


 

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]
Guest


 

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
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-Hide toolbar won't hide? Geoff from LA Vista General 3 03-10-2008 05:41 PM
Out alignment on UI windows form using .net 1.1 MAN .NET General 0 03-07-2008 03:55 AM
Error with Windows.forms.form Kuma PowerShell 6 09-30-2007 07:38 AM
Hide Vista Taskbar (without using auto-hide)? Johnny G Vista General 1 04-28-2007 03:06 AM
Timer not updating windows form ? Andy Webster PowerShell 1 02-20-2007 05:10 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008