To call Visual Studio .NET “pretty cool” is getting a little clichéd.
You know it. I know it. We’ve both created Web applications in minutes, figured out the
power behind the .NET Framework, and read how to create a Windows service without needing to
plug into the devilish delights of C++.
As the Microsoft advertisement I read this morning quite rightly points out: It’ll save you
thousands of development hours and position you far in front of your competitors. Unless, of
course, your competitors are also using Visual Studio .NET. Which is, in fact, extremely
To stay ahead of the field you need to re-figure out all those nifty little secrets you
knew back in Visual Basic 6, getting back to the stage where you knew all those
nooks-and-cranny tricks that only time teaches. I’m talking about those nuggets of knowledge
that can save you even more time, and metamorphose you from an advanced user straight
through to pure .NET expert level. And that’s just what I’m going to cover in this intriguing
series of twelve articles.
Over the next five months, I’ll be writing an article every two weeks, sharing a few of the
nuggets I’m compiling into my next book, VB.NET and ASP.NET Secrets. I’ll be covering
top time-taught tricks for creating Windows, ASP.NET, database, and Web service
applications—and a little more, too.
In this first article, we begin our three-part look at Windows applications, exploring a
few of the simpler gems that you just might not have tagged onto yet. We’ll be looking at:
- Making your form transparent
- Figuring out who stole the ToolTips
- Uncovering how resizing is made easy with VB.NET
- Discovering how to create split panels, Explorer-style
- How to save user time by adding auto-complete to combo boxes
- The power of command-line parameters.
And, don’t forget, this is the simple beginning. Over the next eleven articles, you’ll find
it all gets gloriously exciting.
Ready to begin?
Making your Form Transparent
You can give your Windows form a great transparent look by altering its Opacity
property. Set this anywhere between 0% (completely transparent) and 100% (regular opaque) to
see the windows underneath your application.
Who Stole the ToolTips?
If you haven’t already noticed, someone stole the ToolTip property in .NET. If you want
little popup messages to appear when you hover your mouse over a button or whatever, you’ll
need to figure out the ToolTip control.
Here’s how it works. First, you add an instance of the ToolTip control to your form. This
is an invisible component that actually displays the message. You can alter its properties
through the Property window, such as whether it is “Active” or the popup delay.
Next, you need to add the actual ToolTip messages. Click on any of your controls and scroll
down to the “Misc” section (presuming you order your property list by category). You’ll
see a property called something like “ToolTip on ToolTip1“: This is a fresh property
your ToolTip instance gave every control on your form.
Simply set this property to your ToolTip message—and that’s a wrap!
Tricks of the Trade: Resizing Made Easy!
When the user resizes your Windows form at runtime, by default all of your controls will
stay in place. They will not automatically resize with the form. You can change this behavior
by editing the Anchor property of a control.
The Anchor property determines which sides of a form that control will stretch with.
After the default, the most common setting for this property is “Top, Bottom, Left,
Right”—meaning the control will stretch with all sides of your form, behaving like the
majority of resizable Windows applications.
The Dock property of a control is also useful when positioning and resizing
controls. It allows you to dock a control to a particular side of a form and stick with that
side, regardless of how the form is resized. To set this, simply select a new region via the
Dock property drop-down.
Creating Split Panels, Explorer-Style
If you’re looking to create the split panel look seen in many modern applications, you’ll
be happy to learn that a new .NET Splitter control can help you achieve exactly that
Here are the simple steps to recreating the split panel look in your own programs:
- Add the control you want to appear down the left of your screen to your form. This could
be a TreeView control, if you’re going for the Explorer effect, or a Panel control if you want
to add numerous controls (such as a list of icons for a menu, Outlook-style).
- Set the Dock property of this control to Left.
- Add a Splitter control to your form. It should automatically set its Dock property to
Left. This is the widget your users will “grab” to resize the panels.
- Add your final control to your form. If you’re continuing that elusive Explorer look, this
will probably be the ListView control.
- Set the Dock property of this control to Fill (click the box in the center).
And that’s it! You can now try running your application. Your users will be able to “drag”
the Splitter and your two controls/panels will automatically resize.
Figure 1: Our Splitter control in action
Save User Time: Add AutoComplete to Combo Boxes
Develop an application in a program such as Microsoft Access and all your combo boxes will
incorporate “AutoComplete” by default, that ability to be able to tap a few characters in a
drop down and have the nearest matching selection picked out for you.
In Visual Basic however, there’s no such intrinsic support. If you want AutoComplete,
you’ve got to do it yourself. And this tip shows you how.
Simply add the following methods to your form. The first is called AutoCompleteKeyUp
and accepts a combo box and KeyEventArgs object as arguments. You need to call these in
the KeyUp event of your combo box: it looks at what the user has typed and selects the
most appropriate match. The second is called AutoCompleteLeave and should be called
when the Leave event of your combo box is fired. This one simply takes whatever you’ve
finally chosen and cases it properly, as per the matching selection in the combo box.
Let’s look at those functions now:
Public Sub AutoCompleteKeyUp(ByVal Combo As ComboBox, _ ByVal e As KeyEventArgs) Dim strTyped As String Dim intFoundIndex As Integer Dim objFoundItem As Object Dim strFoundText As String Dim strAppendText As String ' Ignore basic selection keys Select Case e.KeyCode Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, _ Keys.Delete, Keys.Down, Keys.CapsLock Return End Select ' Find what user has typed in list strTyped = Combo.Text intFoundIndex = Combo.FindString(strTyped) ' If found... If intFoundIndex >= 0 Then ' Get list item (actual type depends on whether data bound) objFoundItem = Combo.Items(intFoundIndex) ' Use control to resolve text - in case data bound strFoundText = Combo.GetItemText(objFoundItem) ' Append the typed text to rest of the found string ' (text is set twice due to a combo box quirk: ' on certain platforms, setting just once ignores casing!) strAppendText = strFoundText.Substring(strTyped.Length) Combo.Text = strTyped & strAppendText Combo.Text = strTyped & strAppendText ' Select the appended text Combo.SelectionStart = strTyped.Length Combo.SelectionLength = strAppendText.Length End If End Sub Public Sub AutoCompleteLeave(ByVal Combo As ComboBox) ' Correct casing when leaving combo Dim intFoundIndex As Integer intFoundIndex = Combo.FindStringExact(Combo.Text) Combo.SelectedIndex = -1 Combo.SelectedIndex = intFoundIndex End Sub
And here’s how you may call these functions from your combo box:
Private Sub ComboBox1_KeyUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles ComboBox1.KeyUp AutoCompleteKeyUp(ComboBox1, e) End Sub Private Sub ComboBox1_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles ComboBox1.Leave AutoCompleteLeave(ComboBox1) End Sub
That’s all you need to create your own AutoComplete combo boxes. And if you’re feeling
really adventurous, you might even want to wrap all of this up into a neat little user
control. But we’ll leave that for another tip…
Figure 2: Our AutoComplete combo box strutting its stuff.
The Power of Command-Line Parameters
Command line parameters can be incredibly useful. They allow users or other applications to
pass startup information to your program. For example, if your program was called myapp.exe,
they might run the following:
Here, we have one command line parameter, “/nodialogs”. In VB6, we could read this using
the Command property. In VB.NET, this has been replaced with the
System.Environment.GetCommandLineArgs function, which returns an array of the passed
And here’s a chunk of code to show you just how to read them:
Dim MyStartupArguments()As String, intCount As Integer MyStartupArguments = System.Environment.GetCommandLineArgs For intCount = 0 To UBound(MyStartupArguments) MessageBox.Show(MyStartupArguments(intCount).ToString) Next
Coming up in part two of Windows Secrets, learn how to:
- Extract HTML from the RichTextBox control
- Create applications that accept drag-and-drops from Windows Explorer
- Capture the screen, quick and easy
- Use the .NET equivalent of PrevInstance.
See you then!
About the Author
Karl Moore is a technology author living in Yorkshire, England. He runs his
own consultancy group, White Cliff Computing Ltd, and is author of two best-selling books
exposing the secrets behind Visual Basic .NET. When he’s not writing for magazines, speaking
at conferences, or making embarrassing mistakes on live radio, Karl enjoys a complete lack of
a social life. You can visit Karl on the Web at www.karlmoore.com.
# # #