0% found this document useful (0 votes)
10 views69 pages

Programs

The document provides a comprehensive guide on creating applications using VB.Net, detailing steps for setting up a new project, using various controls, and implementing functionalities such as message boxes, string manipulations, and mathematical operations. It includes code examples for enumerations, label visibility, string functions, date formats, and checking prime numbers. Additionally, it covers exercises for applying inbuilt functions and string concatenation techniques.

Uploaded by

amanyadav672967
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views69 pages

Programs

The document provides a comprehensive guide on creating applications using VB.Net, detailing steps for setting up a new project, using various controls, and implementing functionalities such as message boxes, string manipulations, and mathematical operations. It includes code examples for enumerations, label visibility, string functions, date formats, and checking prime numbers. Additionally, it covers exercises for applying inbuilt functions and string concatenation techniques.

Uploaded by

amanyadav672967
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

VB.

Net DUIAS And DSIM&C, Valsad

• Click File Menu


• Select New Project

• New Project window will appear.


• Select Visual Basic from Left hand side panel and Windows Forms Application
from right hand side panel.
• Give appropriate name for your application and click OK button

• New Window Form will appear

Akruti Naik 1
VB.Net DUIAS And DSIM&C, Valsad

Example 1: Enumeration with MessageBox/MsgBox

• Place 1 Button Control on the form from the ToolBox


• Double Click on that button. The code window will appear. Write following code in
that area.

Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox("Red: " & color.red)
MsgBox("blue: " + color.blue.ToString)
MessageBox.Show("Thursday: " & Days.Thursday)
End Sub

Enum color
red
green = 6
blue
black = 12 ' here blue = 7
End Enum

Enum Days
Sunday = 1
Monday = 2

Akruti Naik 2
VB.Net DUIAS And DSIM&C, Valsad
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Saturday = 7
End Enum
End Class

Display and hide label’s content

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Label1.Visible = True

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Label1.Visible = False
End Sub
End Class

Exercise for Label Control

Public Class Form1

