Minimise a form to the system tray

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
This topic is 12 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
gaiid_it
Posts: 7
Last visit: Thu Mar 13, 2014 1:23 am

Minimise a form to the system tray

Post by gaiid_it »

Hi,Could someone show me how to minimise a form to the system tray and not show it in the tasbar until the systry icon is double clicked? When I minimise the form with, $NotifyIcon = New-Object System.Windows.Forms.NotifyIcon $BalloonTipText = "text" $BalloonTipTitle = "title" Show-NotifyIcon $NotifyIcon $BalloonTipText $BalloonTipTitle $notifyicon1.Visible = $true $form1.ShowInTaskbar = $false $form1.WindowState = "minimized"it minimises but then just closes the form and exits the script. how do I keep the form minimised but open?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Minimise a form to the system tray

Post by davidc »

Using the designer you can tell the Form not to show in the TaskBar as you show in your script. You can add a Double Click event to the NotifyIcon control and toggle the Form's WindowState. Alternatively you can set the forms Visible property to false and set it to true when the user double clicks on the notifyicon.I recommend reading the Spotlight on Control blog articles for help with individual controls. David
David
SAPIEN Technologies, Inc.
User avatar
gaiid_it
Posts: 7
Last visit: Thu Mar 13, 2014 1:23 am

Minimise a form to the system tray

Post by gaiid_it »

Yeah, that is what I am trying to do but when I set the form ShowInTaskBar to $false it just closes the script and doesn't hide it. Is there an extra line I am missing to keep the script running?I had a look through the spotlight articles and they have helped me with many things, but unfortunatley not this.Thanksgaiid_it2012-02-09 09:24:27
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Minimise a form to the system tray

Post by davidc »

Are you sure it actually closes the script? I created a sample and the form continues to run after minimizing it. Did you create a button to minimize the form or are you using Minimize button in the Window bar?David
David
SAPIEN Technologies, Inc.
User avatar
gaiid_it
Posts: 7
Last visit: Thu Mar 13, 2014 1:23 am

Minimise a form to the system tray

Post by gaiid_it »

I created a check box to minimise it. The check sets a timer going and the intention was to minimise the form to the systray while the timer was going.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Minimise a form to the system tray

Post by davidc »

Hard to tell what is happening without the script. There must be something causing the closure. I recommend setting a few break points and debugging the script. David
David
SAPIEN Technologies, Inc.
User avatar
gaiid_it
Posts: 7
Last visit: Thu Mar 13, 2014 1:23 am

Minimise a form to the system tray

Post by gaiid_it »

