0% found this document useful (0 votes)
5 views

12th pract

The document contains multiple Visual Basic (VB) programs demonstrating various programming concepts such as using controls in a form, creating and customizing menus, and implementing conditional logic and loops. Each program showcases different functionalities, including user input handling, arithmetic operations, and event-driven programming. Overall, it serves as a practical guide for beginners to understand and apply VB programming techniques.

Uploaded by

aishwarythengre
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)
5 views

12th pract

The document contains multiple Visual Basic (VB) programs demonstrating various programming concepts such as using controls in a form, creating and customizing menus, and implementing conditional logic and loops. Each program showcases different functionalities, including user input handling, arithmetic operations, and event-driven programming. Overall, it serves as a practical guide for beginners to understand and apply VB programming techniques.

Uploaded by

aishwarythengre
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/ 8

1) VB Program- Use of Various Tools in Toolbox.

Public Class Form1

' Declare controls to be used in the program


Dim btnClickMe As Button
Dim txtInput As TextBox
Dim lblMessage As Label
Dim chkAgree As CheckBox
Dim lstItems As ListBox
Dim rbtnOption1 As RadioButton
Dim rbtnOption2 As RadioButton
Dim progressBar As ProgressBar

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


' Initialize the controls
btnClickMe = New Button()
txtInput = New TextBox()
lblMessage = New Label()
chkAgree = New CheckBox()
lstItems = New ListBox()
rbtnOption1 = New RadioButton()
rbtnOption2 = New RadioButton()
progressBar = New ProgressBar()

' Set properties of Button (btnClickMe)


btnClickMe.Text = "Click Me!"
btnClickMe.Location = New Point(50, 50)
AddHandler btnClickMe.Click, AddressOf BtnClickMe_Click

' Set properties of TextBox (txtInput)


txtInput.Location = New Point(50, 100)
txtInput.Width = 200

' Set properties of Label (lblMessage)


lblMessage.Text = "Please enter something:"
lblMessage.Location = New Point(50, 150)

' Set properties of CheckBox (chkAgree)


chkAgree.Text = "I agree"
chkAgree.Location = New Point(50, 200)

' Set properties of ListBox (lstItems)


lstItems.Items.Add("Item 1")
lstItems.Items.Add("Item 2")
lstItems.Items.Add("Item 3")
lstItems.Location = New Point(50, 250)

' Set properties of RadioButton (rbtnOption1)


rbtnOption1.Text = "Option 1"
rbtnOption1.Location = New Point(50, 300)

' Set properties of RadioButton (rbtnOption2)


rbtnOption2.Text = "Option 2"
rbtnOption2.Location = New Point(50, 330)

' Set properties of ProgressBar (progressBar)


progressBar.Location = New Point(50, 370)
progressBar.Width = 200
progressBar.Minimum = 0
progressBar.Maximum = 100
progressBar.Value = 0

' Add controls to the form


Me.Controls.Add(btnClickMe)
Me.Controls.Add(txtInput)
Me.Controls.Add(lblMessage)
Me.Controls.Add(chkAgree)
Me.Controls.Add(lstItems)
Me.Controls.Add(rbtnOption1)
Me.Controls.Add(rbtnOption2)
Me.Controls.Add(progressBar)
End Sub

' Button click event


Private Sub BtnClickMe_Click(sender As Object, e As EventArgs)
' Display text from TextBox
If txtInput.Text <> "" Then
lblMessage.Text = "You entered: " & txtInput.Text
End If

' Check if the CheckBox is checked


If chkAgree.Checked Then
lblMessage.Text &= " - Agreed!"
End If

' Update ProgressBar


progressBar.Value = 50

' Display selected item from ListBox


If lstItems.SelectedItem IsNot Nothing Then
MessageBox.Show("You selected: " & lstItems.SelectedItem.ToString())
End If

' Check which RadioButton is selected


If rbtnOption1.Checked Then
MessageBox.Show("Option 1 is selected")
ElseIf rbtnOption2.Checked Then
MessageBox.Show("Option 2 is selected")
End If
End Sub
End Class
2) VB program - creating and customizing Manu

Imports System.Windows.Forms

Public Class Form1

' Declare menu items


Private menuStrip As MenuStrip
Private fileMenu As ToolStripMenuItem
Private newItem As ToolStripMenuItem
Private openItem As ToolStripMenuItem
Private exitItem As ToolStripMenuItem

' Form constructor


Public Sub New()
' Initialize components
InitializeComponent()

' Initialize the MenuStrip


menuStrip = New MenuStrip()

' Create the File menu


fileMenu = New ToolStripMenuItem("File")

' Create menu items under File


newItem = New ToolStripMenuItem("New")
openItem = New ToolStripMenuItem("Open")
exitItem = New ToolStripMenuItem("Exit")

' Add click events for menu items