Akruti Naik 3
VB.Net DUIAS And DSIM&C, Valsad
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles button1.Click
Label1.BackColor = Color.Purple
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Label1.ForeColor = Color.Red
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Label1.Text = "WELCOME TO VISUALBASIC"
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Label1.Font = New System.Drawing.Font("arail", 9, FontStyle.Bold)
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
Label1.TextAlign = ContentAlignment.TopRight
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
Label1.Visible = False
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
Label1.Visible = True
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button8.Click
Label1.Image = Image.FromFile("E:\BCA_Akruti\VB\vb\IMG-20170213-
WA0037.jpg")
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button9.Click
End
End Sub
End Class

Apply various Inbuilt Maths functions

Akruti Naik 4
VB.Net DUIAS And DSIM&C, Valsad

Imports System.Math
Public Class Form12
Dim n, m As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
n = InputBox("Enter No:")
MsgBox(Abs(n))
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
n = InputBox("Enter No:")
MsgBox(Cos(n))
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
n = InputBox("Enter No:")
m = InputBox("Enter No:")
MsgBox(Pow(n, m))
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
n = InputBox("Enter No:")
MsgBox(Sqrt(n))
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
MsgBox(PI)
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
n = InputBox("Enter No:")
m = InputBox("Enter No:")
MsgBox(Log(n, m))
End Sub

Akruti Naik 5
VB.Net DUIAS And DSIM&C, Valsad
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button7.Click
n = InputBox("Enter No:")
MsgBox(Sign(n))
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button8.Click
n = InputBox("Enter No:")
m = InputBox("Enter No:")
MsgBox(Round(n, m))
End Sub
End Class

Apply various string functions

Imports System.String
Public Class Form16
Dim n As String
Dim r As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
n = InputBox("Enter First String: ")
r = InputBox("Enter second string: ")
MsgBox(InStr(1, n, r))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
n = InputBox("Enter String:")
MsgBox(LSet(n, 3))
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
n = InputBox("Enter String:")
MsgBox(RSet(n, 3))
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
n = InputBox("Enter String:")
MsgBox(Mid(n, 2, 5))
End Sub

Akruti Naik 6
VB.Net DUIAS And DSIM&C, Valsad
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
n = InputBox("Enter String:")
MsgBox(n.Trim())
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button6.Click
n = InputBox("Enter String:")
MsgBox(LTrim(n))
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button7.Click
n = InputBox("Enter String:")
Dim si, ei As Integer
si = InputBox("Enter Start range:")
ei = InputBox("Enter End range:")
MsgBox(n.Remove(si, ei))
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button8.Click
n = InputBox("Enter String:")
MsgBox(Len(n.PadLeft(5, "*")).ToString)
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button9.Click
n = InputBox("Enter String:")
MsgBox(n.PadRight(5))
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button10.Click
n = InputBox("Enter String")
r = StrReverse(n)
MsgBox("The reverse string is : " + r)
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button11.Click
' n = ""
' Dim l As Integer
' n = InputBox("Enter String")
' r = ""
' l = Len(n)
' For i = l To 1 Step -1
' r = r + Mid(n, i, 1)
' Next i
' If n = r Then
' MsgBox("The string is palindrom ")
' Else
' MsgBox("The string is not palindrom ")
' End If
n = InputBox("Enter String")
r = StrReverse(n)
If n = r Then

Akruti Naik 7
VB.Net DUIAS And DSIM&C, Valsad
MsgBox("The string is palindrom ")
Else
MsgBox("The string is not palindrom ")
End If
End Sub

End Class

Apply various date formats

Public Class Form17


Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles DateTimePicker1.ValueChanged
Dim a As Date = DateTimePicker1.Value
Label1.Text = Format(a, "M/d/y")
Label2.Text = Format(a, "MM/dd/yy")
Label3.Text = Format(a, "dddd,MMMM,d,yyyy")
Label4.Text = Format(a, "d,MMM,yyy")
Label5.Text = Format(a, "hh:mm:ss MM/dd/yy")
Label6.Text = Format(a, "hh:mm:ss")
Label7.Text = Now
End Sub

Private Sub Form17_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

End Sub
End Class

Apply various date functions (1)

Akruti Naik 8
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form18

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Label1.Text = Now
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Label1.Text = DateAdd(DateInterval.Day, 10, DateTimePicker1.Value)
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
Label1.Text = TimeValue(Now)
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button8.Click
Label1.Text = Today
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
Label1.Text = DateValue(DateTimePicker1.Value)
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
Label1.Text = TimeOfDay
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button9.Click
Label1.Text = DateDiff(DateInterval.Day, DateTimePicker1.Value,
DateTimePicker2.Value)
End Sub
End Class

Apply various date functions (2)

Akruti Naik 9
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form19


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim DOB As Date = DateTimePicker1.Value
TextBox2.Text = DateDiff(DateInterval.Year, DOB, Now)
TextBox3.Text = DateDiff(DateInterval.Month, DOB, Now)
TextBox4.Text = DateDiff(DateInterval.Day, DOB, Now)
TextBox5.Text = DateDiff(DateInterval.Minute, DOB, Now)
TextBox6.Text = DateDiff(DateInterval.Second, DOB, Now)
End Sub
Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles DateTimePicker1.ValueChanged
TextBox1.Text = DateTimePicker1.Value.Date
End Sub
End Class

Join two strings (using + / & and concat function)

Public Class Form13


Dim s, r As String
Dim i As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
s = InputBox("Enter String")
r = InputBox("Enter String")
Label1.Text = "The new string is : " & s & r
' i = Len(s) + Len(r)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
s = InputBox("Enter String")
r = InputBox("Enter String")
Label1.Text = "The new string is : " + s.ToString() + r.ToString()
' i = Len(s) + Len(r)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
s = InputBox("Enter String")

Akruti Naik 10
VB.Net DUIAS And DSIM&C, Valsad
r = InputBox("Enter String")
Label1.Text = "The new string is : " & String.Concat(s, r)
End Sub
End Class

Copy string using = and copy function

Public Class Form14


Dim s, s1, s2 As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
s = InputBox("Enter String: ")
s1 = s
s2 = String.Copy(s)
Label3.Text = s1
Label4.Text = s2
End Sub
End Class

Check whether the given number is prime or not

Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim f As Integer
f=0

Akruti Naik 11
VB.Net DUIAS And DSIM&C, Valsad
For i = 2 To TextBox1.Text / 2
If (TextBox1.Text Mod i = 0) Then
f=1
End If
Next i
If (f = 0) Then
MsgBox("The no. is Prime")
Else
MsgBox("The no is not Prime")
End If
End Sub
End Class

Find greater & smallest number from any three numbers

Public Class Form2


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim A, B, C As Integer
A = Val(TextBox1.Text)
B = Val(TextBox2.Text)
C = Val(TextBox3.Text)
If A > B And A > C Then
MsgBox(A & " is the greatest no")
ElseIf B > A And B > C Then
MsgBox(B & " is the greatest no")
ElseIf C > A And C > B Then
MsgBox(C & " is the greatest no")
Else
MsgBox("All are equal nos.")
End If
If A < B And A < C Then
MsgBox(A & " is the smallest no")
ElseIf B < A And B < C Then
MsgBox(B & " is the smallest no")
ElseIf C < A And C < B Then
MsgBox(C & " is the smallest no")
Else
MsgBox("All are equal nos.")

Akruti Naik 12
VB.Net DUIAS And DSIM&C, Valsad
End If
End Sub
End Class

Print given string 10 times on Label control

Public Class Form3


Dim s As String
Dim n As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
s = "Computer"
' n = 10
For i = 1 To 10
Label1.Text = Label1.Text & vbNewLine & s
Next i
End Sub
End Class

Print odd numbers from given range of numbers

Public Class Form4


Dim i, j, k As Integer
Akruti Naik 13
VB.Net DUIAS And DSIM&C, Valsad
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
i = Val(txtstart.Text)
j = Val(txtend.Text)
k=0
If (i > j) Then
MsgBox("Start range is always less than end range")
Else
k=i
While k <= j
If (k Mod 2 <> 0) Then
txtodd.Text = txtodd.Text & " " & k
End If
k=k+1
End While
End If
End Sub
End Class

Check whether the given year is leap year or not

Public Class Form5


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim a As Integer
a = TextBox1.Text
If Date.IsLeapYear(a) Then
MsgBox("Leap-year")
Else
MsgBox("Not Leap-year")
End If
'If (a Mod 4) Then
' MsgBox("Not Leap-year")
'Else
' MsgBox("Leap-year")
'End If
End Sub
End Class

Exercise for TextBox Control

Place 1 Label and 2 TextBox control on form

Akruti Naik 14
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged
TextBox2.Text = TextBox1.Text
End Sub
End Class

Join two TextBox’s content

Public Class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
TextBox3.Text = TextBox1.Text + " " + TextBox2.Text
End Sub
End Class

Check given no is +ve, -ve or zero

Akruti Naik 15
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1


Dim a As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
a = Val(TextBox1.Text)
If a > 0 Then
TextBox2.Text = TextBox1.Text + " is positive no "
End If
If a < 0 Then
TextBox2.Text = TextBox1.Text + " is negative no "
End If
If a = 0 Then
TextBox2.Text = TextBox1.Text + " is zero "
End If
End Sub
End Class

Find minimum no from any 3 nos:

Public Class Form1


Dim a, b, c As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = Val(TextBox3.Text)
If (a < b) Then
If (a < c) Then
TextBox4.Text = a

Akruti Naik 16
VB.Net DUIAS And DSIM&C, Valsad
End If
End If
If (b < a) Then
If (b < c) Then
TextBox4.Text = b
End If
End If
If (c < a) Then
If (c < b) Then
TextBox4.Text = c
End If
End If
End Sub
End Class

Check that no is divisible by 7or not

Public Class Form1


Dim a As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
a = Val(TextBox1.Text)
If (a Mod 7 = 0) Then
TextBox2.Text = "divisible by 7"
Else
TextBox2.Text = "not divisible by 7"
End If
End Sub
End Class

Check for age eligibility for voting

Akruti Naik 17
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
If Val(TextBox2.Text) > 18 Then
TextBox3.Text = TextBox1.Text + " is eligible for voting"
Else
TextBox3.Text = TextBox1.Text + " is not eligible for voting"
End If
End Sub

End Class

Calculate total, grade etc. for student

Public Class Form1


Dim a, b, c, t As Integer
Dim p As Double
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged
a = Val(TextBox2.Text)
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox3.TextChanged
b = Val(TextBox3.Text)
End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox4.TextChanged
c = Val(TextBox4.Text)
t=a+b+c
p=t/3
End Sub

Akruti Naik 18
VB.Net DUIAS And DSIM&C, Valsad

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox6.Text = t
TextBox7.Text = p
TextBox5.Text = TextBox1.Text
If (a > 35) And (b > 35) And (c > 35) Then
If (p > 70) Then
TextBox8.Text = "DIST"
TextBox9.Text = "PASS"
ElseIf (p > 60) And (70 < p) Then
TextBox8.Text = "FIRST"
TextBox9.Text = "PASS"
ElseIf (p > 50) And (60 < p) Then
TextBox8.Text = "SECOND"
TextBox9.Text = "PASS"
Else
TextBox8.Text = "PASS"
TextBox9.Text = "PASS"
End If
Else
TextBox9.Text = "FAIL"
End If
End Sub
End Class

Merge content of TextBoxes

Public Class Form1

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged
TextBox4.Text = TextBox1.Text
End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox4.TextChanged
TextBox4.Text = TextBox1.Text + " " + TextBox2.Text + " " + TextBox3.Text
End Sub

Akruti Naik 19
VB.Net DUIAS And DSIM&C, Valsad
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged
TextBox4.Text = TextBox2.Text
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox3.TextChanged
TextBox4.Text = TextBox3.Text
End Sub
End Class

Apply various textbox properties

Public Class Form8

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox1.Visible = True
TextBox2.Visible = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Visible = False
TextBox2.Visible = False
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
TextBox1.Enabled = True
TextBox2.Enabled = True
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
TextBox1.Enabled = False
TextBox2.Enabled = False
End Sub
Akruti Naik 20
VB.Net DUIAS And DSIM&C, Valsad

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
Using f As Font = TextBox1.Font
TextBox1.Font = New Font(f.FontFamily, f.Size + 3, f.Style)
End Using
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button8.Click
TextBox1.ForeColor = Color.Chocolate
TextBox2.ForeColor = Color.DarkGray
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button9.Click
TextBox1.BackColor = Color.Yellow
TextBox2.BackColor = Color.Lime
End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button10.Click
TextBox1.Font = New Font(TextBox1.Font, FontStyle.Bold)
Dim newFont As Font = New Font(TextBox2.Font, FontStyle.Bold)
TextBox2.Font = newFont
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
Using f As Font = TextBox1.Font
TextBox1.Font = New Font(f.FontFamily, f.Size - 3, f.Style)
End Using
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
TextBox1.Top = TextBox1.Top + 10
End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button11.Click
TextBox1.Font = New Font(TextBox1.Font, FontStyle.Italic)
Dim newFont As Font = New Font(TextBox2.Font, FontStyle.Italic)
TextBox2.Font = newFont
End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button12.Click
TextBox1.Font = New Font(TextBox1.Font, FontStyle.Underline)
Dim newFont As Font = New Font(TextBox2.Font, FontStyle.Underline)
TextBox2.Font = newFont
End Sub

Akruti Naik 21
VB.Net DUIAS And DSIM&C, Valsad

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button13.Click
TextBox2.Text = String.Copy(TextBox1.Text)
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button14.Click
TextBox1.Clear()
TextBox2.Clear()
End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button15.Click
End
End Sub
End Class

Split a single string into multiple strings

Imports System.String
Public Class Form15
Dim a As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
a = TextBox1.Text
MsgBox(Len(a))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
a = TextBox1.Text
MsgBox(LCase(a))
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
a = TextBox1.Text
MsgBox(UCase(a))
End Sub

Akruti Naik 22
VB.Net DUIAS And DSIM&C, Valsad
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim i As Integer
Dim Arr() As String = Strings.Split(TextBox1.Text, " ")
'try with any character in place of space in above function
Label2.Text = ""
Label2.Text = Arr(0)

MsgBox(Arr(0))
MsgBox(Arr(1))
MsgBox(Arr(2))
MsgBox(Arr(3))
MsgBox(Arr(4))
MsgBox(Arr(5))

End Sub
End Class

Change label’s fore color based on radio button selection

Public Class Form20


Dim s As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Label1.ForeColor = Color.Blue
ElseIf RadioButton2.Checked = True Then
Label1.ForeColor = Color.Brown
ElseIf RadioButton3.Checked = True Then
Label1.ForeColor = Color.Cyan
ElseIf RadioButton4.Checked = True Then
Label1.ForeColor = Color.DarkOrange
ElseIf RadioButton5.Checked = True Then
Label1.ForeColor = Color.ForestGreen
ElseIf RadioButton6.Checked = True Then
Label1.ForeColor = Color.Gold
ElseIf RadioButton7.Checked = True Then
Label1.ForeColor = Color.Honeydew
ElseIf RadioButton8.Checked = True Then
Label1.ForeColor = Color.HotPink
ElseIf RadioButton9.Checked = True Then
Label1.ForeColor = Color.IndianRed

Akruti Naik 23
VB.Net DUIAS And DSIM&C, Valsad
ElseIf RadioButton10.Checked = True Then
Label1.ForeColor = Color.Khaki
ElseIf RadioButton11.Checked = True Then
Label1.ForeColor = Color.SlateGray
ElseIf RadioButton12.Checked = True Then
Label1.ForeColor = Color.Yellow
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
s = InputBox("Enter string: ")
Label1.Text = s
End Sub
End Class

Change form’s backcolor using scrollbars

Public Class Form21


Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
Me.BackColor = Color.FromArgb(HScrollBar1.Value, HScrollBar2.Value,
HScrollBar3.Value)
End Sub

Private Sub HScrollBar2_Scroll(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.ScrollEventArgs) Handles HScrollBar2.Scroll
Me.BackColor = Color.FromArgb(HScrollBar1.Value, HScrollBar2.Value,
HScrollBar3.Value)
End Sub

Private Sub HScrollBar3_Scroll(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.ScrollEventArgs) Handles HScrollBar3.Scroll
Me.BackColor = Color.FromArgb(HScrollBar1.Value, HScrollBar2.Value,
HScrollBar3.Value)
End Sub

Akruti Naik 24
VB.Net DUIAS And DSIM&C, Valsad
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
Me.BackColor = Color.FromArgb(VScrollBar1.Value, VScrollBar2.Value,
VScrollBar3.Value)
End Sub

Private Sub VScrollBar2_Scroll(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.ScrollEventArgs) Handles VScrollBar2.Scroll
Me.BackColor = Color.FromArgb(VScrollBar1.Value, VScrollBar2.Value,
VScrollBar3.Value)
End Sub

Private Sub VScrollBar3_Scroll(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.ScrollEventArgs) Handles VScrollBar3.Scroll
Me.BackColor = Color.FromArgb(VScrollBar1.Value, VScrollBar2.Value,
VScrollBar3.Value)
End Sub
End Class

Calculate Discount amount from total payable amount (on radio button check changed
event)

Public Class Form1

Dim a, b As Integer
Dim t, p As Double

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
End
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged
a = Val(TextBox1.Text)
End Sub

Akruti Naik 25
VB.Net DUIAS And DSIM&C, Valsad
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles RadioButton1.CheckedChanged
b = 10
t = a * b \ 100
p=a-t
TextBox2.Text = t
TextBox3.Text = p
End Sub

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles RadioButton3.CheckedChanged
b = 35
t = a * b \ 100
p=a-t
TextBox2.Text = t
TextBox3.Text = p
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles RadioButton2.CheckedChanged
b = 50
t = a * b \ 100
p=a-t
TextBox2.Text = t
TextBox3.Text = p
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
End Sub
End Class

Calculate Discount amount from total payable amount (on button click event)

Akruti Naik 26
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1

Dim a, b As Integer
Dim t, p As Double
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
End
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged
a = Val(TextBox1.Text)
End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles RadioButton1.CheckedChanged
b = 10
End Sub

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles RadioButton3.CheckedChanged
b = 35
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles RadioButton2.CheckedChanged
b = 50

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
t = a * b \ 100
p=a-t
TextBox2.Text = t
TextBox3.Text = p
End Sub
End Class

Hide / display / append textbox and its content

Akruti Naik 27
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox1.Visible = True
TextBox1.Text = "WELCOME TO VB.NET WORLD"
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
TextBox1.Visible = False
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
TextBox1.AppendText(" Hey there!")
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
TextBox1.Enabled = True
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
TextBox1.Enabled = False
End Sub
End Class

Display information based on radio button selection

Akruti Naik 28
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
If RadioButton1.Checked Then
TextBox2.Text = "MR " + TextBox1.Text + " . " + " You are male "
End If
If RadioButton2.Checked Then
TextBox2.Text = "MRS " + TextBox1.Text + " . " + " you are female"
End If
End Sub
End Class

Display information based on checkbox and radio button selection

Public Class Form1


Dim a, b As String
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles RadioButton1.CheckedChanged
a = "male"
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles RadioButton2.CheckedChanged
a = "female"
End Sub

Akruti Naik 29
VB.Net DUIAS And DSIM&C, Valsad

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
b=""
TextBox2.Text = " "
If (CheckBox1.Checked = True) Then
b = "badminton"
TextBox2.Text = " "
End If
If (CheckBox2.Checked = True) Then
b = "cricket" + "," + b
TextBox2.Text = " "
End If
If (CheckBox3.Checked = True) Then
b = "swimming" + "," + b
TextBox2.Text = " "
End If
If (CheckBox4.Checked = True) Then
b = "music" + "," + b
TextBox2.Text = " "
End If
If (CheckBox5.Checked = True) Then
b = "hockey" + "," + b
TextBox2.Text = " "
End If
If (CheckBox6.Checked = True) Then
b = "football" + "," + b
TextBox2.Text = " "
Else
TextBox2.Text = " "
End If
TextBox2.Text = " "
TextBox2.Text = "Your name is " + TextBox1.Text + " ." + vbCrLf + "You are a " + a +
"." + vbCrLf + "and Your hobbies are " + b
End Sub
End Class

Display bill information

Akruti Naik 30
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1


Dim a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0, b1, b2, b3, b4, b5, b6, b7,
b8, d, e, f As Integer
Dim t, c1, c2, c3, c4, c5, c6, c7, c8 As Double

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox11.Text = ""
If (PIZZA.Checked = True) Then
a1 = Val(TextBox3.Text)
b1 = 45
c1 = a1 * b1
t = t + c1
Else
a1 = 0
b1 = 0
End If
If (BURGER.Checked = True) Then
a2 = Val(TextBox4.Text)
b2 = 40
c2 = a2 * b2
t = t + c2
Else
a2 = 0
b2 = 0
End If
If (SANDWICH.Checked = True) Then
a3 = Val(TextBox5.Text)
b3 = 45
c3 = a3 * b3
t = t + c3
Else
a3 = 0
b3 = 0

Akruti Naik 31
VB.Net DUIAS And DSIM&C, Valsad
End If
If (GRILLSNWC.Checked = True) Then
a4 = Val(TextBox6.Text)
b4 = 45
c4 = a4 * b4
t = t + c4
Else
a4 = 0
b4 = 0
End If
If (CHEEZPIZZA.Checked = True) Then
a5 = Val(TextBox7.Text)
b5 = 45
c5 = a5 * b5
t = t + c5
Else
a5 = 0
b5 = 0
End If
If (FRANKIE.Checked = True) Then
a6 = Val(TextBox8.Text)
b6 = 45
c6 = a6 * b6
t = t + c6
Else
a6 = 0
b6 = 0
End If
If (FRENCHFRIES.Checked = True) Then
a7 = Val(TextBox9.Text)
b7 = 45
c7 = a7 * b7
t = t + c7
Else
a7 = 0
b7 = 0
End If
If (HOTDOG.Checked = True) Then
a8 = Val(TextBox10.Text)
b8 = 45
c8 = a8 * b8
t = t + c8
Else
a8 = 0
b8 = 0
End If
If (RadioButton1.Checked = True) Then
d = 30
t=t+d
End If
If (RadioButton3.Checked = True) Then
d = 80

Akruti Naik 32
VB.Net DUIAS And DSIM&C, Valsad
t=t+d
End If
If (RadioButton2.Checked = True) Then
d = 50
t=t+d
End If
TextBox11.Text = "*********************************************WELCOME TO DESI
CLUB*********************************************" + vbCrLf + "NAME OF CUSTOMER:-"
+ TextBox1.Text + " " + "TABLE NO:-" + TextBox2.Text + vbCrLf +
"...................................................................................................................................................................." +
vbCrLf + "QTY" + " " + "PRICE" + " " + "ITEM" + vbCrLf +
"...................................................................................................................................................................." +
vbCrLf + a1.ToString + " " + b1.ToString + " " + PIZZA.Name + vbCrLf +
a2.ToString + " " + b2.ToString + " " + BURGER.Name + vbCrLf + a3.ToString +
" " + b3.ToString + " " + SANDWICH.Name + vbCrLf + a4.ToString + " "+
b4.ToString + " " + GRILLSNWC.Name + vbCrLf + a5.ToString + " "+
b5.ToString + " " + CHEEZPIZZA.Name + vbCrLf + a6.ToString + " "+
b6.ToString + " " + FRANKIE.Name + vbCrLf + a7.ToString + " " + b7.ToString +
" " + FRENCHFRIES.Name + vbCrLf + a8.ToString + " " + b8.ToString + " "
+ HOTDOG.Name + vbCrLf + "COLD DRINK :" + d.ToString + vbCrLf +
"...................................................................................................................................................................." +
"TOTAL:- " + t.ToString()
t=0
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
TextBox11.Text = "*********************************************WELCOME TO DESI
CLUB*********************************************" + vbCrLf + "NAME OF CUSTOMER:-"
+ TextBox1.Text + " " + "TABLE NO:-" + TextBox2.Text + vbCrLf +
"...................................................................................................................................................................." +
vbCrLf + "QTY" + " " + "PRICE" + " " + "ITEM" + vbCrLf +
"...................................................................................................................................................................." +
vbCrLf + a1.ToString + " " + b1.ToString + " " + PIZZA.Name + vbCrLf +
a2.ToString + " " + b2.ToString + " " + BURGER.Name + vbCrLf + a3.ToString +
" " + b3.ToString + " " + SANDWICH.Name + vbCrLf + a4.ToString + " "+
b4.ToString + " " + GRILLSNWC.Name + vbCrLf + a5.ToString + " "+
b5.ToString + " " + CHEEZPIZZA.Name + vbCrLf + a6.ToString + " "+
b6.ToString + " " + FRANKIE.Name + vbCrLf + a7.ToString + " " + b7.ToString +
" " + FRENCHFRIES.Name + vbCrLf + a8.ToString + " " + b8.ToString + " "
+ HOTDOG.Name + vbCrLf + "COLD DRINK :" + d.ToString + vbCrLf +
"...................................................................................................................................................................." +
"TOTAL:- " + t.ToString()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
End
End Sub
End Class

Add / insert / remove items from listbox (1)

Akruti Naik 33
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form22


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim s As String
s = InputBox("Enter Name: ")
ListBox1.Items.Add(s)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
If ListBox1.SelectedIndex = -1 Then
MsgBox("First select any item")
Else
ListBox1.Items.Remove(ListBox1.SelectedItem)
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
ListBox1.Items.Clear()
End Sub
End Class

Add / insert / remove items from listbox

Akruti Naik 34
VB.Net DUIAS And DSIM&C, Valsad
Public Class Form1
Dim i, j As Integer
Dim str As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
ListBox1.Items.Insert(i, str)
TextBox1.Text = ""
TextBox2.Text = ""
MsgBox("sucessfully inserted")
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged
str = TextBox1.Text
End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox2.TextChanged
i = Val(TextBox2.Text)
End Sub

Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox3.TextChanged
str = TextBox3.Text
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
If (ListBox1.FindStringExact(str)) = True Then
ListBox1.Items.Remove(str)
MsgBox("deleted sucessfully")
Else
MsgBox("not found")
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
If (ListBox1.GetSelected(i)) = False Then
ListBox1.Items.RemoveAt(i)
MsgBox("deleted sucessfully")
Else
MsgBox("not found")
End If
End Sub

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox4.TextChanged
i = Val(TextBox4.Text)
End Sub
End Class

Akruti Naik 35
VB.Net DUIAS And DSIM&C, Valsad

Select and change Backgroun and fore color for label from listboxes

Public Class Form1


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Label1.ForeColor = Color.Brown
ComboBox1.Items.Add("blue")
ComboBox1.Items.Add("green")
ComboBox1.Items.Add("black")
ComboBox1.Items.Add("white")
ComboBox1.Items.Add("purple")
ComboBox1.Items.Add("yellow")
ComboBox1.Items.Add("orange")
ComboBox2.Items.Add("blue")
ComboBox2.Items.Add("green")
ComboBox2.Items.Add("black")
ComboBox2.Items.Add("white")
ComboBox2.Items.Add("purple")
ComboBox2.Items.Add("yellow")
ComboBox2.Items.Add("orange")
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.Equals("blue") = True Then
Label1.ForeColor = Color.Blue
End If
If ComboBox1.SelectedItem.Equals("green") = True Then
Label1.ForeColor = Color.Green
End If
If ComboBox1.SelectedItem.Equals("black") = True Then
Label1.ForeColor = Color.Black
End If
If ComboBox1.SelectedItem.Equals("white") = True Then
Label1.ForeColor = Color.White
End If
If ComboBox1.SelectedItem.Equals("purple") = True Then
Label1.ForeColor = Color.Purple
End If
If ComboBox1.SelectedItem.Equals("yellow") = True Then
Label1.ForeColor = Color.Yellow
End If
If ComboBox1.SelectedItem.Equals("orange") = True Then
Label1.ForeColor = Color.Orange

Akruti Naik 36
VB.Net DUIAS And DSIM&C, Valsad
End If
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
If ComboBox2.SelectedItem.Equals("blue") = True Then
Label1.BackColor = Color.Blue
End If
If ComboBox2.SelectedItem.Equals("green") = True Then
Label1.BackColor = Color.Green
End If
If ComboBox2.SelectedItem.Equals("black") = True Then
Label1.BackColor = Color.Black
End If
If ComboBox2.SelectedItem.Equals("white") = True Then
Label1.BackColor = Color.White
End If
If ComboBox2.SelectedItem.Equals("purple") = True Then
Label1.BackColor = Color.Purple
End If
If ComboBox2.SelectedItem.Equals("yellow") = True Then
Label1.BackColor = Color.Yellow
End If
If ComboBox2.SelectedItem.Equals("orange") = True Then
Label1.BackColor = Color.Orange
End If
End Sub
End Class

Swap contents of listboxes with each other

Public Class Form1


Dim i, j, t, t1 As Integer
Dim str As String

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
ListBox1.Items.Insert(i, str)

Akruti Naik 37
VB.Net DUIAS And DSIM&C, Valsad
TextBox1.Text = ""
MsgBox("sucessfully inserted")
t=t+1
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TextBox1.TextChanged
str = TextBox1.Text
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("blue")
ListBox1.Items.Add("green")
ListBox1.Items.Add("black")
ListBox1.Items.Add("white")
ListBox2.Items.Add("purple")
ListBox2.Items.Add("yellow")
ListBox2.Items.Add("orange")
t=4
t1 = 3
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
i = ListBox2.SelectedIndex
str = ListBox2.SelectedItem
ListBox2.Items.RemoveAt(i)
MsgBox("deleted sucessfully")
ListBox1.Items.Add(str)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
i = ListBox1.SelectedIndex
str = ListBox1.SelectedItem
ListBox1.Items.RemoveAt(i)
MsgBox("deleted sucessfully")
ListBox2.Items.Add(str)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
For j = 0 To 15
If t1 > 0 Then
i = ListBox2.GetSelected(0)
str = ListBox2.Items.Item(0)
ListBox2.Items.RemoveAt(0)
MsgBox("deleted sucessfully")
ListBox1.Items.Add(str)
t=t+1
t1 = t1 - 1

Akruti Naik 38
VB.Net DUIAS And DSIM&C, Valsad
End If
Next
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
For j = 0 To 15
If t > 0 Then
i = ListBox1.GetSelected(0)
str = ListBox1.Items.Item(0)
ListBox1.Items.RemoveAt(0)
MsgBox("deleted sucessfully")
ListBox2.Items.Add(str)

t=t-1
t1 = t1 + 1
End If
Next
End Sub
End Class

Create your own Notepad application

Public Class Form24


Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles NewToolStripMenuItem.Click
If Me.Text <> "notpad" Then
RichTextBox1.SaveFile(Me.Text, RichTextBoxStreamType.PlainText)
MsgBox("Your File Is Saved", MsgBoxStyle.Information, "notpad")
Me.Text = "notpad"
RichTextBox1.Clear()
End If
End Sub

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles OpenToolStripMenuItem.Click
With OpenFileDialog1
.Filter = "text file|*.txt|rtf file|*.rtf"
If .ShowDialog + Windows.Forms.DialogResult.OK Then
RichTextBox1.LoadFile(.FileName, RichTextBoxStreamType.PlainText)

Akruti Naik 39
VB.Net DUIAS And DSIM&C, Valsad
Me.Text = .FileName
End If
End With
End Sub

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles SaveToolStripMenuItem.Click
If Me.Text = "notpad" Then
With SaveFileDialog1
.Filter = "text file|*.txt|rtf file|*.rtf"
.Title = "save file"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.SaveFile(.FileName, RichTextBoxStreamType.PlainText)
Me.Text = .FileName
End If
End With
Else
RichTextBox1.SaveFile(Me.Text, RichTextBoxStreamType.PlainText)
MsgBox("Your File Is Saved", MsgBoxStyle.Information, "notpad")
End If
End Sub

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
With SaveFileDialog1
.Filter = "text file|*.txt|rtf file|*.rtf"
.Title = "save file"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.SaveFile(.FileName, RichTextBoxStreamType.PlainText)
Me.Text = .FileName
End If
End With
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub

Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles UndoToolStripMenuItem.Click
RichTextBox1.Undo()
End Sub

Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles RedoToolStripMenuItem.Click
RichTextBox1.Redo()
End Sub

Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles CutToolStripMenuItem.Click
RichTextBox1.Cut()

Akruti Naik 40
VB.Net DUIAS And DSIM&C, Valsad
End Sub

Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles CopyToolStripMenuItem.Click
RichTextBox1.Copy()
End Sub

Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles PasteToolStripMenuItem.Click
RichTextBox1.Paste()
End Sub

Private Sub SelectallToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles SelectallToolStripMenuItem.Click
RichTextBox1.SelectAll()
End Sub

Private Sub SelectedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles SelectedToolStripMenuItem.Click
With FontDialog1
If .ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.SelectionFont = .Font
End If
End With
End Sub
Private Sub AllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles AllToolStripMenuItem.Click
With FontDialog1
If .ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.Font = .Font
End If
End With
End Sub

Private Sub SelectedToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles SelectedToolStripMenuItem1.Click
With ColorDialog1
If .ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.SelectionColor = .Color
End If
End With
End Sub

Private Sub AllToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles AllToolStripMenuItem1.Click
With ColorDialog1
If .ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.ForeColor = .Color
End If
End With
End Sub

Akruti Naik 41
VB.Net DUIAS And DSIM&C, Valsad
Private Sub LeftToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles LeftToolStripMenuItem.Click
RichTextBox1.SelectionAlignment = HorizontalAlignment.Left
End Sub

Private Sub CenterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles CenterToolStripMenuItem.Click
RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
End Sub

Private Sub RightToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles RightToolStripMenuItem.Click
RichTextBox1.SelectionAlignment = HorizontalAlignment.Right
End Sub

Private Sub BulletToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles BulletToolStripMenuItem.Click
RichTextBox1.BulletIndent = 2
RichTextBox1.SelectionBullet = True
End Sub

Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles ToolStripMenuItem2.Click
RichTextBox1.ZoomFactor = 5
End Sub

Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles ToolStripMenuItem3.Click
RichTextBox1.ZoomFactor = 10
End Sub
End Class

Add controls on form at runtime

Public Class Form26


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim btn As New Button
btn.Size = New Size(100, 80)
btn.Location = New Point(200, 200)
Me.Controls.Add(btn)

Akruti Naik 42
VB.Net DUIAS And DSIM&C, Valsad
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Dim txt As New TextBox
txt.Size = New Size(100, 200)
txt.Location = New Point(120, 100)
Me.Controls.Add(txt)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Dim lbl As New Label
lbl.Size = New Size(100, 200)
lbl.Location = New Point(150, 150)
Me.Controls.Add(lbl)
lbl.Text = "New Label"
End Sub
End Class

Use of progressbar

Public Class Form27


Private Sub Form27_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Timer1.Tick
If ProgressBar1.Value < 100 Then
ProgressBar1.Value = ProgressBar1.Value + 10
Else
ToolStripStatusLabel1.Text = Now()
ProgressBar1.Visible = False
End If
End Sub
End Class

Button game

Akruti Naik 43
VB.Net DUIAS And DSIM&C, Valsad

Public Class Form1

Private Sub Button1_MouseEnter(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Button1.MouseEnter
' Button1.SetBounds(Button1.Location.X + 50, Button1.Location.Y + 50,
Button1.Width, Button1.Height)
End Sub

Private Sub Button1_MouseHover(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Button1.MouseHover
' Button1.SetBounds(Button1.Location.X + 50, Button1.Location.Y + 50,
Button1.Width, Button1.Height)
End Sub

Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As


System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
Button1.SetBounds(Button1.Location.X + 50, Button1.Location.Y + 50,
Button1.Width, Button1.Height)
End Sub
End Class

Akruti Naik 44
VB.Net DUIAS And DSIM&C, Valsad
Calculator

Public Class Form23


Dim no1, no2 As Double
Dim op As String

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
TextBox1.AppendText(1)
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button8.Click
TextBox1.AppendText(2)
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button9.Click
TextBox1.AppendText(3)
End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button10.Click
TextBox1.AppendText(4)
End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button11.Click
TextBox1.AppendText(5)
End Sub

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button12.Click
TextBox1.AppendText(6)
End Sub

Akruti Naik 45
VB.Net DUIAS And DSIM&C, Valsad

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button13.Click
TextBox1.AppendText(7)
End Sub

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button14.Click
TextBox1.AppendText(8)
End Sub

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button15.Click
TextBox1.AppendText(9)
End Sub

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button16.Click
TextBox1.AppendText(0)
End Sub
Private Sub Btnadd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Btnadd.Click
no1 = Val(TextBox1.Text)
TextBox1.Clear()
op = "+"
End Sub

Private Sub btndiv_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btndiv.Click
no1 = Val(TextBox1.Text)
TextBox1.Clear()
op = "/"
End Sub

Private Sub btneql_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btneql.Click
no2 = Val(TextBox1.Text)
If op = "+" Then
TextBox1.Text = no1 + no2
ElseIf op = "-" Then
TextBox1.Text = no1 - no2
ElseIf op = "*" Then
TextBox1.Text = no1 * no2
ElseIf op = "/" Then
TextBox1.Text = no1 / no2
End If
End Sub

Private Sub btndot_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btndot.Click
If InStr(TextBox1.Text, ".") = 0 Then
TextBox1.AppendText(".")

Akruti Naik 46
VB.Net DUIAS And DSIM&C, Valsad
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
TextBox1.Clear()
End Sub
End Class

GroupBox Demo

Public Class groupbox


Dim str1, str2 As String
Private Sub optp2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles optp2.CheckedChanged
str1 = "PII"
' OR str1 = optp2.Text
End Sub

Private Sub optp3_CheckedChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles optp3.CheckedChanged
str1 = "PIII"
End Sub

Private Sub optp4_CheckedChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles optp4.CheckedChanged
str1 = "PIV"
End Sub

Private Sub opt95_CheckedChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles opt95.CheckedChanged
str2 = "Windows 95" ' OR str2 = opt95.Text
End Sub

Private Sub optun_CheckedChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles optun.CheckedChanged
str2 = "Unix"
End Sub

Akruti Naik 47
VB.Net DUIAS And DSIM&C, Valsad
Private Sub optnt_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles optnt.CheckedChanged
str2 = "Windows NT"
End Sub

Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnok.Click
Label1.Text = "You have " + str1 + " computer running " + str2 + " OS."
End Sub

End Class

ImageList Demo

Put 1 ImageList Control, 1 Timer Control and 1 PictureBox Control on the form

Public Class ImageList


Dim c As Integer
Private Sub ImageList_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Timer1.Tick
PictureBox1.Image = ImageList1.Images(c)
c=c+1
If c = ImageList1.Images.Count Then
c=0
End If

PictureBox1.SizeMode = PictureBoxSizeMode.Normal
End Sub
End Class

ListView control

Akruti Naik 48
VB.Net DUIAS And DSIM&C, Valsad
Public Class listview111
Private Sub listview111_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'create a new ListView
ListView1.Columns.Add("Item Name", 100, HorizontalAlignment.Left)
ListView1.Columns.Add("Qty", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("Rate", 60, HorizontalAlignment.Left)
ListView1.Columns.Add("Total", 50, HorizontalAlignment.Left)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim str(4) As String
Dim itm As ListViewItem
str(0) = TextBox1.Text
str(1) = TextBox2.Text
str(2) = TextBox3.Text
str(3) = TextBox4.Text
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
End Sub
End Class

MaskedTextBox

Set Mask property according to tour choice/need

TrackBar to display number table based on current tick value

Akruti Naik 49
VB.Net DUIAS And DSIM&C, Valsad
Public Class tic

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles TrackBar1.Scroll
Label1.Text = ""
Dim i As Integer
For i = 1 To 10
Label1.Text = Label1.Text & vbNewLine & TrackBar1.Value & " X " & i & "=" &
(TrackBar1.Value * i)
Next
If TrackBar1.Value Mod 2 = 0 Then
Me.BackColor = Color.Aqua
Else
Me.BackColor = Color.Black
End If
End Sub

TreeView at runtime

Place below code on form’s load event to create tree

Public Class treeview111

Private Sub treeview_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
'create a new TreeView
Dim TreeView1 As TreeView
TreeView1 = New TreeView
TreeView1.Location = New Point(10, 10)
TreeView1.Size = New Size(150, 150)

Me.Controls.Add(TreeView1)
TreeView1.Nodes.Clear()
'Creating the root node
Dim root = New TreeNode("Application")
TreeView1.Nodes.Add(root)
TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 1"))
'Creating child nodes under the first child

For loopindex As Integer = 1 To 4


TreeView1.Nodes(0).Nodes(0).Nodes.Add(New _
TreeNode("Sub Project" & Str(loopindex)))
Next loopindex
' creating child nodes under the root
TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 6"))
'creating child nodes under the created child node

For loopindex As Integer = 1 To 3


TreeView1.Nodes(0).Nodes(1).Nodes.Add(New _
TreeNode("Project File" & Str(loopindex)))
Next loopindex
' Set the caption bar text of the form.

Akruti Naik 50
VB.Net DUIAS And DSIM&C, Valsad
Me.Text = "tutorialspoint.com"
TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 7"))
For loopindex As Integer = 1 To 4
TreeView1.Nodes(0).Nodes(2).Nodes.Add(New _
TreeNode("Project File" & Str(loopindex)))
Next loopindex
End Sub
End Class

Array with Dim, ReDim, Preserve and For Each

Public Class arraycoll


Dim a(5) As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim MyNumbers(4) As Integer
MyNumbers(0) = 1
MyNumbers(1) = 2
MyNumbers(2) = 3
MyNumbers(3) = 4
MyNumbers(4) = 5

MsgBox("First Number is: " & MyNumbers(0))


MsgBox("Second Number is: " & MyNumbers(1))
MsgBox("Third Number is: " & MyNumbers(2))
MsgBox("Fourth Number is: " & MyNumbers(3))
MsgBox("Fifth Number is: " & MyNumbers(4))
'MsgBox("Sixth Number is: " & MyNumbers(5))
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Dim MyNumbers(4) As Integer
Dim i As Integer
MyNumbers(0) = 10
MyNumbers(1) = 20

Akruti Naik 51
VB.Net DUIAS And DSIM&C, Valsad
MyNumbers(2) = 30
MyNumbers(3) = 40
MyNumbers(4) = 50
For i = 0 To 4
'MyNumbers(i)=InputBox()'
ListBox1.Items.Add(MyNumbers(i))
Next i
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Dim MyText(4) As String
Dim i As Integer
MyText(0) = "This"
MyText(1) = "is"
MyText(2) = "a"
MyText(3) = "String"
MyText(4) = "Array"
ListBox1.Items.Clear()
For i = 0 To 4
ListBox1.Items.Add(MyText(i))
Next i

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Dim numbers(10) As Integer
Dim times As Integer
Dim StoreAnswer As Integer
Dim i As Integer
ListBox1.Items.Clear()
times = Val(TextBox1.Text)
For i = 1 To 10
StoreAnswer = i * times
numbers(i) = StoreAnswer
ListBox1.Items.Add(times & " times " & i & " = " & numbers(i))
Next i

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
Dim numbers() As Integer
Dim startAt As Integer
Dim endAt As Integer
Dim times As Integer
Dim StoreAnswer As Integer
Dim i As Integer
times = Val(TextBox2.Text)
startAt = Val(TextBox3.Text)
endAt = Val(TextBox4.Text)

Akruti Naik 52
VB.Net DUIAS And DSIM&C, Valsad
ReDim numbers(endAt)
For i = startAt To endAt
StoreAnswer = i * times
numbers(i) = StoreAnswer
ListBox2.Items.Add(times & " times " & i & " = " & numbers(i))
Next i
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
Dim x() As Integer ' Dynamic Array
ReDim x(5)
For i As Integer = 1 To 5
x(i) = i
ListBox3.Items.Add(i)
Next i
ListBox3.Items.Clear()
ReDim Preserve x(10)
For i As Integer = 6 To 10
x(i) = i
Next i
For i As Integer = 1 To 10
ListBox3.Items.Add(x(i))
Next i
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
Dim StudArr(4) As String

'Store the data


StudArr(0) = "Rhydhm"
StudArr(1) = "Hrushti"
StudArr(2) = "Dwij"
StudArr(3) = "Khanak"
StudArr(4) = "Nishv"
For Each Studname As String In StudArr
If Studname = "Dwij" Then
MsgBox("Found")
End If
Next
For Each Studname As String In StudArr
ListBox4.Items.Add(Studname)
Next
End Sub
End Class

Collection and ArrayList

Akruti Naik 53
VB.Net DUIAS And DSIM&C, Valsad

Public Class CollectionEx


Dim MyVBColl As New Microsoft.VisualBasic.Collection()
Dim i1, i2, i3, i4 As String
Dim myAL As New ArrayList()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(MyVBColl.Item("2ndkey"))
MsgBox(MyVBColl.Item(1))

For i = 1 To MyVBColl.Count
ListBox1.Items.Add(MyVBColl(i))
Next i
End Sub

Private Sub CollectionEx_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load

i1 = "Kruti"
i2 = "Akruti"
i3 = "Mitul"
i4 = "Nirav"
MyVBColl.Add(i1, "1stkey")
MyVBColl.Add(i2, "2ndkey")
MyVBColl.Add(i3, "3rdkey")
MyVBColl.Add(i4, "4thkey")

myAL.Add("Nishv")
myAL.Add("Khanak")
myAL.Add("Hrushti")
myAL.Add("Shruj")
myAL.Add("Arav")
myAL.Add("Kavya")

End Sub
Sub LoadArrList()
ListBox2.Items.Clear()
For i As Integer = 0 To myAL.Count - 1

Akruti Naik 54
VB.Net DUIAS And DSIM&C, Valsad
ListBox2.Items.Add(myAL(i))
Next
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
MyVBColl.Remove("2ndkey")
MyVBColl.Remove(1)
For i = 1 To MyVBColl.Count
MsgBox(MyVBColl(i))
Next i
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
LoadArrList()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
myAL.Sort()
ListBox2.Items.Clear()
LoadArrList()

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
If ListBox2.SelectedItem Is Nothing Then
MsgBox("Please select the name form the list box to search")
Else
MsgBox("Index of the selected name is " &
myAL.IndexOf(ListBox2.SelectedItem).ToString)
End If

End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
myAL.Reverse()
ListBox2.Items.Clear()
LoadArrList()

End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
Dim n As String = InputBox("Please enter the name to search")
If myAL.Contains(n) = True Then
MsgBox("GIVEN NAME HAS BEEN FOUND IN THE COLLECTION")
Else
MsgBox("GIVEN NAME HAS NOT BEEN FOUND IN THE COLLECTION")

Akruti Naik 55
VB.Net DUIAS And DSIM&C, Valsad
End If

End Sub
End Class

Error and Help Provider and status bar

Place errorprovider, helpprovider, timer and status strip on form

Public Class errorhelpprov

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
ErrorProvider1.SetError(TextBox1, "Data should not be blank")
Else 'clear the error description
ErrorProvider1.SetError(TextBox1, "")
End If
End Sub
Private Sub TextBox2_Validating(ByVal Sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles _
TextBox2.Validating
If Not IsNumeric(TextBox2.Text) Then
ErrorProvider2.SetError(TextBox2, "Not a numeric value.")
Else
' Clear the error.
ErrorProvider2.SetError(TextBox2, "")
End If
End Sub

Private Sub errorhelpprov_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
HelpProvider1.SetHelpString(Button10, "Press the button")
ToolStripStatusLabel1.Text = System.DateTime.Today.ToLongDateString()
ToolStripStatusLabel2.Text = Now()

Akruti Naik 56
VB.Net DUIAS And DSIM&C, Valsad
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Timer1.Tick
ToolStripStatusLabel3.Text = Now()
End Sub

End Class

Function and Procedure in Module, Class and Form

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Dim f As New funclass
f.Add(7, 8)
Form14.Show()
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button6.Click
Minus(12, 5)
End Sub

Sub Minus(ByVal x As Integer, ByVal y As Integer)


Dim r As Integer
r=x-y
MsgBox(r)
End Sub

Private Function Minus1(ByVal x As Integer, ByVal y As Integer)


Dim r As Integer
r=x-y
Return r
End Function

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button9.Click

Akruti Naik 57
VB.Net DUIAS And DSIM&C, Valsad
Module1.divi(4, 2)
Call Minus(5, 8)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
Dim f As New funclass
MsgBox(f.Add1(7, 7))
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button7.Click
MsgBox(Minus1(12, 10))
End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button10.Click
MsgBox(Module1.divi1(12, 3))
End Sub
Sub display(ByVal x As Integer, ByVal y As String)
'MsgBox()
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
Dim obj As New clsdemo
obj.disp()
End Sub

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button11.Click
Module3.disp1()
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button8.Click
Module3.disp1(5)
End Sub

Add one class file change its name to funclass and write following code in that

Public Class funclass


Dim A As Integer
Sub Add(ByVal x As Integer, ByVal y As Integer)
A = Val(x) + Val(y)
MsgBox(A)
End Sub
Function Add1(ByVal x As Integer, ByVal y As Integer)
A = Val(x) + Val(y)
Return A
End Function
End Class

Akruti Naik 58
VB.Net DUIAS And DSIM&C, Valsad

Add two module file and write following code in that

Module Module1
Dim r As Integer
Sub divi(ByVal x As Integer, ByVal y As Integer)
r=x/y
MsgBox(r)
End Sub
Function divi1(ByVal x As Integer, ByVal y As Integer)
r=x/y
Return r
End Function
End Module

Module Module3
Private i As Integer

Sub disp1()
MessageBox.Show("Good Afternoon")
End Sub
Sub disp1(ByVal i As Integer)
MsgBox("1 arg")
End Sub
Sub disp1(ByVal i As Integer, ByVal y As Integer)
MsgBox("2 arg")
End Sub

End Module

Exception Handling using On Error and Try Catch

Public Class exhal1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Label4.Visible = False
Dim firstNum, secondNum As Double
Akruti Naik 59
VB.Net DUIAS And DSIM&C, Valsad
On Error GoTo error_handler
firstNum = TextBox1.Text
secondNum = TextBox2.Text
TextBox3.Text = firstNum / secondNum

Exit Sub 'To prevent error handling even the inputs are valid
error_handler:
TextBox3.Text = "Error"
Label4.Visible = True
Label4.Text = " One of the entries is not a number! Try again!"

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
Label5.Visible = False
Dim firstNum, secondNum, answer As Double
Try
firstNum = TextBox4.Text
secondNum = TextBox6.Text
answer = firstNum / secondNum
TextBox5.Text = answer
Catch ex As Exception
TextBox5.Text = "Error"
Label5.Visible = True
Label5.Text = " One of the entries is not a number! Try again!"
End Try

End Sub
End Class

Connectivity with Access Database

Create one table “dept” in “db1.mdb” database. Add fields (d_no, d_name, class, addrs,
dob) and add 5 records in that table. And write following code.

Akruti Naik 60
VB.Net DUIAS And DSIM&C, Valsad

Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim ad As New OleDbDataAdapter
Dim ds As New DataSet
Dim i As Integer = 0
Dim s As String
Sub txtclear()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
End Sub
Public Sub adddata(ByVal s)
con.ConnectionString = "Provider=Microsoft.ACE.oledb.12.0;data
source=E:\BCA_Akruti\VB\Sample\connecitivity\db1.accdb"
con.Open()
cmd.CommandText = s
cmd.Connection = con
ad.SelectCommand = cmd
ds.Clear()
cmd.ExecuteNonQuery()
ad.Fill(ds, "dept")
DataGridView1.DataSource = ds.Tables(0)
con.Close()
If (ds.Tables(0).Rows.Count > 0) Then
navi(i)
End If
Me.BindingSource1.DataSource = ds.Tables("dept")
Me.BindingNavigator1.BindingSource = BindingSource1
End Sub
Public Sub navi(ByVal i As Integer)
TextBox1.Text = ds.Tables(0).Rows(i).Item(0)
TextBox2.Text = ds.Tables(0).Rows(i).Item(1)
TextBox3.Text = ds.Tables(0).Rows(i).Item(2)
TextBox5.Text = ds.Tables(0).Rows(i).Item(3)
TextBox4.Text = ds.Tables(0).Rows(i).Item(4)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
s = "select * from dept"
adddata(s)
navi(i)
BindingNavigator1.Enabled = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnclr.Click
txtclear()

Akruti Naik 61
VB.Net DUIAS And DSIM&C, Valsad

End Sub

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnsave.Click
con.Open()
ds.Clear()
Dim cmd1 As New OleDbCommand("insert into dept values(" & Val(TextBox1.Text)
& " ,' " & TextBox2.Text & " ',' " & TextBox3.Text & " ',' " & TextBox5.Text & " ', ' " &
TextBox4.Text & " ' )", con)
cmd1.ExecuteNonQuery()
ad.InsertCommand = cmd1
ad.Fill(ds, "dept")
navi(Me.BindingContext(ds, "dept").Position)
con.Close()
End Sub

Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btndelete.Click
s = "delete from dept where d_no = " & Val(TextBox1.Text)
i=i-1
adddata(s)
s = "select * from dept"
adddata(s)

End Sub

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnupdate.Click
TextBox1.Enabled = False
s = "update dept set d_name=' " & TextBox2.Text & "',class='" & TextBox3.Text &
"',addrs='" & TextBox5.Text & "',dob='" & TextBox4.Text & "' where d_no=" &
Val(TextBox1.Text)
adddata(s)
TextBox1.Enabled = True
s = "select * from dept"
adddata(s)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
i=0
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
If i = 0 Then
MsgBox("you r on 1st rec")
Else

Akruti Naik 62
VB.Net DUIAS And DSIM&C, Valsad

i=i-1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
If i = ds.Tables(0).Rows.Count - 1 Then
MsgBox("you r on last rec")
Else
i=i+1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End If

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
i = ds.Tables(0).Rows.Count - 1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
s = "select * from dept where d_no =" & Val(TextBox1.Text)
adddata(s)
navi(i)
End Sub

Private Sub BindingNavigatorMovePreviousItem_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
BindingNavigatorMovePreviousItem.Click
If i = 0 Then
MsgBox("you r on 1st rec")
Else
i=i-1
navi(i)
End If
End Sub

Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click
If i = ds.Tables(0).Rows.Count Then
MsgBox("you r on last rec")
Else
i=i+1
navi(i)

Akruti Naik 63
VB.Net DUIAS And DSIM&C, Valsad
End If
End Sub

Private Sub BindingNavigatorMoveFirstItem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles BindingNavigatorMoveFirstItem.Click
Me.BindingContext(ds, "dept").Position = 0
i=0
navi(Me.BindingContext(ds, "dept").Position)
End Sub

Private Sub BindingNavigatorMoveLastItem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles BindingNavigatorMoveLastItem.Click
i = ds.Tables(0).Rows.Count - 1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End Sub

Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click

End Sub

Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click

End Sub
End Class

MDI Application demo

Add one Form1 and change its IsMDIContainer property to true. Place 1 MenuStrip on
form and create menu.

Add another two forms (student and result) in project and on those pages’ load event
write following code

Me.MdiParent = Form1

Form1 will look like

Akruti Naik 64
VB.Net DUIAS And DSIM&C, Valsad

And form2 (i.e. student) will looks like

Code

For Form1

Public Class Form1

Private Sub StudentToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles StudentToolStripMenuItem.Click
student.Show()
End Sub

Private Sub ResultToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles ResultToolStripMenuItem.Click
result.Show()
End Sub
End Class

For student

Imports System.Data.OleDb

Public Class student

Private Sub student_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
Me.MdiParent = Form1
s = "select * from dept"
adddata(s)

Akruti Naik 65
VB.Net DUIAS And DSIM&C, Valsad
navi(i)
' con.Close()
BindingNavigator1.Enabled = True
' Me.BindingNavigator1.BindingSource = BindingSource1
End Sub

Dim con As New OleDbConnection


Dim cmd As New OleDbCommand
Dim ad As New OleDbDataAdapter

Dim ds As New DataSet


Dim i As Integer = 0
Dim s As String
Sub txtclear()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
End Sub
Public Sub adddata(ByVal s)
con.ConnectionString = "Provider=Microsoft.ACE.oledb.12.0;data
source=E:\BCA_Akruti\VB\Sample\connecitivity\db1.accdb"
con.Open()
cmd.CommandText = s
cmd.Connection = con
ad.SelectCommand = cmd
ds.Clear()
cmd.ExecuteNonQuery()
'ds.Clear()
ad.Fill(ds, "dept")
DataGridView1.DataSource = ds.Tables(0)
con.Close()
If (ds.Tables(0).Rows.Count > 0) Then
navi(i)
End If
Me.BindingSource1.DataSource = ds.Tables("dept")
Me.BindingNavigator1.BindingSource = BindingSource1
' inup = True
'con.Close()
End Sub
Public Sub navi(ByVal i As Integer)
TextBox1.Text = ds.Tables(0).Rows(i).Item(0)
TextBox2.Text = ds.Tables(0).Rows(i).Item(1)
TextBox3.Text = ds.Tables(0).Rows(i).Item(2)
TextBox5.Text = ds.Tables(0).Rows(i).Item(3)
TextBox4.Text = ds.Tables(0).Rows(i).Item(4)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnclr.Click

Akruti Naik 66
VB.Net DUIAS And DSIM&C, Valsad
txtclear()

End Sub

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnsave.Click
's = "insert into dept values(" & Val(TextBox1.Text) & " ,' " & TextBox2.Text & " ',' "
& TextBox3.Text & " ',' " & TextBox5.Text & " ', ' " & TextBox4.Text & " ' )"
''i = i + 1
'adddata(s)
''i = i + 1
'MsgBox("record saved")
's = "select * from dept"
'adddata(s)
con.Open()
ds.Clear()
Dim cmd1 As New OleDbCommand("insert into dept values(" & Val(TextBox1.Text)
& " ,' " & TextBox2.Text & " ',' " & TextBox3.Text & " ',' " & TextBox5.Text & " ', ' " &
TextBox4.Text & " ' )", con)
cmd1.ExecuteNonQuery()
ad.InsertCommand = cmd1
ad.Fill(ds, "dept")
navi(Me.BindingContext(ds, "dept").Position)
con.Close()
End Sub

Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btndelete.Click
s = "delete from dept where d_no = " & Val(TextBox1.Text)
i=i-1
adddata(s)
s = "select * from dept"
adddata(s)

End Sub

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnupdate.Click
TextBox1.Enabled = False
s = "update dept set d_name=' " & TextBox2.Text & "',class='" & TextBox3.Text &
"',addrs='" & TextBox5.Text & "',dob='" & TextBox4.Text & "' where d_no=" &
Val(TextBox1.Text)
adddata(s)
TextBox1.Enabled = True
s = "select * from dept"
adddata(s)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button4.Click
i=0
Me.BindingNavigator1.PositionItem.Text = i + 1

Akruti Naik 67
VB.Net DUIAS And DSIM&C, Valsad
navi(i)

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
If i = 0 Then
MsgBox("you r on 1st rec")
Else

i=i-1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
If i = ds.Tables(0).Rows.Count - 1 Then
MsgBox("you r on last rec")
Else
i=i+1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End If

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
i = ds.Tables(0).Rows.Count - 1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button5.Click
s = "select * from dept where d_no =" & Val(TextBox1.Text)
adddata(s)
navi(i)
End Sub

Private Sub BindingNavigatorMovePreviousItem_Click(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
BindingNavigatorMovePreviousItem.Click
If i = 0 Then
MsgBox("you r on 1st rec")
Else
i=i-1
navi(i)

Akruti Naik 68
VB.Net DUIAS And DSIM&C, Valsad
End If
End Sub

Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click
If i = ds.Tables(0).Rows.Count Then
MsgBox("you r on last rec")
Else
i=i+1
navi(i)
End If
End Sub

Private Sub BindingNavigatorMoveFirstItem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles BindingNavigatorMoveFirstItem.Click
Me.BindingContext(ds, "dept").Position = 0
i=0
navi(Me.BindingContext(ds, "dept").Position)
End Sub

Private Sub BindingNavigatorMoveLastItem_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles BindingNavigatorMoveLastItem.Click
i = ds.Tables(0).Rows.Count - 1
Me.BindingNavigator1.PositionItem.Text = i + 1
navi(i)
End Sub

End Class

For result

Public Class result

Private Sub result_Load(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles MyBase.Load
Me.MdiParent = Form1
End Sub
End Class

Akruti Naik 69

You might also like