VB.net Tips-III
Posted On December 27, 2007 by Sneha Philipose filed under
Operator Shortcuts in VB.NET
I have noticed one cool tip in vb.net is operator shortcuts. As a Visual Basic 6.0 programmer I always used to code like this:
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1= Str1 & Str2
'Str1 is now equal to "Total One Thousand Rupees"
Till for some months I use to continue like this in VB.NET also. But now VB.NET also supports the same operator shortcuts the Java and C++ guys have always had. So as an alternative you can use this Way.
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1&= Str2
'Str1 is now equal to "Total One Thousand Rupees"
You can similarly use +=, -=, *=, /=, \=, etc.
This Tip will explain you how to disable the close button or X button in the current form at runtime.
Public Class Form1
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
Public Const SC_CLOSE = &HF060&
Public Const MF_BYCOMMAND = &H0&
Function RemoveXButton(ByVal iHWND As Integer) As Integer
Dim main_menu As Integer
main_menu = GetSystemMenu(iHWND, False)
Return RemoveMenu(main_menu, SC_CLOSE, MF_BYCOMMAND)
End Function
'Disable the button on the current form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘call the RemoveXButton from the Formlode
RemoveXButton(Me.Handle().ToInt32())
End Sub
End Class
List All Logical Drives in VB.NET
This tip will explain how to get the computer's logical drives from the local system.
The program uses the Directory.GetLogicalDrives method to retrieves the names of the logical drives on this computer in the form "<drive letter>:\". All the drive letters will displayed them in a ListBox.
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
' List the drives.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each drive As String In Directory.GetLogicalDrives()
lstDrives.Items.Add(drive)
Next drive
End Sub
End Class
Get Free and Total Disk Space (VB.NET)
Uses PInvoke (Platform Invocation) to call the the GetDiskFreeSpaceEx API function to get free and total space, in megabytes, on the given drive.
GetDiskFreeSpaceEx function obtains the following information about the amount of space available on a disk volume: the total amount of space, the total amount of free space, and the amount of free space available to the user associated with the calling thread.
Public Class Form1
Private Declare Function GetDiskFreeSpaceEx _
Lib "kernel32" _
Alias "GetDiskFreeSpaceExA" _
(ByVal lpDirectoryName As String, _
ByRef lpFreeBytesAvailableToCaller As Long, _
ByRef lpTotalNumberOfBytes As Long, _
ByRef lpTotalNumberOfFreeBytes As Long) As Long
Public Function GetFreeSpace(ByVal Drive As String) As Long
'returns free space in MB, formatted to two decimal places
'e.g., msgbox("Free Space on C: "& GetFreeSpace("C:\") & "MB")
Dim lBytesTotal, lFreeBytes, lFreeBytesAvailable As Long
Dim iAns As Long
iAns = GetDiskFreeSpaceEx(Drive, lFreeBytesAvailable, _
lBytesTotal, lFreeBytes)
If ians > 0 Then
Return BytesToMegabytes(lFreeBytes)
Else
Throw New Exception("Invalid or unreadable drive")
End If
End Function
Public Function GetTotalSpace(ByVal Drive As String) As String
'returns total space in MB, formatted to two decimal places
'e.g., msgbox("Free Space on C: "& GetTotalSpace("C:\") & "MB")
Dim lBytesTotal, lFreeBytes, lFreeBytesAvailable As Long
Dim iAns As Long
iAns = GetDiskFreeSpaceEx(Drive, lFreeBytesAvailable, _
lBytesTotal, lFreeBytes)
If iAns > 0 Then
Return BytesToMegabytes(lBytesTotal)
Else
Throw New Exception("Invalid or unreadable drive")
End If
End Function
Private Function BytesToMegabytes(ByVal Bytes As Long) _
As Long
Dim dblAns As Double
dblAns = (Bytes / 1024) / 1024
BytesToMegabytes = Format(dblAns, "###,###,##0.00")
MsgBox(BytesToMegabytes)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Free Space on D: " & GetTotalSpace("D:\") & "MB")
End Sub
End Class
Create and Delete Directories and Subdirectories in VB.NET
CreateDirectory("C:\MyDir")
CreateSubDirectory("C:\MyDir", "Test")
DeleteDirectory("C:\MyDir")
Imports System.IO
Public Function CreateDirectory(ByVal FullPath As String) _
As Boolean
Dim objDI As New DirectoryInfo(FullPath)
Try
objDI.Create()
Return True
Catch
Return False
End Try
End Function
Public Function CreateSubDirectory(ByVal ParentPath As _
String, ByVal NewSubDirectory As String) As Boolean
Dim objDI As New DirectoryInfo(ParentPath)
Try
objDI.CreateSubdirectory(NewSubDirectory)
Return True
Catch
Return False
End Try
End Function
Public Function DeleteDirectory(ByVal FullPath As String) _
As Boolean
Dim objDI As New DirectoryInfo(FullPath)
Try
objDI.Delete(True)
Return True
Catch
Return False
End Try
End Function
Operator Shortcuts in VB.NET
I have noticed one cool tip in VB.NET is operator shortcuts. As a Visual Basic 6.0 programmer I always used to code like this:
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1= Str1 & Str2
'Str1 is now equal to "Total One Thousand Rupees"
Till for some months I use to continue like this in VB.NET also. But now VB.NET also supports the same operator shortcuts the Java and C++ guys have always had. So as an alternative you can use this Way.
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1&= Str2
'Str1 is now equal to "Total One Thousand Rupees"
You can similarly use +=, -=, *=, /=, \=, etc.
Return Statement in VB.NET
This is the simple tip. Under VB 6.0, you returned a value from a function by implicitly using a variable of the same name as the function, as shown in this example:
Public Function sample() As Boolean
'Code here
Sample = True
End Function
We can avoid using one more variable in VB.NET. Though you can still use this old syntax, the preferred method is to use the Return statement, like this:
Public Function sample() As Boolean
'Code here
Return True
End Function
I have noticed one cool tip in vb.net is operator shortcuts. As a Visual Basic 6.0 programmer I always used to code like this:
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1= Str1 & Str2
'Str1 is now equal to "Total One Thousand Rupees"
Till for some months I use to continue like this in VB.NET also. But now VB.NET also supports the same operator shortcuts the Java and C++ guys have always had. So as an alternative you can use this Way.
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1&= Str2
'Str1 is now equal to "Total One Thousand Rupees"
You can similarly use +=, -=, *=, /=, \=, etc.
This Tip will explain you how to disable the close button or X button in the current form at runtime.
Public Class Form1
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
Public Const SC_CLOSE = &HF060&
Public Const MF_BYCOMMAND = &H0&
Function RemoveXButton(ByVal iHWND As Integer) As Integer
Dim main_menu As Integer
main_menu = GetSystemMenu(iHWND, False)
Return RemoveMenu(main_menu, SC_CLOSE, MF_BYCOMMAND)
End Function
'Disable the button on the current form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘call the RemoveXButton from the Formlode
RemoveXButton(Me.Handle().ToInt32())
End Sub
End Class
List All Logical Drives in VB.NET
This tip will explain how to get the computer's logical drives from the local system.
The program uses the Directory.GetLogicalDrives method to retrieves the names of the logical drives on this computer in the form "<drive letter>:\". All the drive letters will displayed them in a ListBox.
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
' List the drives.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each drive As String In Directory.GetLogicalDrives()
lstDrives.Items.Add(drive)
Next drive
End Sub
End Class
Get Free and Total Disk Space (VB.NET)
Uses PInvoke (Platform Invocation) to call the the GetDiskFreeSpaceEx API function to get free and total space, in megabytes, on the given drive.
GetDiskFreeSpaceEx function obtains the following information about the amount of space available on a disk volume: the total amount of space, the total amount of free space, and the amount of free space available to the user associated with the calling thread.
Public Class Form1
Private Declare Function GetDiskFreeSpaceEx _
Lib "kernel32" _
Alias "GetDiskFreeSpaceExA" _
(ByVal lpDirectoryName As String, _
ByRef lpFreeBytesAvailableToCaller As Long, _
ByRef lpTotalNumberOfBytes As Long, _
ByRef lpTotalNumberOfFreeBytes As Long) As Long
Public Function GetFreeSpace(ByVal Drive As String) As Long
'returns free space in MB, formatted to two decimal places
'e.g., msgbox("Free Space on C: "& GetFreeSpace("C:\") & "MB")
Dim lBytesTotal, lFreeBytes, lFreeBytesAvailable As Long
Dim iAns As Long
iAns = GetDiskFreeSpaceEx(Drive, lFreeBytesAvailable, _
lBytesTotal, lFreeBytes)
If ians > 0 Then
Return BytesToMegabytes(lFreeBytes)
Else
Throw New Exception("Invalid or unreadable drive")
End If
End Function
Public Function GetTotalSpace(ByVal Drive As String) As String
'returns total space in MB, formatted to two decimal places
'e.g., msgbox("Free Space on C: "& GetTotalSpace("C:\") & "MB")
Dim lBytesTotal, lFreeBytes, lFreeBytesAvailable As Long
Dim iAns As Long
iAns = GetDiskFreeSpaceEx(Drive, lFreeBytesAvailable, _
lBytesTotal, lFreeBytes)
If iAns > 0 Then
Return BytesToMegabytes(lBytesTotal)
Else
Throw New Exception("Invalid or unreadable drive")
End If
End Function
Private Function BytesToMegabytes(ByVal Bytes As Long) _
As Long
Dim dblAns As Double
dblAns = (Bytes / 1024) / 1024
BytesToMegabytes = Format(dblAns, "###,###,##0.00")
MsgBox(BytesToMegabytes)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Free Space on D: " & GetTotalSpace("D:\") & "MB")
End Sub
End Class
Create and Delete Directories and Subdirectories in VB.NET
CreateDirectory("C:\MyDir")
CreateSubDirectory("C:\MyDir", "Test")
DeleteDirectory("C:\MyDir")
Imports System.IO
Public Function CreateDirectory(ByVal FullPath As String) _
As Boolean
Dim objDI As New DirectoryInfo(FullPath)
Try
objDI.Create()
Return True
Catch
Return False
End Try
End Function
Public Function CreateSubDirectory(ByVal ParentPath As _
String, ByVal NewSubDirectory As String) As Boolean
Dim objDI As New DirectoryInfo(ParentPath)
Try
objDI.CreateSubdirectory(NewSubDirectory)
Return True
Catch
Return False
End Try
End Function
Public Function DeleteDirectory(ByVal FullPath As String) _
As Boolean
Dim objDI As New DirectoryInfo(FullPath)
Try
objDI.Delete(True)
Return True
Catch
Return False
End Try
End Function
Operator Shortcuts in VB.NET
I have noticed one cool tip in VB.NET is operator shortcuts. As a Visual Basic 6.0 programmer I always used to code like this:
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1= Str1 & Str2
'Str1 is now equal to "Total One Thousand Rupees"
Till for some months I use to continue like this in VB.NET also. But now VB.NET also supports the same operator shortcuts the Java and C++ guys have always had. So as an alternative you can use this Way.
Str1 = “Total”
Str2=”One Thousand Rupees”
Str1&= Str2
'Str1 is now equal to "Total One Thousand Rupees"
You can similarly use +=, -=, *=, /=, \=, etc.
Return Statement in VB.NET
This is the simple tip. Under VB 6.0, you returned a value from a function by implicitly using a variable of the same name as the function, as shown in this example:
Public Function sample() As Boolean
'Code here
Sample = True
End Function
We can avoid using one more variable in VB.NET. Though you can still use this old syntax, the preferred method is to use the Return statement, like this:
Public Function sample() As Boolean
'Code here
Return True
End Function
