(Print this page)

The Strippers’ club
Published date: Monday, May 1, 2006
On: Moer and Éric Moreau's web site

If you are a male, I surely have your attention now. Don’t be disappointed but it won’t be related to sex in anyways.

Starting this month, all my columns will target Visual Studio 2005. Chances are that I will revisit some of the articles I wrote over the last 3 years to tell you how you can benefits from the changes between VB 2005 and VB.Net 2003 (and even in some case VB.Net 2002).

I have already done an introduction to major features of VS 2005 in the November 2005 edition of Universal Thread . I will start this month with the Strip controls family. It is not a single control but a family of controls that will all work together. Controls like the ToolStrip (replaces the ToolBar), the StatusStrip (replaces StatusBar), the MenuStrip (replaces the MainMenu), and the ContextMenuStrip (replaces the ContextMenu) are an improvement over their previous version. They all have more options and are all working much the same way so that when you teach one you know most of the others by the same time.

All the controls of this family provide the same look and feel as Microsoft Office 2003.

A note about the download the demo

The downloadable demo contains the code found here in the article in addition to other to get fully functional application. For example, a RichTextBox object is used with an OpenFileDialog and a SaveFileDialog component in order to have the File Open and File Save menu items working (have a look at my article of June 2003 if you need help on these dialogs).

Backward compatibility

In the past, Microsoft often replaced existing controls with newer version. That means that you were upgrading an application, oldest controls were automatically replaces with newer version. That was correct in many situations but also occasionally led to strange behaviors. The controls MainMenu, ToolBar, StatusBar, and ContextMenu are not included by default in the Visual Studio Toolbox but they are still included in the .NET Framework, primarily for backwards compatibility. So if you need the oldest version for any reason, there still there.

The toolbox

After you created a new Windows form application, open a form in the designer and watch the toolbox. A section dedicated to the strip controls is created by default.

Figure 1: The toolbox


Starting with the menu strip control

Probably the most used control of these is the MenuStrip control. Double-clicking the MenuStrip control adds one to the form. The menu is already docked to the top of the form because, by convention, it goes right there.

You surely recognize the “Type Here” placeholder inviting you to start creating your menu entries just as in VB.Net 2003. But before doing so, I invite you to discover a really cool new feature called the “smart tag”.

Ensure that the MenuStrip control is selected and watch its upper-right corner, you will find a little arrow (see figure 2). This is the so called “smart tag”. If you click on it, a contextual menu appears offering you some frequent tasks related to the currently selected control. With the MenuStrip control, one of the most exciting options is the “Insert Standard Items”. Click on it and you will find a standard menu created with standard items and many of these items already have icons. Also notice that the name of each item is not menuitem1 but a fairly explicit name like NewToolStripMenuItem (for the File New item).

Figure 2: The menu strip control and its smart tag

All that is left now is to insert your own items and … a small detail, write the code required for each of these items (because menu items are created but the code that goes with it is not).

To have a menu item executing some task, you need to add code to its Click event just like we have done for the last decade. For example, I have added a “About” form to the demo application (using the template called “About Box” when adding the new Windows form). Once your form is created, you need to add the handler to the click event of menu item to display your form. The easiest, like it always as been, is to double click the menu item from the designer. Once your event signature is added to your code, all you need to do is:

AboutBox1.ShowDialog()
Isn’t this line a bit strange if you have done VB.Net 2002-2003? It is the old-VB6 syntax that Microsoft decided to get back in VB2005. Is this an improvement? I let you answer that one.

Notice that you can still use the “long version” like this:

Dim fAbout As New AboutBox1
fAbout.ShowDialog()
fAbout = Nothing

The StatusStrip control

The newest version of this control is no more limited to only display labels. It has intrinsic features to display buttons, progress bars and drop downs.

Now that we have the menu, let’s implement a status bar. This time, the smart tag will not create standard items simply there is no standard template for a tool bar. That doesn’t mean that the smart tag is of no use, a very important option can be found there called “Edit items…”. Selecting this option opens the “Items Collection Editor” (as shown in figure 3).

Figure 3: The Item Collection Editor

This designer can also be opened by right-clicking the StatusStrip control or by selecting the Items property from the properties window.

