In this part, we will introduce how to use the Modal dialog box, Modeless dialog box, Tool Settings dialog box, and Dockable dialog box in MicroStation. Modal, non-modal, and docked dialogs are used in various applications. The tool settings dialog is unique to MicroStation and is itself a non-modality, but its content will automatically change as the user selects the tool. Dialogs in the CE version Add-ins can be implemented using both WinForms and WPF. Both can use the visual tools in VS to directly design the dialog interface, which is much more efficient than using the traditional MDL .r resource programming. A person who has written a lot of tedious .r resources will feel that writing interfaces with Add-ins are simply a pleasure. Let's expand our csAddins application step by step.
1. Open the csAddins solution in VS, click the menu Project > Add Windows Form, enter ModalForm.cs in the Name field of the form that appears, and click the Add button to create a new form.
2. Click the Toolbox button (or press Ctrl+Alt+X when Form1 focus) to open the tool box window, which has a variety of form controls for us to choose, drag and place the control on the Form1 form and position it accordingly. Click the Properties icon button (or press Alt+Enter when Form1 is focused) to open the Properties window, which has many properties that we need to set. As shown below:
3. The final designed ModalForm is shown in the figure below. The attributes that need to be set are given below. The Modifiers property of textBox1 is set to Public, the purpose of which is to access the values in the ModalForm class in the DemoForm class that calls the form. Note that the Location and Size in the properties do not need to be manually set, but can be achieved by visually dragging VS.
Properties:
4. Let's create a non-modal dialog box below. Insert a WinForm in the csAddins project, named MultiScaleCopyForm. It is so named because the WinForm will be used in the next chapter. Drag various controls from the VS toolbar to the form and set the form and control properties as follows.
Properties:
5. Right-click MultiScaleCopyForm.cs in the VS solution browser and select View Code in the pop-up menu to open the source code of MultiScaleCopyForm, and modify your code as shown below. Note the following key points:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; using Bentley.MstnPlatformNET.WinForms; namespace csAddins { public partial class MultiScaleCopyForm : //Form Adapter { public MultiScaleCopyForm() { InitializeComponent(); } private void MultiScaleCopyForm_FormClosed(object sender, FormClosedEventArgs e) { RegistryKey rootKey = Registry.CurrentUser.OpenSubKey("Software", true); RegistryKey appKey = rootKey.CreateSubKey("csAddins"); RegistryKey myKey = appKey.CreateSubKey("MultiScaleCopy"); myKey.SetValue("txtScale", txtScale.Text.ToString()); myKey.SetValue("txtXOffset", txtXOffset.Text.ToString()); myKey.SetValue("txtYOffset", txtYOffset.Text.ToString()); myKey.SetValue("txtZOffset", txtZOffset.Text.ToString()); myKey.SetValue("txtCopies", txtCopies.Text.ToString()); } private void MultiScaleCopyForm_Load(object sender, EventArgs e) { RegistryKey myKey = Registry.CurrentUser.OpenSubKey("Software\\csAddins\\MultiScaleCopy"); if (null != myKey) { txtScale.Text = myKey.GetValue("txtScale").ToString(); txtXOffset.Text = myKey.GetValue("txtXOffset").ToString(); txtYOffset.Text = myKey.GetValue("txtYOffset").ToString(); txtZOffset.Text = myKey.GetValue("txtZOffset").ToString(); txtCopies.Text = myKey.GetValue("txtCopies").ToString(); } } private void txtScale_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.') e.Handled = true; } private void txtXOffset_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.' && e.KeyChar != '-') e.Handled = true; } private void txtCopies_KeyPress(object sender, KeyPressEventArgs e) { if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b') e.Handled = true; } private void btnDefault_Click(object sender, EventArgs e) { txtScale.Text = txtScale.Tag.ToString(); txtXOffset.Text = txtXOffset.Tag.ToString(); txtYOffset.Text = txtYOffset.Tag.ToString(); txtZOffset.Text = txtZOffset.Tag.ToString(); txtCopies.Text = txtCopies.Tag.ToString(); } } }
Warning: After we change the base class of the form from Form to Adapter, VS will not be able to edit your WinForm visually. At this time, the following error message will appear when you switch to the design form. This requires you to change the Adapter to Form first, visually edit, and then change the Form back to Adapter after editing.
6. Similarly, create the NoteCoordForm form. All the controls need to use standard actions, and no associated event handling methods are required. The only thing to note is to change its base class from Form to Adapter. We will use this form as a tool setting box later.
Properties:
7. Prepare three picture files to represent modal, non-modal, and tool dialog boxes respectively. Then right-click the project name csAddins and select Properties in the pop-up menu to open the project properties window, switch to the resource page, and click Add Resource to add image resources under the premise that the Image type resource is selected.
8. Create a new form ToolbarForm, add three-button controls, and a ToolTip control in it. Assign the three picture files from the previous step to the three buttons respectively. The final result is shown in the figure below.
Properties:
9. Double-click the three buttons to construct the event handling method. Then edit the ToolbarForm.cs source code as follows. Pay attention to the following two key points:
When the user clicks the button, send a command to Mstn. The specific command processing function is implemented in the next step.
using Bentley.MstnPlatformNET; using Bentley.MstnPlatformNET.GUI; using Bentley.MstnPlatformNET.WinForms; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace csAddins { public partial class ToolbarForm : //Form Adapter, IGuiDockable { public ToolbarForm() { InitializeComponent(); } public bool GetDockedExtent(GuiDockPosition dockPosition, ref GuiDockExtent extentFlag, ref System.Drawing.Size dockSize) { return false; } public bool WindowMoving(WindowMovingCorner corner, ref System.Drawing.Size newSize) { newSize = new System.Drawing.Size(118, 34); return true; } private void btnModal_Click(object sender, EventArgs e) { Session.Instance.Keyin("csAddins DemoForm Modal"); } private void btnTopLevel_Click(object sender, EventArgs e) { Session.Instance.Keyin("csAddins DemoForm TopLevel"); } private void btnToolSettings_Click(object sender, EventArgs e) { Session.Instance.Keyin("csAddins DemoForm ToolSettings"); } } }
10. Create a new DemoForm class named DemoForm.cs to open different types of dialog boxes. It is also a command processing class. Its content is as follows. For different dialog box types, we call the AttachAsGuiDockable, AttachAsTopLevelForm, and AttachToToolSettings methods in the Adapter.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace csAddins { class DemoForm { public static void Toolbar(string unparsed) { ToolbarForm myForm = new ToolbarForm(); myForm.AttachAsGuiDockable(MyAddin.Addin, "toolbar"); myForm.Show(); } public static void Modal(string unparsed) { ModalForm myForm = new ModalForm(); if (DialogResult.OK == myForm.ShowDialog()) MessageBox.Show(myForm.textBox1.Text.ToString()); } public static void TopLevel(string unparsed) { MultiScaleCopyForm myForm = new MultiScaleCopyForm(); myForm.AttachAsTopLevelForm(MyAddin.Addin, false); myForm.Show(); } public static void ToolSettings(string unparsed) { NoteCoordForm myForm = new NoteCoordForm(); myForm.AttachToToolSettings(MyAddin.Addin); myForm.Show(); } } }
11. Modify the existing commands.xml file and add four new commands csAddins DemoForm Toolbar|Modal|TopLevel|ToolSettings. The lines that need to be added are shown below:
<SubKeyinTables> <KeyinTable ID="CreateElement"> <Keyword SubtableRef="Commands" CommandWord="CreateElement"> <Options Required="true"/> </Keyword> <Keyword SubtableRef="DemoForm" CommandWord="DemoForm"> <Options Required="true"/> </Keyword> </KeyinTable> </SubKeyinTables> <SubKeyinTables> <KeyinTable ID="Commands"> <Keyword CommandWord="LineAndLineString1"> </Keyword> <Keyword CommandWord="LineAndLineString2"> </Keyword> <Keyword CommandWord="LineAndLineString3"> </Keyword> <Keyword CommandWord="ShapeAndComplexShape"> </Keyword> <Keyword CommandWord="TextString"> </Keyword> <Keyword CommandWord="Cell"> </Keyword> <Keyword CommandWord="Dimension"> </Keyword> <Keyword CommandWord="BsplineCurve"> </Keyword> </KeyinTable> <KeyinTable ID="DemoForm"> <Keyword CommandWord="Toolbar"/> <Keyword CommandWord="Modal"/> <Keyword CommandWord="TopLevel"/> <Keyword CommandWord="ToolSettings"/> </KeyinTable> </SubKeyinTables> <KeyinHandlers> <KeyinHandler Keyin="csAddins CreateElement LineAndLineString1" Function="csAddins.CreateElement.LineAndLineString1"/> <KeyinHandler Keyin="csAddins CreateElement LineAndLineString2" Function="csAddins.CreateElement.LineAndLineString2"/> <KeyinHandler Keyin="csAddins CreateElement LineAndLineString3" Function="csAddins.CreateElement.LineAndLineString3"/> <KeyinHandler Keyin="csAddins CreateElement ShapeAndComplexShape" Function="csAddins.CreateElement.ShapeAndComplexShape"/> <KeyinHandler Keyin="csAddins CreateElement TextString" Function="csAddins.CreateElement.TextString"/> <KeyinHandler Keyin="csAddins CreateElement Cell" Function="csAddins.CreateElement.Cell"/> <KeyinHandler Keyin="csAddins CreateElement Dimension" Function="csAddins.CreateElement.Dimension"/> <KeyinHandler Keyin="csAddins CreateElement BsplineCurve" Function="csAddins.CreateElement.BsplineCurve"/> <KeyinHandler Keyin="csAddins CreateElement Cone" Function="csAddins.CreateElement.Cone"/> <KeyinHandler Keyin="csAddins DemoForm Toolbar" Function="csAddins.DemoForm.Toolbar"/> <KeyinHandler Keyin="csAddins DemoForm Modal" Function="csAddins.DemoForm.Modal"/> <KeyinHandler Keyin="csAddins DemoForm TopLevel" Function="csAddins.DemoForm.TopLevel"/> <KeyinHandler Keyin="csAddins DemoForm ToolSettings" Function="csAddins.DemoForm.ToolSettings"/> </KeyinHandlers>
12. Compile the entire project in VS. If you can compile successfully at one time, then you are very lucky. After successfully generating csAddins.dll, return to Mstn to load it, and type csAddins DemoForm Toolbar in the Keyin box to call up the toolbar shown in step 8. Click each button to open the modal, non-modal, and tool setting dialog boxes respectively. At the same time, this toolbar dialog can be docked.
Prev:[[Adding Commands to Add-ins]] | Next:[[Use DgnPrimitiveTool and DgnElementSetTool to implement interactive commands]] |