Vbnetlab 221219084537 D10dbef6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

VB.

Net Programs

C# & Dot Net Lab Programs


Part- B
1. VB.net Program to count the number of Vowels in a string

Public Class Form1


Private Sub resultBtn_Click(sender As Object, e As EventArgs) Handles resultBtn.Click
Dim str1, str2 As String
Dim vcount, i, str1len As Integer
vcount = 0
str1 = stringText.Text
str1len = Len(str1)
str1 = LCase(str1)
For i = 1 To str1len
str2 = Mid(str1, i, 1)
If str2 = "a" Or str2 = "e" Or str2 = "i" Or str2 = "o" Or str2 = "u" Then
vcount = vcount + 1
End If
Next i
resultText.Text = vcount
End Sub

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


Close()
End Sub
End Class

Prof. K. Adisesha 1
VB.Net Programs

Output:

Prof. K. Adisesha 2
VB.Net Programs

2. VB.net Program to Check a given is Even or Odd Number or Overflow


if number>10000

Code:
Public Class Form1
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles resultBtn.Click

If valueText.Text Mod 2 = 0 Then


resultText.Text = "EVEN"
Else
resultText.Text = "ODD"
End If
If valueText.Text > 10000 Then
resultText.Text = "Overflow!!!"
End If
Prof. K. Adisesha 3
VB.Net Programs

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click


valueText.Text = ""
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click


Close()
End Sub
End Class

Output:

Prof. K. Adisesha 4
VB.Net Programs

3. VB.net Program to calculate the compound interest

Public Class Form1


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles resultBtn.Click
Dim P As Integer
Dim R As Integer
Dim n As Integer

P = principleText.Text
R = rateText.Text
n = timeText.Text
compText.Text = P * (1 + (R / 100)) ^ n - 1
End Sub

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btnExit.Click


Close()
End Sub

Prof. K. Adisesha 5
VB.Net Programs

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click


principleText.Text = ""
rateText.Text = ""
timeText.Text = ""
End Sub
End Class

Output:

Prof. K. Adisesha 6
VB.Net Programs

4. VB.Net Program to Display the sum of Positive and negative numbers


using input box

Code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub resultBtn_Click(sender As Object, e As EventArgs) Handles resultBtn.Click


Dim arr(), a, b, c, i As Integer
a = CInt(limitText.Text)
ReDim arr(a)
For i = 0 To a - 1 Step 1
arr(i) = CInt(InputBox("Enter elements:"))
If arr(i) > 0 Then
b = b + arr(i)
ElseIf arr(i) < 0 Then
c = c + arr(i)
End If
Next
positiveText.Text = b
negativeText.Text = c
End Sub

Prof. K. Adisesha 7
VB.Net Programs

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


Close()
End Sub
End Class

Output:

Prof. K. Adisesha 8
VB.Net Programs

5. VB.Net Program to concatenate two strings and display result using


Message Box.

Code:

Public Class Form1


Private Sub resultBtn_Click(sender As Object, e As EventArgs) Handles resultBtn.Click
Dim x As String
Dim y As String
x = firstText.Text
y = lastText.Text
MsgBox(firstText.Text + " " + lastText.Text, vbOKCancel)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click


Close()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnclear.Click


firstText.Clear()
lastText.Clear()
End Sub
End Class

Prof. K. Adisesha 9
VB.Net Programs

Output:

Prof. K. Adisesha 10
VB.Net Programs

6. Write a program to change the style of font based on the user’s


choice by using CHECK BOX BUTTON

Public Class Form1


Private Sub setStyle()
Dim style = FontStyle.Regular
If boldCheck.Checked Then
style = style Or FontStyle.Bold
End If
If italicsCheck.Checked Then
style = style Or FontStyle.Italic
End If
If underLineCheck.Checked Then
style = style Or FontStyle.Underline
End If
inputText.Font = New Font(inputText.Font, style)
End Sub

Private Sub boldCheck_CheckedChanged(sender As Object, e As EventArgs) Handles


boldCheck.CheckedChanged
setStyle()
End Sub

Private Sub italicsCheck_CheckedChanged(sender As Object, e As EventArgs) Handles


italicsCheck.CheckedChanged

Prof. K. Adisesha 11
VB.Net Programs

setStyle()
End Sub

Private Sub underLineCheck_CheckedChanged(sender As Object, e As EventArgs)