AddHandler newItem.Click, AddressOf newItem_Click
AddHandler openItem.Click, AddressOf openItem_Click
AddHandler exitItem.Click, AddressOf exitItem_Click

' Add menu items to File menu


fileMenu.DropDownItems.Add(newItem)
fileMenu.DropDownItems.Add(openItem)
fileMenu.DropDownItems.Add(New ToolStripSeparator()) ' Add a separator
fileMenu.DropDownItems.Add(exitItem)

' Add File menu to MenuStrip


menuStrip.Items.Add(fileMenu)

' Add MenuStrip to the form


Controls.Add(menuStrip)

' Set the form properties


MainMenuStrip = menuStrip
Text = "Menu Example"
Size = New Drawing.Size(400, 300)
End Sub
' Event handler for New item
Private Sub newItem_Click(sender As Object, e As EventArgs)
MessageBox.Show("New File Clicked!")
End Sub

' Event handler for Open item


Private Sub openItem_Click(sender As Object, e As EventArgs)
MessageBox.Show("Open File Clicked!")
End Sub

' Event handler for Exit item


Private Sub exitItem_Click(sender As Object, e As EventArgs)
Application.Exit()
End Sub

End Class
3) VB program- Use of if.. then... Else..., For..... next

Imports System

Public Class Program


Public Shared Sub Main()
' Declare variables
Dim numberOfItems As Integer
Dim sumPositive As Integer = 0
Dim userInput As Integer

' Ask the user for the number of items they want to classify
Console.WriteLine("How many numbers would you like to enter?")
numberOfItems = Convert.ToInt32(Console.ReadLine())

' Loop through and process each number


For i As Integer = 1 To numberOfItems
' Ask for input
Console.WriteLine("Enter number " & i & ":")
userInput = Convert.ToInt32(Console.ReadLine())

' Use If...Then...Else to classify the number


If userInput > 0 Then
Console.WriteLine(userInput & " is a positive number.")
sumPositive += userInput ' Add to sum of positive numbers
ElseIf userInput < 0 Then
Console.WriteLine(userInput & " is a negative number.")
Else
Console.WriteLine(userInput & " is zero.")
End If
Next

' After the loop, display the sum of positive numbers


Console.WriteLine("Sum of positive numbers: " & sumPositive)

' Wait for the user to press a key before closing


Console.ReadKey()
End Sub
End Class
4) VB Program - Use of do.. loop...., case..... else
Imports System

Public Class Program


Public Shared Sub Main()
' Declare variables
Dim grade As Integer
Dim validInput As Boolean = False

' Loop until a valid grade between 0 and 100 is entered


Do
Console.WriteLine("Enter a grade (0 to 100):")
grade = Convert.ToInt32(Console.ReadLine())

' Check if the grade is within the valid range


If grade >= 0 And grade <= 100 Then
validInput = True
Else
Console.WriteLine("Invalid grade entered. Please enter a grade between 0 and 100.")
End If

Loop Until validInput ' Loop continues until validInput is True

' Use Select Case to classify the grade


Select Case grade
Case 90 To 100
Console.WriteLine("Grade: A")
Case 80 To 89
Console.WriteLine("Grade: B")
Case 70 To 79
Console.WriteLine("Grade: C")
Case 60 To 69
Console.WriteLine("Grade: D")
Case Else
Console.WriteLine("Grade: F")
End Select

' Wait for the user to press a key before closing


Console.ReadKey()
End Sub
End Class
5) Simple Project Using Visual Basic

Public Class Form1


Dim firstNumber As Double
Dim secondNumber As Double
Dim operation As String

Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn0.Click, btn1.Click, btn2.Click,


btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
TextBox1.Text &= DirectCast(sender, Button).Text
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


firstNumber = Convert.ToDouble(TextBox1.Text)
operation = "+"
TextBox1.Clear()
End Sub

Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click


firstNumber = Convert.ToDouble(TextBox1.Text)
operation = "-"
TextBox1.Clear()
End Sub

Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click


firstNumber = Convert.ToDouble(TextBox1.Text)
operation = "*"
TextBox1.Clear()
End Sub

Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click


firstNumber = Convert.ToDouble(TextBox1.Text)
operation = "/"
TextBox1.Clear()
End Sub

Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click


secondNumber = Convert.ToDouble(TextBox1.Text)
Select Case operation
Case "+"
TextBox1.Text = (firstNumber + secondNumber).ToString()
Case "-"
TextBox1.Text = (firstNumber - secondNumber).ToString()
Case "*"
TextBox1.Text = (firstNumber * secondNumber).ToString()
Case "/"
If secondNumber <> 0 Then
TextBox1.Text = (firstNumber / secondNumber).ToString()
Else
TextBox1.Text = "Error"
End If
End Select
End Sub

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


TextBox1.Clear()
firstNumber = 0
secondNumber = 0
operation = ""
End Sub
End Class

You might also like