VB.NET has been introduced with slight changes in all the controls and objects that were used by the developers in VB. This article doesn’t discuss all object changes from VB 6 to VB.NET but it focuses on two objects: ‘App’ and ‘Clipboard’ which were very important in VB 6 and gave the ability to the users to use these objects without even initialization. These two objects still exist in VB.NET but with slight changes.
The 'App' object
The ‘App’ object in VB was used for many purposes like:
- Getting the current executable file name and path
- Retrieving the current executable file’s version
- Getting the application’s Title
- Checking whether the executable file is having a previous instance already running or not
There were various other functions that were performed by this object but these were the main tasks and thus, we are going to to discuss their implementation in VB.NET.
The ‘App’ object doesn’t exist in VB with this name. One can judge that ‘App’ stands for ‘Application’ and that’s what Microsoft must have thought and that’s why, they removed ‘App’ and introduced it as ‘Application’ object in VB.NET. But this object doesn’t solve all the above four problems. You will have to use some other objects to fulfill some of the above tasks. Let’s see below:
Getting executable file path
In VB6, we used to get an executable file path as:
App.Path
But in VB.NET, this task is fulfilled as:
Application.StartupPath
Getting current executable file name
To get the current executable’s file name, we can’t use the ‘Application’ object. For this, we will have to use the ‘Diagnostics’ object. This object has a number of functions to analyze the processes that are running at a single time on Windows. To retrieve the current executable file name, we have to write the code as follows:
Diagnostics.Process.GetCurrentProcess.ProcessName
In VB6, we used to do it as:
App.EXEName
Retrieving the executable file's version
Sometimes, you might be interested to know which file version you are using. In such cases, you need to retrieve the file version of the current executable file. In VB6, the code used to retrieve the executable version was divided into three commands as follows:
App.Major
(used to generate the major number in the version, for e.g. Major is ‘3’ in version ‘3.50.15’
App.Minor
(used to generate the minor number in the version, for e.g. Minor is ’50’ in version ‘3.50.15’
App.Revision
(used to generate the revision number in the version, for e.g. Revision is ’15’ in version ‘3.50.15’
But in VB.NET, the version can be retrieved by just a single line of code as :
Application.ProductVersion
Getting current application's title
In VB6, the application’s title was known as:
App.Title
But in VB.NET, the title is known as:
Application.ProductName
Checking whether the executable file is having a previous instance already running or not
It is possible that you may not want your application to have multiple instances in order to prevent it’s multi-user capability. In such cases, you need to check whether the application is already running or not at the time of project’s startup. For that, VB6 had a single line of code as:
If App.PrevInstance Then End
But in VB.NET, there is no such provision. As I mentioned above, the ‘Diagnostics’ object has a number of functions to analyze the processes that are running at a single time on Windows. So, you will have to use this object to detect any active previous instances of the current executable as follows:
If UBound (Diagnostics.Process.GetProcessesByName (Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then End
Basically, it checks how many current processes are running in the memory. If the number is greater than 0, it ends the application and prevents it from having multiple instances.
The 'Clipboard' object
The ‘Clipboard’ object is very important in VB and VB.NET. This object enables the users to share the data with the windows clipboard. It is now a requirement in all the applications to provide support for sharing data through windows clipboard. Below are the examples of how to share the data through the windows clipboard.
Copying data to the clipboard
If you want to copy the data from your VB.NET application to the windows clipboard, you will have to write the code as follows:
Dim data As String
Dim DataObj as DataObject
DataObj = New DataObject()
data = "Sumit Mehta"
DataObj.SetData(DataFormats.Text,data)
Clipboard.SetDataObject(DataObj)
In VB.NET, there is a new object called the ‘DataObject’. This object is the placeholder for the data that you want to put on the clipboard. This object’s ‘SetData’ function is used to set the source data and it takes two parameters: First one specifies the format in which you want to place the data on the clipboard.
VB.NET also provides a class ‘DataFormats’ which provides various formats to be used in data loading and saving. In the above example, we are copying a text string to the clipboard, that’s why we have used ‘DataFormats.Text’ format so that the data is placed in a simple text form. For instance, if you want to place the picture on the clipboard, then you will use ‘DataFormats.Bitmap’ format. The second parameter is the data, which is a string named ‘data’ in our example. Then you will call the ‘SetDataObject’ function of the ‘Clipboard’ object which takes a ‘DataObject’ as a parameter and places it on the windows clipboard.
Retrieving data from the clipboard
Once you have copied the data from the VB.NET application to the windows clipboard, you may also need this data in your application a little later. So in that case, you will have to write the following code to retrieve the data from the windows clipboard:
Dim data As String
Dim dataObj As DataObject
dataObj=Clipboard.GetDataObject
If dataObj.GetDataPresent(DataFormats.Text) Then
data=dataObj.GetData(DataFormats.Text).ToString
End If
For copying the data to the clipboard, we used the ‘Clipboard’ object’s ‘SetDataObject’ function. For retrieving the data, we will use the ‘GetDataObject’ function which will return a ‘DataObject’ placed on the clipboard. Then we will use the ‘GetData’ function of the ‘DataObject’ to actually retrieve the data. But before actually loading the data, we must check whether the data is in the required format or not.
Imagine that you have a picture in the ‘DataObject’ and you use it’s ‘GetData’ function to place it’s data on a Textbox in VB.NET application. It is definite that the errors will occur as you can’t place a picture in a Textbox. So you will have to use the ‘GetDataPresent’ function of the ‘DataObject’ which verifies the data format in the ‘DataObject’. It takes one parameter as a ‘DataFormat’ to which you want to compare the data (DataFormat.Text) in our case. If it finds the data in the correct format, it loads the data through the ‘GetData’ function of the ‘Dataobject’.