Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js
As a Visual C++/MFC programmer, I've long preferred to use the various MFC classes to code user interfaces and the .NET Framework for areas where MFC doesn't provide a lot of support (regular expressions, disconnected data, XML, and so forth). In fact, I like this techniquecalled mixed-mode programmingso much that it was the focus of my latest bookExtending MFC Applications with the .NET Framework.
Having said that, I recently completed a project where the client wanted a Managed C++ Windows Forms system without any native, or unmanaged, code. During the process of coding this fairly complex set of GUI-rich applications, I came to realize that all the criticism of how difficult it is to write Managed C++ Windows Forms applications was greatly exaggerated. Please don't misunderstand. I'm certainly not going to tell my fellow veteran MFC coders that writing UI code in Windows Forms is as easy as in MFC. In many instances, it's not. For example, MDI applications are much easier to code using MFC than Windows Forms. However, plenty of standard user interfaces that we're accustomed to can be accomplished easily enough with Windows Forms, and some interfaces are even much easier to develop using Windows Forms!
Because the majority of my previous Managed C++ articles have focused on non-UI topics (with the obvious exception of the GDI+ articles), I devote this article (and more depending on the feedback I receive) to exploring how to create standard user interfaces using Windows Forms (see Figure 1). This first tip will illustrate how easy it is to create a typical SDI application with the following elements:
- Main menu
- Status bar
- List view that consumes the entire client area
- Panel control that basically performs the same functionality as a dialog bar
Figure 1. Creating standard user interfaces can be done in seconds with Visual Studio .NET
Adding the Main Menu
Once you've created a Managed C++ Windows Forms application, one of your first tasks is adding a menu. Visual Studio .NET doesn't do this via the Resource Editor any more. Instead, you drag a MainMenu control onto a form via the Toolbox as you would any UI control (see Figure 2). However, because the menu doesn't have a visual representation on the form itself, you'll see an icon with the menu's name.
Figure 2. Adding a menu to a form
Creating the Menu Items
To add a new menu or submenu to the form, click on the menu at the top of the form where you want the text to appear and type in the value. Optionally, you can set the menu item's Name property. You can name the menu item anything you want, but I prefer to create the menu item name from the fully qualified menu text. Therefore, if you have a menu item for Exit under the File menu, the menu item name would be FileExit. Previous versions of Visual Studio would do this for you automatically instead of naming your menu items menuItem1, menuItem2, and so on. Hopefully, something this basic will be addressed in future releases of Visual Studio.
Wiring the Menu Items to Event Handlers
To write an event handler for a menu item, double-click the menu item. This action will create a method for the Click event, with the method name taking the form [menuItemName]_Click. The fact that the menu name is used in the method name is why I recommend naming your menu items (and all other controls). Someone in one of my recent in .NET training seminars asked: "Why name the control if the editor is creating the method?" The answer is because a day or two from now, you'll look through your code and won't have any idea what a method name based on a default control name represents. In other words, FileExit_Click is going to be much easier to recognize than menuItem2_Click. Once Visual Studio .NET generates the method, it inserts the cursor into that method and you can code it to do what you need.
Adding the Status Bar
I typically define the status bar next because it can be difficult to add a status bar where you want on the form once you've already added other controls with docking abilities (more on that shortly). To add a status bar control, drag a StatusBar control from the toolbox onto the form (see Figure 3). Without other docking controls, VS.NET will place the status bar at the bottom of the form. The main properties to set here are the Name, Text (the default text that will default to the name property), and ShowPanels (a boolean value that indicates whether each panel in the status bar will be displayed as opposed to a 2D single flat panel). Once you've inserted the status bar control, you can update its text by changing its Text property.
Figure 3. Adding a status bar to a dialog/form is much easier than in previous versions of Visual Studio.