[CONNECT .NET] MstnPlatformNet Forms Classes

I want to find how to make my C# AddIn project's WinForm a child of MicroStation's main window.  Is that possible?

While browsing MstnPlatformNet help I came across the Bentley.MstnPlatformNET.WinForms Namespace.  It contains a number of useful-looking classes with little indication of their purpose.  For example, are the following relevant to my first question?

  • MicroStationWin32
  • Adapter

There are some classes that look like they might be interesting, but I don't see how to use them.  For example, the ItemTypeSelectionForm Class, which is documented with an unpublished source file...

[DialogInformationAttribute(EnglishTitle = "Item Type Selection", Status = DialogInformationStatus.SystemDialog, 
	SourceFile = "$Source: mstn/mscore/clr/MicroStation/WinForms/Controls/ItemTypeSelectionForm.cs $")]
public class ItemTypeSelectionForm : Adapter
  • Hi Jon,

    Unknown said:
    I want to find how to make my C# AddIn project's WinForm a child of MicroStation's main window.

    The question is why you need to do it:

    • If you only need to display your dialog without any specific behaviour, you usually don't have to do anything.
    • If you want to implement your WinForms dialog as MicroStation dialog, you have to inherit not Form, but Adapter and to AttachAsTopLevelForm method. See the attached simple example.
    • Using Adapter and WindowManager you can implement also docking inside MicroStation application window. It's demonstrated in WPFDemo3 SDK example.

    An overall concept is not different from V8i API, so you can also check older examples.

    The question is why to don't use WPF instead of WinForms, that are technically obsolete and maintained only by Microsoft (but widely used everywhere ;-).

    With regards,

      Jan

    WinFormsTest.zip

  • Unknown said:
    If you only need to display your dialog without any specific behaviour, you usually don't have to do anything

    The WinForms dialog works but is masked by MicroStation's main window if user clicks in MicroStation.  I want my WinForm app. to remain a visible child window.

    Unknown said:
    See the attached simple example

    Thanks!

    Unknown said:
    The question is why to don't use WPF instead of WinForms

    A third-party plug-in requires WinForms.

     
    Regards, Jon Summers
    LA Solutions

  • Hi Jon,

    I'm not sure if this is what your looking for, but the Winforms created in the Visual Studio Tools for MicroStation CONNECT Edition is inherit from the Bentley.MstnPlatformNET.WinForms.Adapter. In this case, the default is single instance of a winform that is a child of MicroStation that stays with MicroStation that can be docked. Also just as an FYI, the WPF counter part uses Bentley.MstnPlatformNET.WPF.DockableWindow to take control of the inherited usercontrol. The default is a single instance of a WPF that is a child of MicroStation that also can be docked.

    In the form1.designer for WinForm you would see something similar to...

     #region Intialize MicroStation Form
            private static Form1 currentForm;
            private Bentley.Windowing.WindowContent m_windowContent;
            private Bentley.MstnPlatformNET.AddIn m_addIn;
            /// <summary>
            /// Show the form if it is not already displayed
            /// </summary>
            internal static void ShowForm(Bentley.MstnPlatformNET.AddIn addIn)
            {
                if (null != currentForm)
                {
                    currentForm.Focus();
                    return;
                }
    
                currentForm = new Form1(addIn);
                currentForm.AttachAsTopLevelForm(addIn, true);
    
                currentForm.AutoOpen = true;           
    
                currentForm.NETDockable = true;
                Bentley.Windowing.WindowManager windowManager =
                            Bentley.Windowing.WindowManager.GetForMicroStation();
                currentForm.m_windowContent =
                    windowManager.DockPanel(currentForm, currentForm.Name, currentForm.Name,
                    Bentley.Windowing.DockLocation.Floating);
    
                currentForm.m_windowContent.CanDockHorizontally = false; // limit to left and right docking
            }
    
            /// <summary>
            /// Adjust to controls when the form changes size
            /// </summary>
            private void Form1_SizeChanged(object sender, System.EventArgs e)
            {
                if (this.DesignMode)
                {
                    System.Diagnostics.Debug.Assert(!this.DesignMode, "Do not use SetFormSizes in design mode.");
                    return;
                }
            }
    
            /// <summary>Handle the standard Closed event
            /// </summary>
            private void Form1_Closed(object sender, System.EventArgs e)
            {
                if (currentForm != null)
                {
                    currentForm.DetachFromMicroStation();
                    currentForm.Dispose(true);
                    currentForm = null;
                }
            }
        }
        #endregion

    The Winform is implemented using

    Form1.ShowForm(Program.Addin);

    The Form.cs inherits from Bentley.MstnPlatformNET.WinForms.Adapter

    #region Bentley Namespaces
    using BMW = Bentley.MstnPlatformNET.WinForms;
    #endregion
    
    namespace DescartesStandAloneAddin2
    {
        public partial class Form1 :
            BMW.Adapter
        //Form    
        {
            /// <summary>Constructor</summary>
            internal Form1(Bentley.MstnPlatformNET.AddIn addIn)
            {
                m_addIn = addIn;
                InitializeComponent();
    
                //  Set up events to handle resizing of form; closing of form
                this.SizeChanged += new System.EventHandler(Form1_SizeChanged);
                this.Closed += new System.EventHandler(Form1_Closed);
            }
        }
    }

    Best Regards

    Marty

  • Thanks, Marty!

    Your MicroStation Tools for Viz Studio are very useful.  Besides a WinForm class that inherits from the Bentley Adapter, do you also provide a WPF class, as Jan recommends?

     
    Regards, Jon Summers
    LA Solutions

  • Hi Jon,

    Yes, there is a WPF class. If you create a C# project using the Visual Studio Tools for MicroStation CONNECT Edition, you can then right click on the project and select Add/New Item. Select Bentley/MicroStation CONNECT Edition and you will notice a variety project items that can be added to the project. One is the MicroStation WinForm as we discussed and another one is a WPF Usercontrol. You can add this project item and inspect its contents to see how it is implemented. When you select a project item to add to the project, you will be asked if you want to add this to the keyins. If you select yes, the item will be added to Command.xml (command table) for the first level keyin. At the same time, the associated call to KeyinCommands.cs will be created. This method will show you how the user control is implemented. The MicroStation components are in the Usercontrol.xaml.cs file. Although this class inherited from Usercontrol, Bentley.MstnPlatformNET.WPF.DockableWindow will be used to attach the wpf to MicroStation. The end result by default is a single instance, wpf child of MicroStation with Bentley product icon, dockable, and stays on top of MicroStation.

    FYI: in Visual Studio Tools for MicroStation V8i for .Net 3.5, the WPF is hosted in a WinForm with the same behavior as a child of MicroStation.

    Best Regards

    Marty

  • Unknown said:
    An overall concept is not different from V8i API, so you can also check older examples

    Including the problem that you can't use Viz Studio designer to edit a Form that inherits from Bentley's Adapter class?

     
    Regards, Jon Summers
    LA Solutions

  • True. We need to try to remember to enter the form code editor and comment out the Bentley Adapter class and inherit from System.Windows.Forms.Form class. Now we can use the form designer, makes changes and close the form designer.  We can now uncomment the Bentley Adapter class and comment out System.Windows.Forms.Form class in the code editor.

    We do not have this issue with the WPF designer. We could even use Blend.

  • I just switch my build to a custom "Design" configuration which defines DESIGN and created this custom class

    #if DESIGN
    
        public class MsForm : Form{
    
            private void AttachAsTopLevelForm(TopoDOTApp myAddin, bool b)
            {
                //throw new NotImplementedException();
            }
    
            public MsForm()
            {
    
                
            }
    #else
        internal class MsForm : Bentley.MstnPlatformNET.WinForms.Adapter
        {
            public MsForm()
            {
                this.FormClosing += this.MsForm_FormClosing;
                this.Load += this.MsForm_Load;
            }
    #endif

    Answer Verified By: Jon Summers 

  • Unknown said:
    We do not have this issue with the WPF designer. We could even use Blend.

    It's exactly the reason I plan to don't use WinForms for CE development at all. A separation of form content and style definition to xaml, that can be edited by tools like Blend, binded to code, provides great flexibility.

    Regards,

      Jan

  • Unknown said:
    I just switch my build to a custom "Design" configuration which defines DESIGN and created this custom class

    I agree it's quite elegant solution. I use the same approach (to define custom Configurations) to control usage of analyzers. They provide great feedback about code quality, but substantially increase compilation time: Now my new projects contain usually Debug and Debug (analyzers), where Debug is configured to use standard rules (I guess they are Microsoft Managed Recommended Rules) and Debug (analyzers) with several different analyzers applied.

    So I can imagine to use Debug and Release Configurations where Adapter is inherited and the code compiled correctly and e.g. Debug (form design) where Form class is used and WinForm designer can be used.

    With regards,

      Jan