Archive

Archive for January, 2009

Namespaces in VB.NET

January 25, 2009 programmervb Leave a comment

By Dan Mabbutt

The most common way that VB.NET namespaces are used by most programmers is to tell the compiler which .NET Framework libraries are needed for a particular program. When you choose a “template” for your project (such as “Windows Forms Application”) one of the things that you’re choosing is the specific set of namespaces that will be automatically referenced in your project. This makes the code in those namespaces available to your program.

For example, some of the namespaces and the actual files they are in for a Windows Forms Application are shown below:

System –> in System.dll
System.Data –> in System.Data.dll
System.Deployment –> System.Deployment.dll
System.Drawing –> System.Drawing.dll
System.Windows.Forms –> System.Windows.Forms.dll

You can see (and change) the namespaces and references for your project in the project properties under the References tab. I have previously written about this side of namespaces in the article, References and Namespaces in VB.NET.

This way of thinking about namespaces makes them seem to be just the same thing as “code library” but that’s only part of the idea. The real benefit of namespaces is organization.

Most of us won’t get the chance to establish a new namespace hierarchy because it’s generally only done once ‘in the beginning’ for a large and complicated code library. But this article will tell you how to interpret (and … Hey! … maybe even criticize) the namespaces that you will be asked to use in many organizations.

Namespaces make it possible to organize the tens of thousands of .NET Framework objects … and all the objects that VB programmers create in projects too … so they don’t clash.

For example, if you search .NET for a Color object, you find two. There is a Color object in both:

System.Drawing
System.Windows.Media

If you add an Imports statement for both namespaces (a reference may also be necessary in the project properties) …

Imports System.Drawing
Imports System.Windows.Media

… then a statement like …

Dim a As Color

… will be flagged as an error with the note, “Color is ambiguous” and .NET will point out that both namespaces contain an object with that name. This kind of error is called a “name collision.”

This is the real reason for “namespaces” and it’s also the way namespaces are used in other technologies (such as XML). Namespaces make it possible to use the same object name, such as Color, when the name fits and still keep things organized. You could define a Color object in your own code and keep it distinct from the ones in .NET (or the code of other programmers).

Namespace MyColor
Public Class Color
Sub Color()
‘ Do something
End Sub
End Class
End Namespace

You can also use the Color object somewhere else in your program like this:

Dim c As New MyColor.Color
c.Color()

