Support Statement: hybrid upgrade of Toolbar.Buttons.Add to WinForms extension

Support Statement: hybrid upgrade of Toolbar.Buttons.Add to WinForms extension

This is a workaroung to deal with the migration of ToolBarButton.DisplayStyle to two different ToolStripItem classes: Button and Separtor

See sample 

  File Modified

ZIP Archive AddToolbarButton_01.zip

Aug 10, 2019 by Mark Juras

I have this VB6:

With frmMainForm.tbrMain.Buttons .Add , gstrTOOLBAR_BUTTON_KEY_COPY, , tbrDefault, gstrTOOLBAR_BUTTON_KEY_COPY ... .Add , , , tbrSeparator The calls are passing the Key, Style, and ImageKey

And I get this C#

withTemp1 = frmMainForm.DefInstance.tbrMain.Items; withTemp1.Add(null,modAddToolbarButton.gstrTOOLBAR_BUTTON_KEY_COPY,null,System.Windows.Forms.ButtonStyleConstants.tbrDefault,modAddToolbarButton.gstrTOOLBAR_BUTTON_KEY_COPY); ... withTemp1.Add(null,null,null,System.Windows.Forms.ButtonStyleConstants.tbrSeparator,null);

and this error:

The type or namespace name 'ButtonStyleConstants' does not exist in the namespace 'System.Windows.Forms'

The IDF has this default definition for The Toolbar.Buttons.Add

Source Name = GM.MSCOMCTL.OCX Run Date = 7/24/2019 9:29:03 AM Pattern = "Add" Location Name = DescriptionFile/library:GM.MSCOMCTL.OCX/class:IButtons/method:Add <method id="Add" type="Button"> <argument id="Index" type="Variant" status="ByVal" optional="Default"/> <argument id="Key" type="Variant" status="ByVal" optional="Default"/> <argument id="Caption" type="Variant" status="ByVal" optional="Default"/> <argument id="Style" type="Variant" status="ByVal" optional="Default"/> <argument id="Image" type="Variant" status="ByVal" optional="Default"/> </method>

This is occurs in a custom IDF that is migrating MSCOMCTL to Winforms. WinForms.Toolbar.Buttons.Add has this:

public ToolStripItem Add(string text, Image image, EventHandler onClick); public int Add(ToolStripItem value); public ToolStripItem Add(Image image); public ToolStripItem Add(string text, Image image);

The most useful form is the one that can take a ToolStripItem: either a ToolStripButton or a ToolStripSeparator

ctor: public ToolStripButton(string text, Image image) or ctor: public ToolStripSeparator()

I ended up fixing this with some rules and some hand coding. It is not general, but for this situation it is clean.

The rules go for this type of translation:

With frmMainForm.tbrMain.Buttons .Add , gstrTOOLBAR_BUTTON_KEY_COPY, , tbrDefault, gstrTOOLBAR_BUTTON_KEY_COPY .Add , , , tbrSeparator

becomes:

withTemp1.ToolbarButtonsAdd(modAddToolbarButton.gstrTOOLBAR_BUTTON_KEY_COPY,modAddToolbarButton.gstrTOOLBAR_BUTTON_KEY_COPY); withTemp1.ToolbarButtonsAdd();

Here are the rules done as changes to the IDF GM.MSCOMCTL.OCX/IButtons.Add:

<method id="Add" type="Button" nPram="6" migPattern="%1d.ToolbarButtonsAdd(%3o,%6o)"> <argument id="Index" type="Variant" status="ByVal" optional="Def.Overload"/> <argument id="Key" type="Variant" status="ByVal" optional="Def.Overload"/> <argument id="Caption" type="String" status="ByVal" optional="Def.Overload"/> <argument id="Style" type="Variant" status="ByVal" optional="Def.Overload"/> <argument id="Image" type="String" status="ByVal" optional="Def.Overload"/> </method>

Here is the support code:

namespace MigrationSupport { public static class MSComctlLibHelper { public static ToolStripItem ToolbarButtonsAdd(this ToolStripItemCollection tbr, string Caption, string ImageKey) { ToolStripItem itm = new ToolStripButton(Caption) { ImageKey = ImageKey, DisplayStyle = ToolStripItemDisplayStyle.Image }; tbr.Add(itm); return itm; } public static ToolStripItem ToolbarButtonsAdd(this ToolStripItemCollection tbr) { ToolStripItem itm = new ToolStripSeparator(); tbr.Add(itm); return itm; } } }