Welcome to the Visual Basic. NET. No matter who you are or what your background, If you’re reading this blog, you’re not to late to learn Visual Basic. You might, however, be a dummy about Visual basic, how to use it, or why it can help you do your job better.
This blog is expressly designed to make you a good Visual Basic programmer from start to finish. Visual Basic — despite all appearances — is really very easy to use. Much of the complexity of the language is hidden in tools provided to you by Microsoft. Many of these tools are not expressly for Visual Basic, but they will become very important to your success as a programmer. This blog is also about those tools, because they make writing good, working programs faster and easier.
This blog is also about you, the programmer. I’m a programmer like you. I have been using in Visual Basic 6 since 1999, and I’ve lived through all the ups and downs. In this blog, I help you become a good Visual Basic programmer.
Getting Started with VB. NET
I’m assuming you have installed Visual Studio 2005 or later. You may have even already explored the new environment on your own, but this blog doesn’t require any knowledge of Visual Basic 6. It doesn’t require anything more than a familiarity with windows. As you might already know, Visual Basic. NET is just one of the languages you can use to build applications with Visual Studio 2005 or later (Orcas). I also convinced you that it is also the simplest, most convenient language, but this isn’t really the issue. What you should keep in mind is that Visual Studio 2005 is an integrated environment for building, testing, and debugging a variety of applications : Windows applications, Web applications, classes and custom controls, console applications, even mobile application. It provides numerous tools for automating the development process, visual tools to perform many common design and programming tasks, and more features than any I would hope to cover.
The first thing you must learn is the environment you’ll be working in from now on. In the first section of this blog, you’ll familiarize yourself with the integrated development environment (IDE) and how its tools allow you to quickly design the user interface of your application, as well as how to program the application.
It will be a while before you explore all the items of the IDE. Visual Studio is an environment for developing all types of applications, from a simple Windows application to a complete Web app involving databases and XML files. I will explain the various items as needed in the course of the blog. In this section, we’ll look at the basic components of the IDE needed to build simple Windows applications.
Microsoft. NET Framework
When Microsoft announced its plans for. NET Framework, it surprised many developers, especially Visual Basic developers, who saw it as a giant step backward for " Rapid Application Development. " But the release of the. NET Framework version 1. 0 in 2002 did bring many needed benefits.
- .NET introduced a unified programming environment. All. NET - enabled languages compile to " Microsoft Intermediate Language " before being assembled into platform - specific machine code. Visual Basic and C # are language wrappers around this common. NET " language. " Because all. NET - enabled compilers speak the same underlying language, they no longer suffer from the many data and language conflicts inherent in other component - based systems such as COM. The. NET version of Visual Studio also unified the standard user interface that lets programmers craft source code.
- .NET committed developers to object - oriented technologies. Not only does. NET fully embrace the object - oriented programming paradigm, everything in. NET is contained in an object : all data values, all source code blocks, and the plumbing for all user - initiated events. Everything appears in the context of an object.
- .NET simplified Windows programming. Programming in Visual Basic before. NET was easy enough, until it came time to interact with one of the API libraries, something that happened a lot in professional programming. With. NET, most of these APIs are replaced with a hierarchy of objects providing access to many commonly needed Windows features. Because the hierarchy is extensible, other vendors can add new functionality without disrupting the existing framework.
- .NET enhanced security. Users and administrators can now establish security rules for different. NET features to limit malicious programs from doing their damage.. NET's " managed " environment also resolved buffer overrun issues and memory leaks through features such as strong data typing and garbage collection.
- .NET enhanced developer productivity through standards. The. NET Framework is built upon and uses many new and existing standards, such as XML and SOAP. This enhances data interchange not only on the Windows platform, but also in interactions with other platforms and systems.
- .NET enhanced Web - based development. Until. NET, a lot of Web - based development was done using scripting languages.. NET brings the power of compiled, desktop development to the Internet.
- .NET simplified the deployment of applications. If. NET is installed on a system, releasing a program is as simple as copying its EXE file to the target system (although an install program is much more user - friendly). Features such as side - by - side deployment, ClickOnce deployment (new in 2005), and an end to file version conflicts and " DLL hell " (the presence of multiple versions of the same DLL on a system, or the inability to remove a version of a DLL) make desktop and Web - based deployments a snap.
If you didn't understand some of the terms used in this section, that's all right. You will encounter them again, with explanations, in later section.
The Integrated Development Environment (IDE)
Visual Studio 2005 is an environment for developing Windows and Web applications. Visual Basic 2005 is just one of the languages you can use to program your applications. Actually, Visual Studio. NET was designed to host any language, and many companies are working on languages that will be integrated in Visual Studio 2005. Some people will develop Windows applications in Visual Studio. NET with C #, C ++, or J #.
So, what’s the distinction between Visual Studio. NET and the language ? Visual Studio. NET is the environment that provides all the necessary tools for developing applications. The language is only one aspect of a Windows application. The visual interface of the application isn’t tied to a specific language, and the same tools you’ll use to develop your application’s interface will also be used by all programmers, regardless of the language they’ll use to code the application.
The tools you’ll use to access databases are also independent of the language. Visual Studio provides tools that allow you to connect to a database, inspect its objects, retrieve the information you’re interested in, and even store it in objects that can be accessed from within any language.
There are many visual tools in the IDE, like the Menu Designer. This tool allows you to visually design menus and to set their names and basic properties (such as checking, enabling, or disabling certain options). Designing a menu doesn’t involve any code, and it’s carried out with point - andclick operations. Of course, you will have to insert some code behind the commands of your menus, and (again) you can use any language to program them.
To simplify the process of application development, Visual Studio. NET provides an environment that’s common to all languages, which is known as integrated development environment ( IDE). The purpose of the IDE is to enable the developer to do as much as possible with visual tools, before writing code. The IDE provides tools for designing, executing, and debugging your applications. It’s your second desktop, and you’ll be spending most of your productive hours in this environment.
Figure 1 - 1 The Visual Studio 2005 " Start Page "
Visual Studio 2005 is the third major release of the product since. NET's initial introduction in 2002. Each release (in 2002, 2003, and 2005) corresponded to a related release of the. NET Framework (versions 1. 0, 1. 1, and 2. 0, respectively) and of the. NET implementation of Visual Basic. The 2003 release was a relatively minor update to Visual Basic and the Framework, but the 2005 release is major. It is packed with new usability features, and comes in five delicious flavors.
- Visual Studio 2005 Express Edition. This entry - level product is geared toward the home hobbyist and weekend programmer who wants to learn. NET and one of its core programming languages, but won't be snuggling up to it on a daily basis. Visual Studio 2005 Express Edition is actually multiple Express Edition language products bundled together, including Visual Basic 2005 Express Edition (although Visual Basic 2005 Express Edition is also provided separately). Microsoft's goal is to introduce as many people as possible to the joys of. NET programming, so it offers the Express Edition at no cost. The package includes a simplified Visual - Studio - like user interface, but it does impose a few restrictions on your program - crafting ability. You can still edit the source code directly and craft applications of any complexity, but the Express UI won't always assist you with this. For instance, you cannot develop web applications with the Express product unless you install the separate Visual Web Developer product. Also, Express doesn't include much support for deployment ; applications designed with the Express Edition are generally expected to be used on your own workstation only.
- Visual Studio 2005 Standard Edition. Visual Studio's Standard Edition is just like the Express Edition, with a few extras thrown in, such as documentation on how to use the BCL and FCL (amazing), and deployment support through the ClickOnce deployment feature. It also includes support for mobile devices, such as cell phones and PDAs.
- Visual Studio 2005 Professional Edition. This is the minimum level required by programmers who will develop applications on a daily basis for money. It's the version that I use, and it includes all of the " power " features needed by a single programmer for both desktop and web - based development. The straightjacketed Express user interface is out, replaced by the full Visual Studio " mighty " Integrated Development Environment (IDE). But wait, there's more. You also get SQL Server 2005 Developer Edition. All instructions in this blog that relate to using the development environment refer to the Professional Edition. But if you are following along using the Express or Standard Editions, you will be just fine because the interfaces are quite similar.
- Visual Studio 2005 Tools for the Microsoft Office System. This " TOS " version is the Professional Edition, but all support for mobile devices is removed, replaced by special components that target the Microsoft Office suite.
- Visual Studio 2005 Team System. The crème de la crème of the Visual Studio product line is Team System. It includes features needed by development teams that work on projects together, features such as project management tools and source code control. Visual Studio 2005 Team Foundation Server, a separate product, can be installed on a shared server, and enhances the features of the Team System package.
Microsoft is pushing its new version of SQL ServerSQL Server 2005 this time around. An Express Edition is available for entry - level programmers ; a Developer's Edition is included in the Visual Studio 2005 Professional Edition and beyond. A special " Everywhere " edition targets mobile platforms. Of course, there's the complete SQL Server product available for full - scale deployments. Microsoft continues to support Microsoft Access, but it is encouraging the use of SQL Server for even small projects due to its tighter integration with. NET (starting with the 2005 release).
Beyond the database support, Visual Studio 2005 has been endowed with several new usability and feature enhancements.
- Edit and Continue. This blast from the past was in Visual Basic since version 1. 0, but it has been conspicuously absent since the first. NET release in 2002. Edit and Continue allows you to modify Visual Basic source code while actively running and debugging the application within Visual Studio, and continue running the modified application without a restart. The programmers at " M " have surely given their blood, sweat, and tears to this feature, so use it well.
- Enhanced compile - time warnings and errors. Visual Studio always flagged invalid statements in your code, but it now flags warnings on code that will compile, and may give unexpected results when executed. Figure 1 - 2 shows a warning for a declared variable that has yet to be used in code.
- ClickOnce Deployments. This new method of distributing. NET applications imposes fewer requirements on the installing user. For instance, a ClickOnce deployment does not require administrator - level security to install and use the application. Of course, some features may be disabled if the user lacks sufficient privileges.
- Code Snippets, Project and Item Templates, and Starter Kits. These features make it easier to integrate pre - written code into your new projects. The Code Snippets feature lets you save a hierarchy of short code blocks for quick insertion into your source code. They include fill - in - the - blank areas if you need them.
- Generics. Both the. NET Framework and Visual Basic include support for generics, a new feature discussed in Section, " Generics. " Generics allow you to enforce the use of specific data types on classes that would otherwise impose no such restrictions.
- Operator Overloading. Visual Basic adds new support for overloaded operators. This feature lets you assign special meanings to standard language operators, such as the addition operator (+). Instead of adding just numbers together, you develop code to add your own complex classes together ; you define what " adding " means for your class.
- My. That's right : just " My. " My is a new Visual Basic feature that provides simple and centralized access to FCL features that would normally be spread throughout that class library. You can read more about it in the very next posted blog.
Figure 1 - 2 Fair Warning
When actual syntax errors appear in code, Visual Studio now makes recommendations on how to fix them (in many cases), and will fix them for you at the click of a mouse button. In Figure 1 - 3, clicking on the " Insert the missing 'Next' " line in the Error Correction window will add in the missing " Next " keyword. If that small red circle and the black arrow to its right look familiar, that's because they're from the Smart Tags feature found in Microsoft Office products.
Figure 1 - 3 Easy error correction
Despite all of these great new features, Microsoft still refuses to implement the most requested Visual Studio feature, " Procedure AutoCompletion ," in which Visual Studio would create the entire content of a source code procedure based on your entry of its name and the use of the Control + Space key combination. Instead, they fritter away their time on other so - called productivity features. With Procedure AutoCompletion, you could write entire applications in minutes. Until that feature becomes available, you and I will have to continue writing software, crafting the quality code that users have come to expect from our fingers.
Your First VB. NET Application
To get started with Visual Basic 2005, I recommend that you jump right in and write software ! And to help you with such an assertive approach, this section gives you just what you need to test the waters of the Visual Basic pool and get comfortable with its place in the larger Visual Studio environment.
Then, you can really get your feet wet as you build Hello World — your first VB 2005 Windows Forms application ! You find out how to launch Visual Studio 2005 (the development tool for your VB applications), how to start a new project, and how to build a form visually and make it work with code.
Starting a Windows Forms project
After installing Visual Studio, you can run it by choosing Start | All Programs | Microsoft Visual Studio 2005 | Microsoft Visual Studio 2005. The environment loads, and you can get started on a program by choosing File | New | Project from the main menu. The New Project dialog box appears, as shown in Figure 1 - 4. In the Project Types pane, you find a folder structure that lists the languages loaded with your installation and the project types available for those languages. I suggest beginning with a plain old Windows Application — which is the Visual Basic 2005 answer to the traditional (and perhaps familiar) VB 6. 0 application.
Figure 1 - 4 The New Project dialog box
To get started building your Hello World application, following these steps :
- Select the project type from the Templates pane in the New Project dialog box .
For this example, select Windows Application. Also, make sure Visual Basic is the language selected in the Project Types pane. If you loaded other languages during installation, you may have other choices. - Type the name you want to give your project to replace the default name in the Name text box.
In this example, I type Hello World in the text box. - Click OK .
Visual Basic loads the default form (called Form1) and presents it to you in the Design View. The default form comes complete with a workspace, the title bar, and familiar windows elements like the resize buttons and the Close button. You do most of the work to customize your form using this visual view. - Click the word Toolbox on the left side of the screen.
The Toolbox appears, with Windows Forms controls loaded, as shown in Figure 1 - 5. - Double - click the Button control .
Visual Studio loads a button onto the default form in Design View. - On the default Form1 , click the Button control and drag it to reposition it on the form .
Figure 1 - 6 shows the result of dragging controls to the middle of the Form1 window.
Control | Property | Value |
Label | Text | &Name : |
TextBox | Name | txtName |
Button | Name | btnOK |
Text | &OK | |
Form | Text | Hello World |
Figure 1 - 5 Choosing the Button control from the Toolbox
Figure 1 - 6 Placing Label, Textbox and Button control to the form
This step list gives you the beginnings of the Windows Forms application, which you see as a Form1 in the Design View. But to see where Visual Basic comes in, you have to find the code behind the form. Visual Studio offers you (surprise !) the Code View when you’re ready to use Visual Basic to add functionality to your form.
Adding functionality to the form with VB code
To add a little functionality to the Windows form you build in the preceding section, follow these steps :
- Double - click the Button control to enter Code View .
In the Code View window, you see basic button - click code that looks like the following :Public Class Form1
Private Sub btnOK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnOK.Click
End Sub
End Class
This code is a template that wraps the code that will be run when you click the button. Visual Studio does the hard part for you, making sure the formatting of the Sub is correct ! - In the Code View window , type a line of code to change the text that appears on the Button control to Hello World .
Specifically, type the following code on the line preceding the End Sub line :Dim name As String
name = txtName.Text
If name = "" Or name.Length = 0 Then
MsgBox("Hello 'World'. Welcome to Visual Basic .NET")
Else
MsgBox("Hello '" & name & "'. Welcome to Visual Basic .NET")
End If
Your button’s code now looks like the following :
Private Sub btnOK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnOK.Click
Dim name As String
name = txtName.Text
If name = "" Or name.Length = 0 Then
MsgBox("Hello 'World'. Welcome to Visual Basic .NET")
Else
MsgBox("Hello '" & name & "'. Welcome to Visual Basic .NET")
End If
End Sub
End Class
Running and operating your Windows form
So, this experience is pretty cool, right ? Programming with Visual Basic is so easy that, you can already write a Windows Forms application. But what can you do with it ? Check out the following :
- Run your Windows Forms application within the Visual Studio environment . Press the F5 key on your keyboard, and Visual Studio opens your active project as a Windows program. It appears in your taskbar and everything. Click the button on your form, and the button text changes to “ Hello World ,” (or whatever text you specified in the code). Pretty neat, huh ? Your Windows form should look something like the image in Figure 1 - 7.
- Run your application outside of the Visual Studio environment. If you are still in Debug mode, you will need to stop your program first by using the Stop button on the toolbar or by closing the form window. Then you can save and move on. The very simple way to run an application outside of Visual Studio is as follows :
- Choose Build | Build Program Name from the main menu.
In this example, choose Build | Build HelloWorld, and Visual Studio compiles your pplication into a usable Windows program (with file extension. exe) and stores it in the default folder. - Navigate to the default folder containing your new Windows application .
For my application, the path is E :\Projects\Tutorial - VBNET\HelloWorld\HelloWorld\bin. - Double - click the filename for the compiled program to run it .
You may see a host of files in the default folder, but in the example, HelloWorld. exe is the file you’re looking for.
Figure 1 - 7 Your First VB. NET Application
There is a more complex method for running your VB programs outside the Visual Studio environment. You use a Setup Project, which is a very cool tool but beyond the scope of this blog. Research the term Setup Project in the MSDN Library when you’re ready to find out more about this device, which helps you distribute your application to other users.
More Power in Visual Studio
Earlier in this blog, I show you the Windows Forms application development environment and a little of the new Visual Basic 2005 code. If you are familiar with VB 6. 0, the form and the code look pretty familiar at this point. In fact, the major Windows development tools for any programming language work pretty much this way.
But when you look beyond the Windows form and the code structure, a few more details become evident. For instance, Visual Studio takes your VB code beyond the Windows form. The following sections give you an overview of the development power that you find in Visual Studio.
Visual Studio doesn’t just do Windows !
The first evident change that sets Visual Studio apart as a development tool is this : You can use Visual Studio to write programs that run on the World Wide Web as well as on Windows computers. When you click the File menu to add a new project, notice the second option in the menu. As shown in Figure 1 - 7, the second project option is a new Web Site.
Choose this option to create a Web application, which incorporates a whole host of technologies — the. NET Framework, ASP. NET, Visual Basic, and HTML — that each have essential roles for enabling an application to run online.
Figure 1 - 8 The File menu in Visual Studio
Visual Basic goes mobile
Mobile computing devices make their move into Visual Basic 2005. Two project types that run on such devices are built right into Visual Studio. Windows CE, Pocket PC 2003, and SmartPhone platforms are all represented.
I don’t give examples of these specific project types in this blog because you can create a mobile device application in the same manner you create a Windows Forms application (like the Hello World program discussed earlier in the blog). You should know that getting familiar with the Visual Basic language as presented in this blog puts you on the right track for creating applications for a Pocket PC. Mobile computing applications require some special programming practices, so make sure to grab some device - specific information when you work on those project types.
Writing routines to use with other software is easier with Visual Basic 2005. You can write add - ins for Microsoft Office apps, including Excel and Word templates with VB code running behind them. These routines don’t use the VBScript that you may have seen before ; a completely new part of Office 2003 allows you to write templates with special, built - in functionality. For example, I’ve built a Word template that automates a reporting process by asking the user for a report number, checking that number against a database of all the reports filed, and filling out part of the document - in - process with the relevant information from the database.
VB as your one - stop development shop
Generally, Visual Studio and the. NET Framework are designed to be the onestop shop for any kind of development on Windows machines. But in this version, Visual Basic 2005 can also do it all. The language can now touch all of the parts of the. NET Framework that any of the other languages can get to, without resorting to the cryptic function calls necessary in prior versions of VB.
The new features covered in this blog include :
- Security : Encryption, validation, and permissions. Securing your code using sophisticated encryption is now built in to the framework, among other things.
- Data : Collections of information, accessing databases, and XML. There are new Data controls for your forms pages, too !
- IO : Integrate program activities with files, disks, and folders in a way that requires writing much less code.
- System. Net : VB knows about the Internet. Web, FTP, and Mail are all in one place.
- Drawing : Comprehensive screen graphics for Web and Windows — even 3D.
- The My Object : Get to the hard - to - reach places more easily with this simple - to - use set of shortcuts.
This list shows you that Visual Basic has grown up somewhat. If you don’t know VB 6, then you have no worries ! Getting chummy with this version (Visual Basic 2005) is a much better place to be. If you do know VB 6, then welcome home. This is where you always wanted to be.