Skip to content

July 28, 2015

PowerShell WinForm ToolStrip Bug Workaround

There is a known bug with the Tool Strips in Windows Forms. The issue is that when using the renderer, you will see a white border around your ToolStrip. Microsoft has stated that they will not be fixing the bug, however there is a C# workaround by overloading the method.

If you are using PowerShell to generate your Winforms, the fix is slightly different. In order to override this method for the fix, you must add the C# code to your PowerShell script by using the Add-Type cmdlet.  Once the Type is added you can assign the object to $ToolStrip.Renderer.

Example:

# C# Code
$TypeCustomSystemRender
= @’
using System.Windows.Forms;
public class CustomSystemRender : ToolStripSystemRenderer
{
    public CustomSystemRender() { }
    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        //base.OnRenderToolStripBorder(e);
    }
}
‘@

# Compile and add the code into script
Add-Type
-ReferencedAssemblies System.Windows.Forms -TypeDefinition $TypeCustomSystemRender -WarningAction SilentlyContinue

# Apply the Override to a ToolStrip
$ToolStrip
= New-Object System.Windows.Forms.ToolStrip
$ToolStrip.Location = New-Object System.Drawing.Point 10, 40
$ToolStrip.RenderMode = “System”
$ToolStrip.Renderer = New-Object CustomSystemRender
$Form.Controls.Add($ToolStrip)

 

Read more from PowerShell

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments