Visual Basic.NET





Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to  Visual Basic pre-2002, it is not the same technology,

Share tech news, updates, or what's on your mind.

Sign up to Post


Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares.
The application is written in VB.NET targeting the .NET framework 2.0.
The source code and the compiled programs have been included in the article.


Enter a computername, Share Name, Share Path and Share Description if needed

You can get a list of shares by pressing the Show button
You can Create a share if the necessary information has been filled in by pressing Create
You can Remove a share by selecting the share and pressing Remove
You can Open the share by dubbleclicking on the share listed in the listbox


This tool has been made to facilitate creating shares on remote machines.
Many times it has been very handy, a user wants some files on this machine.
You simply create a share on this machine, browse to the users desktop and place the files
right under their nose...  

Using the code

The code is pretty straight forward in using, it exists in 2 versions.
A GUI version and a Console version.

The Main functions are self-explanatory, one for create a share, one for removing a share and of course a function that gives you a list of the available shares on that machine.
You must an administrator on the remote computer for the connection to be made through WMI.

For the moment no impersonation is implemented.


Remote Share GUI
Remote Share In Actions
Remote Share Console

Main Functions
LVL 96

Expert Comment

by:Bob Learned
Great article!!  You might want to think about looking at the System.Management namespace, and the classes, such as the ManagementObjectSearcher, ManagementObject, etc.

Author Comment

Yes that would indeed be an other option, this was actually an vbscript I adapted for VB.NET.

But that might be a good follow up article...
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the kill process portion with a msgbox stating that excel was detected and it should be closed before proceeding as a precaution statement.

I've seen many examples and discussions on how to properly exit and dispose of excel objects. Believe me, I tried them all. Some examples worked once, while others had no effect. I needed something to consistently dispose of the excel.exe trail in memory.

Since the ultimate effect of killing the process is not desirable, I did create a IsExcelOpen() function, which is basically the same function of the CloseAllResidentExcels function except the prc.kill() process is eliminated and instead displays a warning message to the end user to please save and close all currently open spreadsheets before continuing. I was fully prepared for the hiss and moaning to ensue shortly after deploying the update — but surprisingly everyone had no issue with it.  

Many of the examples for closing an excel spreadsheet probably do work in the simple most case scenarios. But now I'm starting to wonder if the datagridview1 object has to be closed as well before quitting the excel application for these scenarios to work. I haven't had to much to time to find out why it's not closing properly. Everyone …
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String Literal, only instead of starting and ending with with a quote character ("), it begins with (<) and ends with (>).  You can use regular XML tags right there in the code!


XML Literals allow you to use XML syntax in your code.  It’s easy to work with XML files and XML fragments this way, since you have the tags in the code, but it’s also quicker to access information rather than the traditional methods using XmlDocument and XmlElement.

It’s available in the namespace System.Xml.Linq, since the .NET Framework 3.5/Visual Studio 2008 (for Visual Basic Only) it supports most of the Extensible Markup Language (XML) 1.0 specification, and together with Lambda Expressions and/or LINQ, gives you a better experience with XML files.  It’s also recognized by the intellisense system, making it very convenient to use.  It even does the indenting automatically.

Topics in this article:

XML Literals
Read Information
List Information
Embedded Expressions
Modify Nodes
Inserting Nodes
Deleting Nodes
Finally Example (web)

XML Literals

NOTE: Most of the examples use Option Infer On but you can declare the correct variables data type.

The basic concept looks like this:

Open in new window

LVL 25

Expert Comment

Great article, thanks for the info!

Expert Comment

excellent article
You've probably seen a table that allows the user to start typing and moves immediately to the row whose key field starts with the characters you've typed. A ComboBox does a very rudimentary form of this: you type the first character, and it moves to the first match. But a progressive search is more powerful; as you type, say, S, M, I, T, with a list of names, it'll go first (in my local phone directory) to Saab, then to Smale, then to Smialkowski, then to Smith. The full list of names is still displayed, so the user can go up or down from wherever he lands with the search (unlike versions of progressive searches that filter the rows). You can undo a letter by pressing Backspace.

While I've seen progressive searches in other languages that will highlight the letters entered in the list itself, there doesn't seem to be an easy way to highlight portions of text inside a DGV cell. So instead, this routine displays the text it's searching for in a separate label that is displayed elsewhere on the form (I put mine just above the key field of the DGV). One complication is that many punctuation characters must be enclosed in brackets, or they'll be considered to have special meanings, not to be characters to search for. The label display won't be necessarily the same as the internal search string.

This requires the DGV to use a DataSource that has the .Select method, such as a DataTable. Note that the search is case-insensitive, which is inherent in the use of LIKE in the …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray.

The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Run() instead of a Form instance.  With this approach we automatically get a built-in message pump and do not see any ugly Console windows.  Having a message pump means that an infinite loop is not necessary to keep the app "alive".  It also means that we can freely create Controls and Forms at run-time and respond to their events as usual.  We can even elegantly multi-thread the application by using a BackgroundWorker() Control from within the Inherited ApplicationContext Class.

Why would you want to do this?  Perhaps you have a "background" operation that needs to run all the time and would like to give the user a way to interact with the application.  The tray icon gives you the option to display menus or simply popup a configuration dialog if clicked.  This is an easy approach compared to implementing a full blown service.

The ApplicationContext provides complete freedom in controlling when the program is closed.  You are no longer constrained to just the two shutdown mode choices of "When startup form closes" or "When last form closes".  With no "startup form" you can now freely switch between forms without worrying about the app closing on you.  You can display a Login form and then actually …
LVL 15

Expert Comment

Thanks, very useful.

Great Article...

Expert Comment

Hi guys

Thanks for all your comments unfortuntately all seem to have a problem in that if "Enable application framework" is unchecked then the Splash Screen I have added is not called and displayed.

Any ideas?

First there was PAFSO – Programmers Against the File System Object
Then there was PAUES – Programmers Against the Use of the End Statement

Now Experts Exchange brings you PAEVISS - Programmers Against Embedded Values In SQL Statements

With ADO.NET there is sometimes a need to insert, update or query the database using a command object.
Using Connection As SqlConnection = New SqlConnection(ConnectionString)
	Dim CmdText As String = "INSERT INTO [Person] (FirstName, LastName) " & _
                "VALUES ('" & txtFirstname.Text & "', '" & txtLastname.Text & ")"

	Using cmd As SqlCommand = New SqlCommand(CmdText, Connection)
	End Using
End Using

Open in new window

While code like this will work sometimes, it isn’t ideal for a number of reasons:

If either of the textbox values contains an apostrophe, then it requires escaping by doubling up.
By entering SQL statements in the textboxes, a user could perform a SQL injection attack.
Values such as numbers and dates will be coerced into strings, which are subject to culture rules, which can cause problems.  For example, there is always much confusion over which date string format is used when sending dates to SQL server using formatted strings.  The US format of MM/dd/yyyy will often cause confusion in environments expecting a little endian date format (dd/MM/yyyy.)
Instead, let's look at how you can use parameterized queries.

Open in new window

LVL 38

Author Comment

Thanks Dan, I think you did a good job with the edits.
LVL 15

Expert Comment

Thanks, Now, I have a reference in which I can prove to my boss why I do not want to use embedded values in SQL statement.
Hello Experts Exchange Users!

This is my first tutorial, so I hope that you like it!

Goal : Create a program to get the keys entered into the form
Programming Language : Visual Basic.NET 2008 SP1

Step 1
  The first thing that we must do to reach our goal is create a new project. This can be done by clicking File > New Project or by pressing Control and N on your keyboard.

Step 2
  Name the project "Capturing Keys in VB.NET" or anything else that you want.

 Figure 1 - Name the projectStep 3
  Customize the form in anyway that you want.

  Figure 2 - Customize the formStep 4
  Add two labels and name them "lblKeyCode" and "lblKeyValue".

  Figure 3 - Add two labelsStep 5
  Select the form, enter the list of handlers and double-click the "KeyDown" handler.

  Figure 4 - Create a "KeyDown" handlerStep 6
  In the "KeyDown" handler, you must put the following code:
lblKeyCode.Text = e.KeyCode.ToString

Open in new window

lblKeyValue.Text = e.KeyValue.ToString

Open in new window

Explanation for Step 6
  The code "lblKeyCode.Text = e.KeyCode.ToString" will set the first label to the internal key name. (eg. "A" or "Delete")
  The code "lblKeyValue.text = e.KeyValue.ToString" will set the second label to the value of the key (eg. "19" or "49")

Step 7
  Press the debug button and press any key on your keyboard
  The value of the labels should change, depending on which key you press

- Daniel Sage

Expert Comment

Does this work even if the program is running in the background?
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.  


Form with textboxes and watermarks

1. Create VB class

Create a new VB class in your solution and name it WaterMarkTextBox.vb

2. Copy VB code to new class

Copy the code from the snippet below to your new class
Option Strict On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class WaterMarkTextBox
    Inherits TextBox
    Private oldFont As Font = Nothing
    Private waterMarkTextEnabled As Boolean = False
#Region "Attributes"
    Private _waterMarkColor As Color = Drawing.Color.Gray
    Public Property WaterMarkColor() As Color
            Return _waterMarkColor
        End Get
        Set(ByVal value As Color)
            _waterMarkColor = value
        End Set
    End Property
    Private _waterMarkText As String = "Water Mark"
    Public Property WaterMarkText() As String
            Return _waterMarkText
        End Get
        Set(ByVal value As String)
            _waterMarkText = value
        End Set
    End Property
#End Region
    ' Default constructor
    Public Sub New()
    End Sub
    Private Sub JoinEvents(ByVal join As Boolean)
        If join Then

Open in new window


Expert Comment

by:ram prasad chakraborty
It really works well.

Expert Comment

by:Dewang Desai
Great Stuff.
I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps.

Because there are a lot of programmers that are going to be making this move, I thought I would share this information.  I'm certain it will be very valuable to those who are faced with this task.

One of the first things to think about is whether you want to upgrade your VB6 program to a VB.NET 2008 Windows Forms application or a VB.NET 2008 Windows Presentation Foundation (WPF) application.  If you are upgrading to VB.NET 2005, then you don't have the WPF option, but if you are upgrading to VB.NET 2008, then you do, and you may choose to use it.  

Why would you want to upgrade a VB6 Windows Forms application to WPF?  Because that is the future of Windows programming.  Windows Forms have been using the same technology for more than 15 years, and are now at the very beginning of their sunset.  Windows Forms applications use User32 (User before 32-bit) and GDI/GDI+ for gui rendering, but WPF applications use DirectX for gui rendering, which is much more powerful.  Additionally, WPF uses Extensible Application Markup Language (XAML) to define the layout of the interface, and instantiate .NET objects.

By far, the easiest choice is to upgrade a VB6 Windows Forms application to a VB.NET 2008 Windows Forms application. Since Windows Forms are going…

Expert Comment

Hi VBRocks,

your upgrade article is very helpful, but I have doubts on one point. You are saying that the VB6 string funtions like InStr and Mid do not work fast in VB.NET. But I have found other articles in the Internet claiming that only the first call of the VB6 functions is slow while the following calls are (almost) as fast as the VB.NET functions. Can you confirm this, or did you get different results when you compared the functions?


Expert Comment

I have noticed that when compared with Windows Forms in VB.NET (e.g., Visual Studio 2010) a lot of features are not available for WPF, and many users have said forget going down that path.  If you come into the VB.NET space with significant WPF abilities, then perhaps it's another story.  

There may also be many Windows Form issues in what you are converting from VB6 to VB.NET, so it may be better to stick with the Windows Form approach.  

As a specific example, developing a workflow GUI for task handling in which button, pictures can be connected with lines, moved, and right-clicked for command menus can be accomplished more straightforwardly using the ShapeContainer package tools in Windows Forms.   So, when the WPF seems like the logical choice for creating a GUI application for workflow and tasking, most experts have claimed that there are too many limitations with WPF.
Using "atoms" is an old and good technique than I started to use a long time ago (probably, with vb4 or vb5...). It's an excellent way to inter-communicate separate processes without the need to write or read physical files on disk.  You can learn a lot about atoms, of course, in MSDN:

An atom table is a system-defined table that holds a series of strings and, for each one of them, an associated 16-bit integer (in, Short or Int16 data type). So, think in them as a collection of strings, each string with a unique integer ID. Each application can create a local atom table, but there is also a global atom table that can be accesed system-wide from within any application, so inter-process communication is so easy this way.  This means that one application can create values in this global table and other application in a complete separate execution process can read them (or delete them).

One typical scenario of this approach is to communicate from one process to another, for example, when a job has finished, writing a value in this global table. The second process keeps monitoring the global atom table and, when it finds the specific value, it knows that the job is done.

The best of all is that writing, reading and deleting values in the global atom table is extremely easy!!  With the use of only 4 Win32 APIs we'll do all the work.
LVL 49

Expert Comment

Question: If using atoms in a VB.Net application, does that part of the application go out into unmanaged code since it directly calls a Windows API?
LVL 25

Author Comment

by:Luis Pérez
Mmmm... it's a peculiar question. Well, Windows APIs are in fact unmanaged code from .NET's point of view, but here we are encapsulating those functionality into .NET classes (that, in fact, is what the entire Framework does). Really I don't know what is the right answer here. I think it is a matter for purists.

Visual Basic.NET





Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to  Visual Basic pre-2002, it is not the same technology,