Archive for the ‘addMenuItem function’ Category
Adding a MenuStrip to a Form. Hands Only
As can be clearly seen from the survey Ravikanth conducted here http://www.ravichaganti.com/blog/?p=2111 , the great majority of powershellers do write GUI. Moreover, there is no common universal tool. Every powersheller chooses the way one creates user interface.
Well, why don’t we create a small example by using just bare hands. Let’s do our preparation. A recipe is simple: a bit of text editor (Intellisense-enabled is preferable), a piece of rich text or even a text file, and a keyboard.
Supposedly, we need a form with a menu (two-level one, not to be considered as a simpleton), a label and a textbox. The user should be able to manage content of textbox using menu.
There is two ways to create a form:
1.1 by using PrimalForms:
#Generated Form Function function GenerateForm { ######################################################################## # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0 # Generated On: 26/04/2011 09:43 p.m. # Generated By: apetrovskiy ######################################################################## #region Import the Assemblies [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null #endregion #region Generated Form Objects $form1=New-Object System.Windows.Forms.Form $textBox1=New-Object System.Windows.Forms.TextBox $label1=New-Object System.Windows.Forms.Label $InitialFormWindowState=New-Object System.Windows.Forms.FormWindowState #endregion Generated Form Objects #region Generated Form Code $System_Drawing_Size=New-Object System.Drawing.Size $System_Drawing_Size.Height =262 $System_Drawing_Size.Width =284 $form1.ClientSize=$System_Drawing_Size $form1.DataBindings.DefaultDataSourceUpdateMode=0 $form1.Name="form1" $form1.Text="Primal Form" $textBox1.DataBindings.DefaultDataSourceUpdateMode=0 $System_Drawing_Point=New-Object System.Drawing.Point $System_Drawing_Point.X =34 $System_Drawing_Point.Y =82 $textBox1.Location=$System_Drawing_Point $textBox1.Name="textBox1" $System_Drawing_Size=New-Object System.Drawing.Size $System_Drawing_Size.Height =20 $System_Drawing_Size.Width =195 $textBox1.Size=$System_Drawing_Size $textBox1.TabIndex=1 $form1.Controls.Add($textBox1) $label1.DataBindings.DefaultDataSourceUpdateMode=0 $System_Drawing_Point=New-Object System.Drawing.Point $System_Drawing_Point.X =36 $System_Drawing_Point.Y =45 $label1.Location=$System_Drawing_Point $label1.Name="label1" $System_Drawing_Size=New-Object System.Drawing.Size $System_Drawing_Size.Height =22 $System_Drawing_Size.Width =177 $label1.Size=$System_Drawing_Size $label1.TabIndex=0 $label1.Text="label1" $form1.Controls.Add($label1) #endregion Generated Form Code #Save the initial state of the form $InitialFormWindowState=$form1.WindowState #Init the OnLoad event to correct the initial state of the form $form1.add_Load($OnLoadForm_StateCorrection) #Show the Form $form1.ShowDialog() | Out-Null } #End Function #Call the Function GenerateForm
1.2 by typing the following code:
[System.Windows.Forms.Form]$form1=` New-Object System.Windows.Forms.Form; [System.Windows.Forms.Label]$label1=` New-Object System.Windows.Forms.Label; $label1.Name="label1"; $label1.Text="label1"; $label1.Left=20; $label1.Top=50; [System.Windows.Forms.TextBox]$textbox1=` New-Object System.Windows.Forms.TextBox; $textbox1.Name="textbox1"; $textbox1.Left=20; $textbox1.Top=100; $form1.Name="form1"; $form1.Controls.Add($label1); $form1.Controls.Add($textbox1); $form1.ShowDialog() | Out-Null;
All our further actions are insertions to be written above the string $form1.ShowDialog()
2. Now we add the following simple function:
function addMenuItem { param([ref]$ParentItem, [string]$ItemName='', [string]$ItemText='', [scriptblock]$ScriptBlock=$null ) [System.Windows.Forms.ToolStripMenuItem]$private:menuItem=` New-Object System.Windows.Forms.ToolStripMenuItem; $private:menuItem.Name =$ItemName; $private:menuItem.Text =$ItemText; if ($ScriptBlock -ne $null) { $private:menuItem.add_Click(([System.EventHandler]$handler=` $ScriptBlock)); } if (($ParentItem.Value) -is [System.Windows.Forms.MenuStrip]) { ($ParentItem.Value).Items.Add($private:menuItem); } if (($ParentItem.Value) -is [System.Windows.Forms.ToolStripItem]) { ($ParentItem.Value).DropDownItems.Add($private:menuItem); } return $private:menuItem; }
The function receives a reference to the parent menu item, the name and the label chosen for a new menu element, and, if nesessary, scriptblock. Don’t pass a scriptblock to menuitems that are not at the lowest leel of your menu hierarhy.
3. After that it’s time to add a MenuStrip:
[System.Windows.Forms.MenuStrip]$mainMenu=New-Object System.Windows.Forms.MenuStrip; $form1.Controls.Add($mainMenu);
If we run our form now, we can see a hardly visible (depending on your color schema) strip at the top of the $form1.
4. Now we can add handlers form menu items. To simplify the example, our handlers are scriptblocks. I wrote one, crischening it as $sb1. Undoubtedly, by searching for $mainMenu in ObjectBrowser untidy code you may find more examples.
[scriptblock]$sb1= { #your code }
5. Finally, we add menu items (see sample code for more details):
#region File (addMenuItem -ParentItem ([ref]$mainMenu) -ItemName 'mnuFile' -ItemText 'File' -ScriptBlock $null) | %{ $null=addMenuItem -ParentItem ([ref]$_) -ItemName 'mnuFileOpen' -ItemText 'Open' -ScriptBlock $sb1; $null=addMenuItem -ParentItem ([ref]$_) -ItemName 'mnuFileSave' -ItemText 'Save' -ScriptBlock $null; $null=addMenuItem -ParentItem ([ref]$_) -ItemName 'mnuFileExit' -ItemText 'Exit' -ScriptBlock $null;} | Out-Null; #endregion File
Today’s code attached here.
Rate this:
Written by Alexander Petrovskiy
April 26, 2011 at 6:47 pm
Posted in addMenuItem function, DropDownItems.Add, Items.Add, Powershell, System.EventHandler, System.Windows.Forms.Form, System.Windows.Forms.MenuStrip, System.Windows.Forms.OpenFileDialog, System.Windows.Forms.ToolStripItem, WinForms
Tagged with How to deal with a [WinForms] control sample, WinForms sample

