Skip to content

Form Designer Redundant Typing #117

Description

@TMA-2

Description

I've noticed that across PSScriptPad, the Visual Studio extension, and the barebones WinForms designer, they explicitly type certain objects redundantly, sometimes three times over. For instance, in the first code snippet below, the first [AnchorStyles] enum is double-cast, and the values passed to the second snippet [System.Drawing.Color] for the button object are cast to [System.Byte] twice before [System.Int32], despite FromArgb() taking Int32 in its overloads.

On top of every type explicitly having the System namespace explicitly defined (instead of perhaps having using namespace System inserted at the top of the designer file, or System, System.Windows.Forms, System.Drawing, etc.) which increases line width and file size, this makes it very irritating to manually edit when needed, as any changes made will be overwritten if it needs to be modified again within a designer.
As the generation code is apparently shared to some extent between all three apps, I assume fixing one would fix all three (four?).

These instances could be found with the regex (\[System\.[\w.]+\])\1 and replaced with $1. The twice-redundant casting can be found with (\[System\.[\w.]+\])\(((\[System\.[\w.]+\])\3(\d+))\) and replaced with $1$4.

Expected Behavior

Each type cast is done once, optionally with the shortened form sans System (perhaps with Using namespace System inserted at top if needed), or even without the cast. There's probably a good reason for why this is done, but at the very least, a single cast for each value would be the expected behavior.

Actual Behavior

Certain properties and methods get double or even triple cast. Some of the ones I've found are:

  • [AnchorStyles] - Just the first enum value
  • [System.Drawing.Color]::FromArgb() - Any instance of this method has Byte to Byte to Int32
  • [System.Byte] - It seems like every byte is double-cast.

Redundant Types

$GroupBoxType.Anchor = `
	([System.Windows.Forms.AnchorStyles][System.Windows.Forms.AnchorStyles]::Top -bor `
	[System.Windows.Forms.AnchorStyles]::Left -bor `
	[System.Windows.Forms.AnchorStyles]::Right
)

Double-redundant Casting

This is very odd, as FromArgb() doesn't call for a Byte in any overload.

$btnOK.FlatAppearance.MouseDownBackColor = [System.Drawing.Color]::FromArgb(`
	([System.Int32]([System.Byte][System.Byte]192)),`
	([System.Int32]([System.Byte][System.Byte]255)),`
	([System.Int32]([System.Byte][System.Byte]192))
)
$cbAged.ForeColor  = [System.Drawing.Color]::FromArgb(([System.Int32]([System.Byte][System.Byte]184)),([System.Int32]([System.Byte][System.Byte]81)),([System.Int32]([System.Byte][System.Byte]165)))

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions