SAPIEN Powershell Studio – Scaling form contents when resizing


Update: June Blender  kindly reached out to the experts on this and provided the following method.


The anchor property defines that that the specified edge of the object should maintain its position in relation to its parent object on that edge.

This means that an anchor set to

  • None will float proportionally to the parent object
  • Left will maintain the number of pixels between the left side of the object and the left side of the parent object
  • Right will maintain the number of pixels between the right side of the object and the right side of the parent object
  • Top  will maintain the number of pixels between the upper edge of the object and the upper edge of the parent object
  • Bottom  will maintain the number of pixels between the lower edge of the object and the lower edge of the parent object

From a scaling point of view

  • For any objects that you want to scale both vertically, set the anchor to ‘Top,Bottom’
  • For any objects that you want to scale only horizontally, set the anchor to ‘Left,Right’.

You can use any combination of these to float an object below other objects or to dynamically expand any objects as the form grows/shrinks.

For my dgMonitorInfo below, setting $dgMonitorInfo.Anchor = ‘Top,Left,Right’ means  that it will maintain its position in relation to the top, left and right sides of the tab group.  The result is that it scales horizontally

For my computer status grid view below, setting $dgClientStatus.Anchor=’Top,Bottom,Left,Right’ means that it will maintain its position in relation to the top, bottom, left and right sides of the tab group.  The result is that it scales horizontally and vertically.

After the above is completed for any objects you want to scale, simply anchor any non-scaling items to an edge to prevent the scaling objects from scaling over top.


Below you will find the original post, which I am leaving up since the function Get-RecursiveControls shows how to iterate through all child objects of a form.   


First off, ensure that your form and all child elements you want to scale have a minimum size property configured and set it to the same as the starting size.

For Example (or configure it with the PowerShell Studio “Designer” pane)

$Form.MinimumSize = New-Object Drawing.Size(706, 531)
$Form.Size = New-Object System.Drawing.Size(706, 531)

$Datagridview.MinimumSize = New-Object Drawing.Size(622, 309)
$Datagridview.Size = New-Object Drawing.Size(622, 309)

Next, ensure that any child objects that you don’t want to scale are configured as follows:
1. Any objects that are above or to the left of an item that will be scaled should have an Anchor of Top or Top, Left
2. Any objects that are below or to the right of an item that will be scaled should have an Anchor of Bottom or Bottom, Right

Next, add an “SizeChanged” event.

In this event, a little bit of code to calculate the size difference does the trick nicely:

function Get-RecursiveControls
{
	Param([System.Windows.Forms.Control]$control)
	$hasChildren = $control.hasChildren
	$arrTotalControls = @()
	if ($hasChildren)
	{
		$allControls = $control.Controls
		ForEach ($childcontrol in $allcontrols)
		{
			$arrTotalControls += $childControl
			$subchildControls = Get-RecursiveControls -control $childControl
			$arrTotalControls += $subChildControls
		}
	}
	Return $arrTotalControls
}

        $heightDiff = ($formclient.Height - $formclient.MinimumSize.Height)
	$widthDiff = ($formclient.width - $formclient.MinimumSize.width)

	#Change tabcontrol size
	$height = $tabControl1.MinimumSize.Height + $heightDiff
	$width = $tabControl1.MinimumSize.Width + $widthDiff
	$newsize = New-Object System.Drawing.Size($Width,$Height)
	$tabControl1.Size = $newsize

	#Resize all child datagrid views
	$allControls = Get-RecursiveControls -control $formClient
	ForEach ($gridview in ($allControls | Where{ $_.GetType() -match 'DataGridView'}))
	{
		if ($gridview.Name -notin @('dgMonitorInfo'))
		{
			$height = $gridview.MinimumSize.Height + $heightDiff
		}
		else
		{
			$height = $gridview.size.height
		}
		$width = $gridview.MinimumSize.Width + $widthDiff
		$newsize = New-Object System.Drawing.Size($Width, $Height)
		$gridview.Size = $newsize
	}

In this example, $formclient is the variable holding my form.

First I calculate the height and width changes of the form.

        $heightDiff = ($formclient.Height - $formclient.MinimumSize.Height)
	$widthDiff = ($formclient.width - $formclient.MinimumSize.width)

Then since all of the items in my form are contained inside my tab control, I resize this to match:

#Change tabcontrol size
	$height = $tabControl1.MinimumSize.Height + $heightDiff
	$width = $tabControl1.MinimumSize.Width + $widthDiff
	$newsize = New-Object System.Drawing.Size($Width,$Height)
	$tabControl1.Size = $newsize

Then I get all the child controls of the form. Since I only want to resize my DataGridViews, I filter all these.

        $allControls = Get-RecursiveControls -control $formClient
	ForEach ($gridview in ($allControls | Where{ $_.GetType() -match 'DataGridView'}))
	{

I have one Datagridview I only want to grow width-wise, but not height-wise. For all other Datagridview controls, I add the detected growth height ($heightDiff) to the minimum height of the control. For all of the DataGridView controls I add the detected growth in width ($widthDiff) to the mimumum width of the control.

        	if ($gridview.Name -notin @('dgMonitorInfo'))
		{
			$height = $gridview.MinimumSize.Height + $heightDiff
		}
		else
		{
			$height = $gridview.size.height
		}
		$width = $gridview.MinimumSize.Width + $widthDiff

Then save the new size:

		$newsize = New-Object System.Drawing.Size($Width, $Height)
		$gridview.Size = $newsize

Here is the form before scaling:
FormSmall

Here is the form after scaling (not that the DataGridView ‘dgMonitorInfo’ scaled only width wise while the other DataGridView scaled both height and width:
FormScaled

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s