Pages

Wednesday, 11 July 2012

Google Search Using VB.NET

This tutorial is going to tech you how to make simple Google Search Application using VB.NET. In this tutorial we are using Timer to control the Google Search appropriate manner. We are also using VB.NET WebBrowser control to navigate and search, so before trying this code you have to add WebBrowser control to your form from Toolbar shown in figure given below:

Add WebBrowser



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Timer1.Interval = 500
        Timer1.Enabled = True
        Timer1.Start()
        WebBrowser1.Navigate("http://www.google.com/")

End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
            Timer1.Stop()
            WebBrowser1.Document.GetElementById("q").SetAttribute("value", "VB.NET Tutorials")
            WebBrowser1.Document.GetElementById("btnk").InvokeMember("click")
        End If

End Sub

Monday, 9 July 2012

VB.NET How To Copy Move and Delete File

This tutorial is going to teach you how to Copy, Move and Delete a file from one location to another. For this we use System.IO.File property Copy, Move and Delete. This is very useful to interact with files stored in Hard Disk. In this example, for Copy and Move take input as specific location where you file stored like "D:\Test.txt" and destination location where we want to Paste or Move file like "D:\NewText.txt". In case of Delete only specific only where the file located to delete.

Public Sub FileCopy()

        Dim CopyFrom As String = "D:\Test.txt"
        Dim CopyTo As String = "D:\NewText.txt"

        If System.IO.File.Exists(CopyFrom) = True Then
            System.IO.File.Copy(CopyFrom, CopyTo)
            MsgBox("File Copied!")
        End If

End Sub


Public Sub FileMove()

        Dim MoveFrom As String = "D:\Test.txt"
        Dim MoveTo As String = "E:\Test.txt"

        If System.IO.File.Exists(MoveFrom) = True Then
            System.IO.File.Move(MoveFrom, MoveTo)
            MsgBox("File Moved!")
        End If

End Sub


Public Sub FileDelete()

        Dim DeleteFrom As String = "E:\Test.txt"

        If System.IO.File.Exists(DeleteFrom) = True Then
            System.IO.File.Delete(DeleteFrom)
            MsgBox("File Deleted!")
        End If

End Sub

Sunday, 8 July 2012

VB.NET Filter OpenFileDialog

This tutorial is going to teach you to code you a Filtered OpenFileDialog in VB.NET. Sometime we need to restrict user to choose limited File Format. To do this we have to use a Filter OpenFileDialoag. In example given below we Filtered OpenFileDialog with two Format Text and Microsoft Word files using Filter property of OpenFileDialog. Before to use the code given below you have to open your Toolbox, locate the control called "OpenFileDialog". Double click on it to add it to your project.

Double Click on it
Double Click on it


Public Sub myOpenFileDialog()

        OpenFileDialog1.InitialDirectory = "D:\"
        OpenFileDialog1.Title = "Open a Text File"
        OpenFileDialog1.Filter = "Text Files|*.txt"
        OpenFileDialog1.Filter = "Text Files|*.txt|Word Files|*.doc"
        OpenFileDialog1.ShowDialog()

End Sub

Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        Dim filename As String = OpenFileDialog1.FileName
        If filename = "" Then
            MsgBox("Clicked on Cancel Button")
        Else
            filename = OpenFileDialog1.FileName
            MsgBox(filename)
        End If

End Sub

Vb.net Read and Write Text File

This tutorial is going to teach to how to read and writing data from a Text file. We use StreamReader to read and StreamWriter to write date on Text file. In example given below, we using TextBox1.Text to get the full address of the Text file. So, Before writing this code you have to insert TextBox in your form.


Public Sub Readwrite()

        If System.IO.File.Exists(TextBox1.Text) = True Then
            Dim myobjreader As New System.IO.StreamReader(TextBox1.Text)
            Dim str As String = myobjreader.ReadToEnd
            MsgBox(str)
            myobjreader.Close()

            Dim myobjwriter As New System.IO.StreamWriter(TextBox1.Text)
            myobjwriter.Write(str & "This file is write using VB.NET.")
            myobjwriter.Close()
        Else
            MsgBox("File Does Not Exist !")
        End If

End Sub

Thursday, 5 July 2012

Computer Information using VB.NET

This Tutorial is going to teach you how to get information about your computer. Its a simple but very useful application to know and use our computer information in specified steps. For example we can get Computer Name, Username, OS Name, Memory, Screen Resolution of your system are shown given below:-

Public Sub computer_info()

        MsgBox("Name of My Computer= " & My.Computer.Name)
        MsgBox("Username of My Computer= " & My.User.Name)
        MsgBox("Name of Operationg System of My Computer= " & My.Computer.Info.OSFullName)
        MsgBox("Available Physical Memory of My Computer= " & My.Computer.Info.AvailablePhysicalMemory)
        MsgBox("Avaialable Virtual Memory of My Computer= " & My.Computer.Info.AvailableVirtualMemory)
        MsgBox("Total Physical Memory of My Computer= " & My.Computer.Info.TotalPhysicalMemory)
        MsgBox("Total Virtual Memory of My Computer= " & My.Computer.Info.TotalVirtualMemory)
        MsgBox("Caps Lock ON of My Computer= " & My.Computer.Keyboard.CapsLock)
        MsgBox("Wheel exists of mouse of My Computer= " & My.Computer.Mouse.WheelExists)
        MsgBox("Screen Resolution of My Computer= " & My.Computer.Screen.WorkingArea.ToString)
        MsgBox("Screen Bits Per Pixel of My Computer= " & My.Computer.Screen.BitsPerPixel)

End Sub

VB.NET Text to Speak application

