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

Docvb

java

Uploaded by

nilkamal2266
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Docvb

java

Uploaded by

nilkamal2266
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

18. Write a program to print Pascal Triangle upto n rows.

Design :-

Code :-
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Pascal's Triangle"
End Sub

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


btnGenerate.Click
Dim n As Integer

If Not Integer.TryParse(txtRows.Text, n) OrElse n < 1 Then


MessageBox.Show("Please enter a valid positive integer.", "Input Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If

lstPascalTriangle.Items.Clear()

For i As Integer = 0 To n - 1
Dim row As String = String.Join(" ", GetPascalRow(i))
lstPascalTriangle.Items.Add(row)
Next
End Sub

Private Function GetPascalRow(rowIndex As Integer) As List(Of Integer)


Dim row As New List(Of Integer)
row.Add(1)

For i As Integer = 1 To rowIndex


row.Add(row(i - 1) * (rowIndex - i + 1) \ i)
Next

Return row
End Function
End Class

Output :-
23. Create an application to implement the working of context menu on
textbox.

Design :-

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

Me.Text = "Context Menu Example"

Dim contextMenu As New ContextMenuStrip()

Dim cutItem As New ToolStripMenuItem("Cut")


AddHandler cutItem.Click, AddressOf CutText
contextMenu.Items.Add(cutItem)

Dim copyItem As New ToolStripMenuItem("Copy")


AddHandler copyItem.Click, AddressOf CopyText
contextMenu.Items.Add(copyItem)

Dim pasteItem As New ToolStripMenuItem("Paste")


AddHandler pasteItem.Click, AddressOf PasteText
contextMenu.Items.Add(pasteItem)

Dim selectAllItem As New ToolStripMenuItem("Select All")


AddHandler selectAllItem.Click, AddressOf SelectAllText
contextMenu.Items.Add(selectAllItem)

Dim clearItem As New ToolStripMenuItem("Clear")


AddHandler clearItem.Click, AddressOf ClearText
contextMenu.Items.Add(clearItem)

txtInput.ContextMenuStrip = contextMenu
End Sub

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


If txtInput.SelectedText <> "" Then
Clipboard.SetText(txtInput.SelectedText)
txtInput.SelectedText = ""
End If
End Sub

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


If txtInput.SelectedText <> "" Then
Clipboard.SetText(txtInput.SelectedText)
End If
End Sub

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


If Clipboard.ContainsText() Then
Dim selectionStart = txtInput.SelectionStart
txtInput.Text = txtInput.Text.Insert(selectionStart,
Clipboard.GetText())
txtInput.SelectionStart = selectionStart +
Clipboard.GetText().Length
End If
End Sub

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


txtInput.SelectAll()
End Sub

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


txtInput.Clear()
End Sub
End Class

Output :-
26. Write a Program to launch using PictureBox and Timer control.

Design :-

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

Me.Text = "Rocket Launch Simulation"

picRocket.SizeMode = PictureBoxSizeMode.StretchImage
picRocket.Image =
vbproject.My.Resources.Resources.Screenshot_2025_01_03_171907
picRocket.Location = New Point((Me.ClientSize.Width - picRocket.Width)
\ 2, Me.ClientSize.Height - picRocket.Height)

tmrLaunch.Interval = 50
AddHandler tmrLaunch.Tick, AddressOf tmrLaunch_Tick
End Sub

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


btnLaunch.Click

tmrLaunch.Start()
btnLaunch.Enabled = False
End Sub

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

If picRocket.Top > -picRocket.Height Then


picRocket.Top -= 5
Else

tmrLaunch.Stop()
MessageBox.Show("Rocket Launched!", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information)
btnLaunch.Enabled = True
End If
End Sub
End Class

Output :-
42. Develop an application which is similar to login form.

Design :-

Code :-
Public Class Form2

Private Const ValidUsername As String = "admin"


Private Const ValidPassword As String = "password123"

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


MyBase.Load
Me.Text = "Login Form"
End Sub

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


btnLogin.Click

If txtUsername.Text.Trim() = ValidUsername AndAlso txtPassword.Text =


ValidPassword Then
MessageBox.Show("Login Successful!", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Invalid username or password. Please try again.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub

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


btnCancel.Click

Me.Close()
End Sub

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


Handles txtPassword.TextChanged
End Sub
End Class

Output :-
44. Develop a project which displays the student information in the relevent
fields from the database which already exists.
Design :-

Code :-
Imports System.Data.OleDb

Public Class Form2

Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data


Source=C:\Users\HP\OneDrive\Documents\StudentDB.accdb;"
Dim connection As New OleDbConnection(connectionString)
Dim adapter As OleDbDataAdapter
Dim table As New DataTable()
Dim currentRow As Integer = 0

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


MyBase.Load
Try
connection.Open()
adapter = New OleDbDataAdapter("SELECT * FROM Students",
connection)
adapter.Fill(table)
connection.Close()
If table.Rows.Count > 0 Then
DisplayCurrentRow()
Else
MessageBox.Show("No records found in the database.",
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Database Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub DisplayCurrentRow()


If table.Rows.Count > 0 Then
TextBox1.Text = table.Rows(currentRow)("ID").ToString()
TextBox2.Text = table.Rows(currentRow)("sname").ToString()
TextBox3.Text = table.Rows(currentRow)("Qualification").ToString()
TextBox4.Text = table.Rows(currentRow)("Grade").ToString()
End If
End Sub

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


ButtonFirst.Click
If table.Rows.Count > 0 Then
currentRow = 0
DisplayCurrentRow()
Else
MessageBox.Show("No records available.", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub

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


ButtonLast.Click
If table.Rows.Count > 0 Then
currentRow = table.Rows.Count - 1
DisplayCurrentRow()
Else
MessageBox.Show("No records available.", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub

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


ButtonNext.Click
If currentRow < table.Rows.Count - 1 Then
currentRow += 1
DisplayCurrentRow()
Else
MessageBox.Show("You are already on the last record.",
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub

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


ButtonPrevious.Click
If currentRow > 0 Then
currentRow -= 1
DisplayCurrentRow()
Else
MessageBox.Show("You are already on the first record.",
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub

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


ButtonCancel.Click
Me.Close()
End Sub
End Class

Output :-

DataBase :-
56. Create a table for employee and write a program using Dataset to add,
delete, edit and navigate record.
Design :-

Code :-
Imports System.Data.OleDb

Public Class Form2

Private connectionString As String =


"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\HP\OneDrive\Documents\
EmployeeDB.accdb;"
Private connection As OleDbConnection
Private adapter As OleDbDataAdapter
Private dataset As DataSet
Private currentRow As Integer = 0

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


MyBase.Load
connection = New OleDbConnection(connectionString)
adapter = New OleDbDataAdapter("SELECT * FROM Employee", connection)

Dim builder As New OleDbCommandBuilder(adapter)

dataset = New DataSet()


adapter.Fill(dataset, "Employee")
BindData()
End Sub

Private Sub BindData()


If dataset.Tables("Employee").Rows.Count > 0 Then
Dim row As DataRow = dataset.Tables("Employee").Rows(currentRow)
txtEmployeeID.Text = row("EmployeeID").ToString()
txtFirstName.Text = row("FirstName").ToString()
txtLastName.Text = row("LastName").ToString()
txtPosition.Text = row("empPosition").ToString()
txtSalary.Text = row("empSalary").ToString()
Else
ClearFields()
End If
End Sub

Private Sub ClearFields()


txtEmployeeID.Clear()
txtFirstName.Clear()
txtLastName.Clear()
txtPosition.Clear()
txtSalary.Clear()
End Sub

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


btnAdd.Click
Try
Dim newRow As DataRow = dataset.Tables("Employee").NewRow()
newRow("EmployeeID") = txtEmployeeID.Text
newRow("FirstName") = txtFirstName.Text
newRow("LastName") = txtLastName.Text
newRow("empPosition") = txtPosition.Text
newRow("empSalary") = Convert.ToDecimal(txtSalary.Text)

dataset.Tables("Employee").Rows.Add(newRow)
adapter.Update(dataset, "Employee")
MessageBox.Show("Record added successfully.")
Catch ex As Exception
MessageBox.Show("Error adding record: " & ex.Message)
End Try
End Sub

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


btnUpdate.Click
Try
If dataset.Tables("Employee").Rows.Count > 0 Then
Dim row As DataRow =
dataset.Tables("Employee").Rows(currentRow)
row("EmployeeID") = txtEmployeeID.Text
row("FirstName") = txtFirstName.Text
row("LastName") = txtLastName.Text
row("empPosition") = txtPosition.Text
row("empSalary") = Convert.ToDecimal(txtSalary.Text)

adapter.Update(dataset, "Employee")
MessageBox.Show("Record updated successfully.")
End If
Catch ex As Exception
MessageBox.Show("Error updating record: " & ex.Message)
End Try
End Sub

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


btnDelete.Click
Try
If dataset.Tables("Employee").Rows.Count > 0 Then
dataset.Tables("Employee").Rows(currentRow).Delete()
adapter.Update(dataset, "Employee")
MessageBox.Show("Record deleted successfully.")

If currentRow > 0 Then currentRow -= 1


BindData()
End If
Catch ex As Exception
MessageBox.Show("Error deleting record: " & ex.Message)
End Try
End Sub

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


btnNext.Click
If currentRow < dataset.Tables("Employee").Rows.Count - 1 Then
currentRow += 1
BindData()
End If
End Sub

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


btnPrevious.Click
If currentRow > 0 Then
currentRow -= 1
BindData()
End If
End Sub
End Class
Output :-
Add :-
Update :-

Delete :-

DataBase :-
57. Write a program to access a database using ADO.net and display a key
column in the combo box or list box when an item is selected in it, its
corresponding records is shown in Datagridconrol.

Design :-

Code :-
Imports System.Data.OleDb

Public Class Form3

Private connectionString As String =


"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\HP\OneDrive\Documents\
EmployeeDB.accdb;"
Private connection As OleDbConnection
Private adapter As OleDbDataAdapter
Private dataset As DataSet

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


MyBase.Load

connection = New OleDbConnection(connectionString)

LoadEmployeeIDs()
End Sub

Private Sub LoadEmployeeIDs()


Try

Dim query As String = "SELECT EmployeeID FROM Employee"


adapter = New OleDbDataAdapter(query, connection)
Dim dataTable As New DataTable()
adapter.Fill(dataTable)

cmbEmployeeID.DisplayMember = "EmployeeID"
cmbEmployeeID.ValueMember = "EmployeeID"
cmbEmployeeID.DataSource = dataTable
Catch ex As Exception
MessageBox.Show("Error loading Employee IDs: " & ex.Message)
End Try
End Sub

Private Sub cmbEmployeeID_SelectedIndexChanged(sender As Object, e As


EventArgs) Handles cmbEmployeeID.SelectedIndexChanged

If cmbEmployeeID.SelectedValue IsNot Nothing Then


Dim selectedID As Integer =
Convert.ToInt32(cmbEmployeeID.SelectedValue)
LoadEmployeeDetails(selectedID)
End If
End Sub

Private Sub LoadEmployeeDetails(employeeID As Integer)


Try

Dim query As String = "SELECT * FROM Employee WHERE EmployeeID =


@EmployeeID"
Dim command As New OleDbCommand(query, connection)
command.Parameters.AddWithValue("@EmployeeID", employeeID)

adapter = New OleDbDataAdapter(command)


dataset = New DataSet()

adapter.Fill(dataset, "EmployeeDetails")
dgvEmployeeDetails.DataSource = dataset.Tables("EmployeeDetails")
Catch ex As Exception
MessageBox.Show("Error loading employee details: " & ex.Message)
End Try
End Sub
End Class

Output :-
DataBase :-

You might also like