Handles underLineCheck.CheckedChanged
setStyle()
End Sub

End Class

Output:

Prof. K. Adisesha 12
VB.Net Programs

7. Write a program to generate a Student Enrolment Details form using


Combo box.

Code:
Public Class Form1
Private Sub dispBtn_Click(sender As Object, e As EventArgs) Handles dispBtn.Click
MsgBox("These are the following details entered: " + vbCrLf + " Name:" + nameText.Text + vbCrLf
+ "Course:" + courseCombo.Text + vbCrLf + "Semester:" + semCombo.Text + vbCrLf + "Marks:" +
marksText.Text, vbOKOnly)
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click


Close()
End Sub
End Class

Prof. K. Adisesha 13
VB.Net Programs

Output:

Prof. K. Adisesha 14
VB.Net Programs

8. Write a program to generate a Dynamic User login form using


Database.

Code:
Note: Follow the following steps:
➢ Provide MySQL password
➢ Create a Database and provide DB name in Inputbox.
➢ Login table will be created with the given Name in Inputbox

Imports System.Data.Odbc

Public Class Form1


Dim uname As String = InputBox("Enter the UserName of MySql:")
Dim pass As String = InputBox("Enter the password of MySql:")
Dim dB As String = InputBox("Enter the DataBase name")

Dim connectionString As String = "DRIVER={MySQL ODBC 8.0 Unicode


Driver};SERVER=localhost;DATABASE=" + dB + ";UID=" + uname + ";PASSWORD=" + pass
+ ";OPTION=3;"
Public conn As New OdbcConnection(connectionString)
Dim sqlTableCreate As String = "CREATE TABLE users(username varchar(25),password
varchar(25));"
Dim cmdCreate As New OdbcCommand(sqlTableCreate, conn)

Prof. K. Adisesha 15
VB.Net Programs

Sub Open()
cmdCreate.ExecuteNonQuery()
End Sub
Private Sub createBtn_Click(sender As Object, e As EventArgs) Handles createBtn.Click

Try

If unameText.Text = "" Or passText.Text = "" Then


MsgBox("Please Provide the values", vbExclamation)
Else
conn.Open()
Dim rows As Integer
Dim sqlInsert As String = "INSERT INTO users(username,password) values(" + "'"
+ unameText.Text + "','" + passText.Text + "')"
Dim cmd As New OdbcCommand(sqlInsert, conn)
rows = cmd.ExecuteNonQuery()
If rows > 0 Then
MsgBox("User Created!", vbOK)
End If
End If
conn.Close()
Catch ex As Exception
MsgBox("Database Error", vbExclamation)
End Try

End Sub

Private Sub loginBtn_Click(sender As Object, e As EventArgs) Handles loginBtn.Click


Try
conn.Open()
Dim sqlSelect As String = "SELECT username,password FROM users WHERE
username=" + "'" + unameText.Text + "' AND " + "password='" + passText.Text + "'"
Dim cmd As New OdbcCommand(sqlSelect, conn)
Dim sqlResult As OdbcDataReader
sqlResult = cmd.ExecuteReader()
If sqlResult.HasRows Then
MsgBox("User Successfully Logged In", vbOK)
conn.Close()
Else
MsgBox("No Match found", vbExclamation)
conn.Close()
End If
Catch ex As Exception
MsgBox("DataBase Error", vbCritical)
End Try

Prof. K. Adisesha 16
VB.Net Programs

End Sub

Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click


unameText.Clear()
passText.Clear()
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles


showPassCheck.CheckedChanged
If showPassCheck.CheckState = CheckState.Unchecked Then
passText.PasswordChar = "*"
ElseIf showPassCheck.CheckState = CheckState.Checked Then
passText.PasswordChar = ""
End If
End Sub

End Class

Output:

Prof. K. Adisesha 17
VB.Net Programs

9. Program to Implement MDI (Multiple Document Interface) Parent Form

Follow the following Steps


➢ To make a form as MDI Form set its IsMdiContainer property as true.
➢ To define a parent form to a child form set MdiParent property.
➢ To arrange the child forms, use LayoutMdi() method.
➢ To get reference of the current child form use ActiveMdiChild property.
➢ To get reference of a control from the child form use its Controls collection.

Output:

Prof. K. Adisesha 18
VB.Net Programs

Prof. K. Adisesha 19

You might also like