Page tree
Skip to end of metadata
Go to start of metadata

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 Aug 10, 2019 by Mark.Juras

I have this VB6:
    With frmMainForm.tbrMain.Buttons
      .Add , , , tbrSeparator
      The calls are passing the Key, Style, and ImageKey
And I get this C#

   withTemp1 = frmMainForm.DefInstance.tbrMain.Items;

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"/>

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)
   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 , , , tbrSeparator


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"/>

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
               return itm;
           public static ToolStripItem ToolbarButtonsAdd(this ToolStripItemCollection tbr)
               ToolStripItem itm = new ToolStripSeparator();
               return itm;
  • No labels