This tutorial is going to teach you change the text form into voice in VB.net. This is very useful VB.NET application to make a VB.NET program more GUI by using voice property. We have to just write text and it will talk to user. It can also be quite entertaining!


Public Sub speak_now()

        Dim MySAPI
        MySAPI = CreateObject("SAPI.spvoice")
        MySAPI.Speak("Welcome to the my VB.NET tutorial")

End Sub

Monday, 2 July 2012

Important and useful Date and Time functions

DateAdd : DateAdd function in VB.NET is used to return specified Date added into current Date to know that specified Date. In example given below we specified the Date after 10 day in current Date.


Public Sub Date_Add()

        MsgBox("Date after 10 days::" & DateAdd(DateInterval.Day, 10, Now.Date))

End Sub



DateDiff : DateDiff function in VB.NET is used to return the time interval between two Dates specified in the function. The time interval between two Date can be year, month, day, hour, minute and also can be a second. In example given below we only show date difference between day.

        Dim d1 As Date = #1/1/2012#
        Dim d2 As Date = #7/1/2012#
        Dim res As Long
        res = DateDiff(DateInterval.Day, d1, d2)
        MsgBox("Time difference between the dates is::" & res)

End Sub



IsDate : IsDate function in VB.NET is used to return a Boolean either True or False to specified that the expression is a valid format Date or not? In example given below we check a string value is a correct format Date and its return True.

Public Sub Is_Date()

        Dim curdate As String
        curdate = "5/31/2010"
        MsgBox("valid date::" & IsDate(curdate))

End Sub

Sunday, 1 July 2012

String Functions of VB.NET(Part III)

Replace : Replace Function in VB.NET is used to replace a substring by another substring for every time the same substring found in the string.This function is case sensitive. For eg. string 'This is Visual Basic application', 'Visual Basic' is replaced with 'VB.NET' are shown given below:-

Public Sub str_replace()

        Dim strg As String = "This is Visual Basic application"
        MsgBox(Replace(strg, "Visual Basic", "VB.NET"))

End Sub



StrComp : StrComp Function in VB.NET is used to compare two strings to return values '-1', '1', '0' based on the value. In this function optional comparison method can have Binary or Text for binary and text comparison. For string comparison False it returns '-1', True returns '0' and type mismatch returns '1' are shown given below:-

 Public Sub Strg_comp()

        Dim Str1 As String = "Google.com"
        Dim Str2 As String = "google.com"
        Dim Str3 As String = "Google.com"
        Dim a As Integer = 5
        MsgBox("Result of string comparison is:: " & StrComp(Str1, Str2, CompareMethod.Binary))
        MsgBox("Result of string comparison is:: " & StrComp(Str1, Str3, CompareMethod.Binary))
        MsgBox("Result of string comparison is:: " & StrComp(Str1, a, CompareMethod.Binary))

 End Sub



StrReverse : StrReverse Function in VB.NET is used to return a string with characters in Reverse order. For eg. string contains value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' return a string 'ZYXWVUTSRQPONMLKJIHGFEDCBA'  are shown given below:-

Public Sub Strg_reverse()

        Dim Str As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        MsgBox("Reversed string is:: " & StrReverse(Str))
 
End Sub

Saturday, 30 June 2012

String Functions of VB.NET(Part II)

Split Function: Split Function in VB.NET is used to break string using a specific delimiter. For eg. Split function break string into three part 'red', 'green' and 'blue' by using delimiter ',' as shown given below:-
Public Sub str_split()

        Dim str() = Split("red,green,blue", ",")
        For i = 0 To str.Length - 1
            MsgBox(str(i))
        Next
 
End Sub



InStr Function: InStr Function in VB.NET is used to find the starting position of a substring inside a string. For eg. InStr function return 15 as the substring 'VB.NET' is in the beginning of the string as shown given below:-

Public Sub InStrg()

        MsgBox("Starting index of VB.NET is:: " & InStr("Hello This Is VB.NET Code", "VB.NET"))

End Sub



LCase Function: LCase Function in VB.NET is used to returns a string after converting to lowercase. For eg. LCase function is used to converted 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' to 'abcdefghijklmnopqrstuvwxyz' lowercase alphabets as shown given below:-

Public Sub Lower_case()

        Dim Str As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        MsgBox("String in Lowercase is::" & LCase(Str))

End Sub



LTrim Function: LTrim Function in VB.NET is used to retruns a copy of a string without leading spaces. For eg. LTrim function remove the leading spaces from String such as '                  VB.NET' become 'VB.NET' as shown given below:-


Public Sub L_trim()

        MsgBox("String after removing leading spaces is:: " & LTrim("                  VB.NET"))

End Sub

Friday, 29 June 2012

String Functions of VB.NET(Part I)

Asc Function: Asc Function in VB.NET is used to return the ascii code for the first character in the string. For eg. Ascii code for A,B,C are 65, 66, 67 respectively as shown given below:-

Public Sub ascii()

        MsgBox("Character Code of A :: " & Asc("A"))
        MsgBox("Character Code of B :: " & Asc("B"))
        MsgBox("Character Code of C :: " & Asc("C"))
      
End Sub 


Chr Function: Chr Function in VB.NET is used to return the character for the specified Ascii value. For eg. character code for 65 to 90 represent the Capital A to Z  as shown given below:-


Public Sub chr_code()

        For i = 65 To 90
            MsgBox("Character " & i & " is :: " & Chr(i))
        Next
 
End Sub


GetChar Function: GetChar Function in VB.NET is used to return the character of the string from the specified index. For eg. character W, T, A return index 1, 9, 12  as shown given below:-

Public Sub GetCharacter()

        Dim Str As String = "Welcome To All"
        For i = 1 To 14
            MsgBox("Character at the index value " & i & " is :: " & GetChar(Str, i))
        Next
 
End Sub