The first thing you need to do in this editor is to select the type of element you want to add to your StatusStrip using the top-left combobox and click the Add button. Now that you have added the items you want, it is time to set their properties.

A ToolStripStatusLabel is pretty simple. You mostly set its text property but you may want to have look at other important properties that are self-explained like BorderStyle, BorderSides, DisplayStyle, AutoSize.

The ToolStripDropDownButton is able to display a menu. And it isn’t a plain old menu with text items but also comboboxes, separators and even textboxes. If you want to display some text on the StatusStrip, change the DisplayStyle to Text and then set the Text property. Now to add elements in the drop down, you need to select the DropDownItems and click the ellipsis. This will open another editor dedicated to these elements as shown in figure 4.

Figure 4: Drop down items editor

Again you need to select the item type you want from the top-left combobox. The MenuItem type and the Separator should already be familiar to you. The TextBox type lets you embed a textbox into the menu (if you ever used Access, you surely saw that and ask yourself how they’ve done that). The tricky thing is how to get rid of the drop down when you detect the textbox to be completed? I found that the best way to do this is to call the HideDropDown menu. For example, I have added to detect when the Enter key was pressed using the KeyDown event in that TextBox like this:
If e.KeyCode = Keys.Enter Then
    MessageBox.Show("You just entered " + _
                    ToolStripTextBox1.Text + _
                    " into the textbox of the ToolStripDropDown")
    ToolStripDropDownButton1.HideDropDown()
End If
I have applied the same method in the SelectedIndexChanged event when an item is selected in the combobox:
MessageBox.Show("You just entered " + _
                ToolStripComboBox1.Text + _
                " into the textbox of the ToolStripDropDown")
ToolStripDropDownButton1.HideDropDown()
The ToolStripSplitButton is very similar to the ToolStripDownButton in that it can display a menu. The only difference I have noticed is that this control offers a Click event on the button part (that does not interfere with the drop down section like the ToolStripDownButton).

To test the ToolStripProgressBar , I have added a timer to the test form. On the Click event of this ToolStrip element, I start the timer after having set the Minimum, Maximum and Value property of the progress bar like this:

With ToolStripProgressBar1
    .Minimum = 1
    .Maximum = 10
    .Value = 1
End With
Timer1.Interval = 5 'to make it start quickly
Timer1.Start()
On each Tick event, the Value property is incremented until the Maximum property is reached like this:
With ToolStripProgressBar1
    .Value += 1
    If .Value = .Maximum Then
        Timer1.Stop()
    End If
End With
Timer1.Interval = 250

The ToolStrip

Just like the MenuStrip, the ToolStrip control also has a smart tag that automatically inserts standard items. In my demo, I have used this feature to insert the first buttons for me.

You must then write the code to link your buttons to the related menu events. The cool thing I found is that each button of the ToolStrip now has its own Click event (in the previous versions, we were having a global event for the toolbar and we then had to find which button was pressed). So this is the kind of code you should have in your Click event of the Save button:

SaveToolStripMenuItem.PerformClick()

Figure 5: Available
components of a ToolStrip

That’s it! You just have to call the related menu event because everything is already coded there and I hope you will get as lazy (I should say proficient) as me and not repeat the same code twice in your form.

But the ToolStrip is not limited to host buttons. Take a look at figure and you will discover that 8 different components can be hosted. This list should look familiar to you as we have already seen these when we have explored the StatusStrip. In fact, the StatusStrip inherits the ToolStrip (and so does the MenuStrip for which I haven’t said that it can also contain a TextBox and a ComboBox and display the “Edit Items…” dialog). This list was made visible after I have clicked the small arrow after the last item of the ToolStrip. It allows you to select and insert the selected component. Once inserted, a component can be dragged and dropped elsewhere on the ToolStrip so it is very easy to reorder items. Instead of manipulating elements of the ToolStrip like this, you can get access to the same dialog you have used for the StatusStrip. This dialog is available when you select “Edit items…” from the smart tag, or “Edit items…” after you right-click your ToolStrip, or if you click the ellipsis of the Items property of your ToolStrip (from the Properties dialog).

Conclusion

I will let you discover the ContextMenuStrip which isn’t different from regular MenuStrip.

These new controls really offer great improvements (over there previous version)… until the new version of Office is out!

I hope you appreciated the topic and see you next month.


(Print this page)