This is my code. I have tried debugging and, in my very limited, experience didn't show anything obvious. function OnApplicationLoad { #Note: This function is not called in Projects #Note: This function runs before the form is created #Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path #Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList) #Important: Form controls cannot be accessed in this function #TODO: Add snapins and custom code to validate the application load return $true #return true for success or false for failure}function OnApplicationExit { #Note: This function is not called in Projects #Note: This function runs after the form is closed #TODO: Add custom code to clean up and unload snapins when the application exits $script:ExitCode = 0 #Set the exit code for the Packager}$form1_Load={ #TODO: Initialize Form Controls here Build-Table}#region Control Helper Functionsfunction Show-NotifyIcon{<# .SYNOPSIS Displays a NotifyIcon's balloon tip message in the taskbar's notification area. .DESCRIPTION Displays a NotifyIcon's a balloon tip message in the taskbar's notification area. .PARAMETER BalloonTipText Sets the text to display in the balloon tip. .PARAMETER BalloonTipTitle Sets the Title to display in the balloon tip. .PARAMETER BalloonTipIcon The icon to display in the ballon tip.#> param( [Parameter(Mandatory = $true, Position = 0)] [ValidateNotNull()] [System.Windows.Forms.NotifyIcon]$NotifyIcon, [Parameter(Position = 1)] [String]$BalloonTipText, [Parameter(Position = 2)] [String]$BalloonTipTitle, [Parameter(Position = 3)] [System.Windows.Forms.ToolTipIcon]$BalloonTipIcon ) if($NotifyIcon.Icon -eq $null) { #Set a Default Icon otherwise the ballon will not show $NotifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon([System.Windows.Forms.Application]::ExecutablePath) } if($BalloonTipText -ne $null -and $BalloonTipText.Length -ne 0) { $notifyicon1.BalloonTipText = $BalloonTipText } if($BalloonTipTitle -ne $null -and $BalloonTipTitle.Length -ne 0) { $notifyicon1.BalloonTipTitle = $BalloonTipTitle } if($BalloonTipIcon -ne $null -and $BalloonTipIcon.Length -ne 0) { $notifyicon1.BalloonTipIcon = $BalloonTipIcon } $notifyicon1.ShowBalloonTip(0)}function Load-DataGridView{ <# .SYNOPSIS This functions helps you load items into a DataGridView. .DESCRIPTION Use this function to dynamically load items into the DataGridView control. .PARAMETER DataGridView The ComboBox control you want to add items to. .PARAMETER Item The object or objects you wish to load into the ComboBox's items collection. .PARAMETER DataMember Sets the name of the list or table in the data source for which the DataGridView is displaying data. #> Param ( [Parameter(Mandatory=$true)] [System.Windows.Forms.DataGridView]$DataGridView, [Parameter(Mandatory=$true)] $Item, [Parameter(Mandatory=$false)] [string]$DataMember ) $DataGridView.SuspendLayout() $DataGridView.DataMember = $DataMember if ($Item -is [System.ComponentModel.IListSource]` -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] ) { $DataGridView.DataSource = $Item } else { $array = New-Object System.Collections.ArrayList if ($Item -is [System.Collections.IList]) { $array.AddRange($Item) } else { $array.Add($Item) } $DataGridView.DataSource = $array } $DataGridView.ResumeLayout()}function Build-Table{ $datagridview1.Rows.Clear() 1..8 | %{$datagridview1.Rows.Add($_,$_+1,$_+2,$_+3,$_+4)}}#endregion$TotalTime = 10 #in seconds$x = 1$button1_Click={ Build-Table}$timer1_Tick={ #Use Get-Date for Time Accuracy [TimeSpan]$span = $script:StartTime - (Get-Date) #Update the display $textbox1.Text = "{0:N0}" -f $span.TotalSeconds if($span.TotalSeconds -le 0) { $timer1.Stop() Build-Table $textbox2.Text = "Count:$x";$x++ $TotalTime = 10 $script:StartTime = (Get-Date).AddSeconds($TotalTime) $timer1.Start() }}$checkbox1_CheckedChanged={ $NotifyIcon = New-Object System.Windows.Forms.NotifyIcon $notifyicon1.Icon = New-object System.Drawing.Icon("icon.ico") $BalloonTipText = "TipText" $BalloonTipTitle = "TipTitle" $BalloonTipIcon = "Info" If($checkbox1.Checked){ $script:StartTime = (Get-Date).AddSeconds($TotalTime) $button1.Enabled = $false $button1.Text = "Auto" $timer1.Start() Start-Sleep 5 Show-NotifyIcon $NotifyIcon $BalloonTipText $BalloonTipTitle $BalloonTipIcon $form1.WindowState = "Minimized" $form1.ShowInTaskbar = $false } If(!$checkbox1.Checked){ $timer1.Stop() $button1.Enabled = $true $button1.Text = "Refresh" $checkbox1.Text = "Auto Refresh: " }}$notifyicon1_MouseDoubleClick=[System.Windows.Forms.MouseEventHandler]{#Event Argument: $_ = [System.Windows.Forms.MouseEventArgs] #TODO: Place custom script here $form1.ShowInTaskbar = $true $form1.WindowState = "Normal"}
User avatar
gaiid_it
Posts: 7
Last visit: Thu Mar 13, 2014 1:23 am

Minimise a form to the system tray

Post by gaiid_it »

BTW making the form visible = $false, instead of showintaskbar = $false also closes the form.Thanks
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Minimise a form to the system tray

Post by davidc »

Ok it seems changing the ShowInTaskBar while the form displays makes it disappear. Unfortunately this is one of those .NET quarks that we can't do anything about. I recommend setting the ShowInTaskBar in the designer and only minimize and maximize the app. In addition, I recommend adding the NotifyIcon in the designer so you don't have to recreate the object. David
David
SAPIEN Technologies, Inc.
User avatar
gaiid_it
Posts: 7
Last visit: Thu Mar 13, 2014 1:23 am

Minimise a form to the system tray

Post by gaiid_it »

At least it is not just me then! Thanks for your time.
This topic is 12 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.