Before getting into some of the other features, be aware that every project is contained in a namespace. VB.NET uses the name of your project (WindowsApplication1 for a standard forms application if you don’t change it) as the default namespace. To see this, create a new project (I used the name NSProj and check out the Object Browser tool:

——–
Click Here to display the illustration
Click the Back button on your browser to return
——–

The Object Browser shows your new project namespace (and the automatically defined objects in it) right along with the .NET Framework namespaces. This ability of VB.NET to make your objects equal to .NET objects is one of the keys to the power and flexibility. For example, this is why Intellisense will show your own objects as soon as you define them.

To kick it up a notch, let’s define a new project (I named mine NewNSProj in the same solution (use File > Add > New Project …) and code a new namespace in that project. And just to make it more fun, let’s put the new namespace in a new module (I named it NewNSMod). And since an object must be coded as a class, I also added a class block (named NewNSObj). Here’s the code and Solution Explorer to show how it fits together:

——–
Click Here to display the illustration
Click the Back button on your browser to return
——–

Since your own code is ‘just like Framework code’, it’s necessary to add a reference to NewNSMod in NSProj to use the object in the namespace, even though they’re in the same solution. Once that’s done, you can declare an object in NSProj based on the method in NewNSMod. You also need to “build” the project so an actual object exists to reference.

Dim o As New NewNSProj.AVBNS.NewNSMod.NewNSObj
o.AVBNSMethod()

That’s quite a Dim statement though. We can shorten that by using an Imports statement with an alias.

Imports NS = NewNSProj.AVBNS.NewNSMod.NewNSObj

Dim o As New NS
o.AVBNSMethod()

Clicking the Run button displays the MsgBox from the AVBNS namespace, “Hey! It worked!”

So far, we have talked about how to use namespaces. On the next page, we’ll talk about when and why.

Categories: Tutorial, VBNet

Dim thirdOfJuly As Date = #7/3/1776 11:59:59 PM#

January 23, 2009 programmervb 1 comment

Public Class Tester
Public Shared Sub Main
Dim thirdOfJuly As Date = #7/3/1776 11:59:59 PM#
Dim fourthOfJuly As New Date(1776, 7, 4)
Dim inTheMorning As New Date(1776, 7, 4, 9, 45, 30)

Console.WriteLine( _
“The 3rd and 4th of July, 1776…” & _
vbNewLine & vbNewLine & _
“#7/3/1776 11:59:59 PM# … ” & _
thirdOfJuly.ToString & vbNewLine & _
“New Date(1776, 7, 4) … ” & _
fourthOfJuly.ToString & vbNewLine & _
“New Date(1776, 7, 4, 9, 45, 30) … ” & _
inTheMorning.ToString)
End Sub

End Class

Categories: VBNet

Adding System Wide ‘Auto Complete’ to your Textbox/Combobox Controls in Visual Basic 2005.

January 18, 2009 programmervb 1 comment

As you will see, adding Auto Complete support to textbox and combobox controls has NEVER been easier than it is now in Visual Basic.NET 2005. No longer do you HAVE to use the Windows API’s to get the Auto Complete functionality.

I will assume you already have a ‘Windows Application’ based project open. You can go ahead and add a textbox control or combobox control to the form. Select the textbox and goto the properties window. (I will assume you added a textbox, but the process is the same for a combobox also. I will just refer to textbox instead of combobox.)

There are 3x properties that relate to the ‘Auto Complete’ feature. You have “AutoCompleteCustomSource”, “AutoCompleteMode” and “AutoCompleteSource”. AutoCompleteCustomSource is exactly how it sounds. You can have your own text that you want the user to be able to use when they are typing in the control. But, I am not interested in going over that property right now though. There is really nothing more to it than what I already said. :)

OK, the “AutoCompleteMode” property is simply how the AutoComplete text responds. Click on the “AutoCompleteMode” property and then open the dropDown list associated with it. You will see 4x ‘modes’ available. “None, “Suggest”, “Append”, and “SuggestAppend”. This property MUST be selected either “Append”, “Suggest”, or “SuggestAppend” for the AutoComplete feature to work. If you select “Suggest” mode, then ‘AutoComplete’ will display a list that matches the text that has been typed thus far that you can scroll through and choose from. If you select “Append” mode, then as you are typing, it will automatically highlight the closest match to the text thus far in the textbox/combobox control, usually in alphabetical order. Plus, while you are typing, you CAN use the ‘Up/Down’ buttons on the keyboard to scroll through the list of text that is similar to the text that was typed in the textbox control. The “SuggestAppend” mode, is of course the combination of the other 2x modes. As you are typing the text, it will highlight the first occurance of matching text while also displaying a list of all the text that matches what has been typed in the control.

The “AutoCompleteSource” property is exactly how it sounds. It is the location on the computer system to get the ‘AutoComplete’ list from. Click on that property and then click on the box to dropdown the enumerator values that are available as the source. Available values, at least on my computer are: “FileSystem”, “HistoryList”, “RecentlyUsedList”, “AllUrl”, “AllSystemSources”, “FileSystemDirectories”, “CustomSource”. Just like the “AutoCompleteMode” property, you MUST select one of these values for you to get ‘AutoComplete’ functionality. Since all of the values are self-explanatory, I will not go over them.

Just to test out the ‘AutoComplete’ feature. With a textbox control, (Or Combobox), click on the “AutoCompleteMode” property and select the “Suggest” mode. Then click on the “AutoCompleteSource” property and then select “AllUrl” as the source. Run the project, start typing in the textbox ‘www’ and you should see a list of url’s you’ve visited that start with ‘www’ popup. You can then click on one of the urls and it will put that url text in the textbox.

And thats ALL there is to it! Enjoy!

Categories: VBNet

Adding Custom Tooltips – Visual Basic .NET 2002/2003

January 18, 2009 programmervb 3 comments

In Visual Basic 6.0 we had a property for tooltip balloons. All you needed to do was type in the text you wanted to display. But you didn’t really have much control over the toolTips in general. In Visual Basic.NET, that simple feature appears to have been removed by default. That doesn’t mean we still can’t have tooltips. Below you will see just how simple it is to implement this feature and control how your tooltips will react. You have more “say” in what goes on with your tooltips. For Example, you can now set the length of time the pointer must remain stationary within the tooptip region before the balloon will appear.

To get started, start a project. In your ToolBox look for a ToolTip component. If you do not see one, then right click inside your toolbox and select “Customize Toolbox”. Click the ”.Net Frameworks Componets” tab, scroll down till you find “ToolTip” then check the box and press OK. You should now see a ToolTip component available. Double click the ToolTip to add it to your project. I re-named mine toolTip. If you look at the properties available for the ToolTip you will see: Automatic Delay, Initial Delay, and such that are available. I will not go over any of these settings since they are pretty much self-explanatory. Go ahead and add a button to the form. I named mine btn. In the btn_MouseHover event put:

toolTip.SetToolTip(btn, “Hello, This is just a simple test….”)

Start your project and place your mouse pointer within the button region and let it sit idle for a few seconds. You should then see the ToolTip balloon popup with the text you specified. You can also goto the ‘btn’ Button control property, you will see a property named: “Tooltip on tooltip”. You can put the text you want displayed and it will use your ToolTip settings without having to execute the toolTip yourself. As you can see, it is very easy to add support for ToolTips.

Categories: VBNet