Sunteți pe pagina 1din 26

GUI Lab Window programs

VB application to illustrate button events


Public Class Form1
Dim NW As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox1.Text = Welcome
End Sub
Private Sub Button1_MouseEnter(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MouseEnter
me.BackClor=Color.red
End Sub
Private Sub Button1_MouseLeave(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MouseLeave
Button1.BackClor=Color.blue
End Sub
End Class

VB application to cut and copy using button event


Public Class Form1
Dim NW As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox2.Text = TextBox1.Text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox2.Text = TextBox1.Text
TextBox1.Text =
End Sub
End Class

VB application to create button at runtime


Public Class Form1
Dim WithEvents Button2 As Button
Dim WithEvents Button3 As Button
Dim WithEvents Button4 As Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If (Button2 Is Nothing) Then
Button2 = New Button
Button2.Size = New Size(80, 30)
Button2.Location = New Point(115, 20)
Button2.Text = "Button2"
Button2.BackColor = Color.Blue
Controls.Add(Button2)
AddHandler Button2.Click, AddressOf Button_Click
End If

If (Button3 Is Nothing) Then


Button3 = New Button
Button3.Size = New Size(80, 30)
Button3.Location = New Point(115, 60)
Button3.Text = "Button3"
Button3.BackColor = Color.Red
Controls.Add(Button3)
AddHandler Button3.Click, AddressOf Button_Click

End If

If (Button4 Is Nothing) Then


Button4 = New Button
Button4.Size = New Size(80, 30)
Button4.Location = New Point(115, 100)
Button4.Text = "Button4"
Button4.BackColor = Color.Green
Controls.Add(Button4)
AddHandler Button4.Click, AddressOf Button_Click
End If
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

If sender Is Button2 Then


TextBox1.Text = "you clicked Button:2"
End If
If sender Is Button3 Then
TextBox1.Text = "you clicked Button:3"
End If
If sender Is Button4 Then
TextBox1.Text = "you clicked Button:4"
End If
End Sub
End Class

/* Window application for Message Box */

Public Class Form1


Dim r As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim res As Integer
res = MsgBox("This is a message box!", MsgBoxStyle.OkCancel)
If (res = MsgBoxResult.Ok) Then
TextBox1.Text = "you Clicked on OK"
End If
End Sub
End Class

VB application to communicate with two different forms


form-1 creation
Public Class Form1
Dim NW As New Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox1.Text = NW.TextBox1.Text
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
NW.Show()
End Sub
End Class

Form-2 creation
Public Class Form2

End Class

VB application to illustrate check box with button event


Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If (CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked
= True) Then
TextBox1.Text = "You check mark_1&2&3 is checked"
ElseIf (CheckBox1.Checked = True And CheckBox2.Checked = True) Then
TextBox1.Text = "You check mark_1&2 is checked"
ElseIf(CheckBox1.Checked = True And CheckBox3.Checked = True) Then
TextBox1.Text = "You check mark_1&3 is checked"
ElseIf (CheckBox2.Checked = True And CheckBox3.Checked = True) Then
TextBox1.Text = "You check mark_2&3 is checked"
ElseIf (CheckBox1.Checked = True) Then
TextBox1.Text = "You check mark_1 is checked"
ElseIf (CheckBox2.Checked = True) Then
TextBox1.Text = "You check mark_1 is checked"
ElseIf (CheckBox3.Checked = True) Then
TextBox1.Text = "You check mark_1 is checked"
End If
End Sub
End Class

VB application to illustrate radio button


Public Class Form1
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton1.CheckedChanged
TextBox1.Text = "You are Male Student"
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton2.CheckedChanged
TextBox1.Text = "You are Female Student"
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton3.CheckedChanged
TextBox2.Text = "I-Year Student"
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton4.CheckedChanged
TextBox2.Text = "II-Year Student"
End Sub
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton5.CheckedChanged
TextBox2.Text = "III-Year Student"
End Sub
Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton6.CheckedChanged
TextBox2.Text = "IV-Year Student"
End Sub End Class

VB application to illustrate Combo Box


Public Class Form1

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As


System.Object, ByVal e As System.EventArgs) Handles
ComboBox1.SelectedIndexChanged
TextBox1.Text = ComboBox1.SelectedItem
End Sub

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


System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("Govt Emp")
ComboBox1.Items.Add("Business")
ComboBox1.Items.Add("Farmer")
ComboBox1.Items.Add("Private Emp")
ComboBox1.Text = "Profession"
End Sub

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


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

VB application to illustrate List Box


Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim Arr() = {"Wgl", "Kmm", "Krm", "Adb"}
ListBox1.BeginUpdate()
ListBox1.Items.Add("One")
ListBox1.Items.Add("Two")
ListBox1.Items.Add("Three")
ListBox1.Items.Add("Four")
ListBox1.Items.Add("Five")
ListBox1.Items.AddRange(Arr)
ListBox1.SetSelected(1, True)
ListBox1.EndUpdate()
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Text = ListBox1.SelectedItem
End Sub
End Class

VB application to illustrate Checked List Box


Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CheckedListBox1.BeginUpdate()
CheckedListBox1.Items.Add("SSC", True)
CheckedListBox1.Items.Add("Inter", True)
CheckedListBox1.Items.Add("B.Tech", True)
CheckedListBox1.Items.Add("M.Tech", False)
CheckedListBox1.Items.Add("Phd", False)
CheckedListBox1.EndUpdate()
End Sub

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


System.EventArgs) Handles Button1.Click
For Ind = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(Ind, True)
Next
End Sub

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


System.EventArgs) Handles Button2.Click
For Ind = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.GetItemChecked(Ind) = True Then

TextBox1.Text &= CheckedListBox1.Items(Ind)


End If
Next
End Sub

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


System.EventArgs) Handles Button3.Click
For Ind = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(Ind, False)
Next
End Sub
End Class

VB application to illustrate date and time picker


Public Class Form1

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


System.EventArgs) Handles DateTimePicker1.ValueChanged
TextBox1.Text = "Date Selected" & DateTimePicker1.Text
End Sub

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


Handles MyBase.Load
'DateTimePicker1.Format = DateTimePickerFormat.Custom
'DateTimePicker1.CustomFormat = "MMMM dd yyyy hh:mm:ss tt"
End Sub
End Class

/* Window application for Picture box */


Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If (OpenFileDialog1.ShowDialog() <> DialogResult.Cancel) Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub

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


System.EventArgs) Handles Button2.Click
PictureBox1.ClientSize = New Size(300, 300)
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles
PictureBox1.MouseDown
TextBox1.Text = "You clicked at" & e.X & "," & e.Y
End Sub
End Class

/* Window application for Scroll Bars */

Public Class Form1


Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
TextBox1.Text = "H-Pos: " & e.NewValue
End Sub
Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
TextBox1.Text = "V-Pos: " & e.NewValue
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
HScrollBar1.Minimum = 10
HScrollBar1.Maximum = 100
HScrollBar1.Value = 20
End Sub
End Class

Window program to create and display file


Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim fs As New System.IO.FileStream("D:\Testfile.txt", FileMode.Create,
FileAccess.Write)
Dim w As New StreamWriter(fs)
w.BaseStream.Seek(0, SeekOrigin.End)
w.WriteLine("This is a sample file")
w.WriteLine("Test Line")
w.Flush()
w.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim fs As New System.IO.FileStream("D:\Testfile.txt", FileMode.Open,
FileAccess.Read)
Dim r As New StreamReader(fs)
r.BaseStream.Seek(0, SeekOrigin.Begin)
While r.Peek() > -1
TextBox1.Text &= r.ReadLine()
End While
r.Close()
End Sub
End Class

Window program to create dictionary and copy file into that dictionary
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Directory.CreateDirectory(TextBox1.Text)
Catch ex As Exception
MessageBox.Show("Dirctroy not Created" & ex.ToString)
Exit Sub
End Try
MessageBox.Show("Dirctroy Created")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Try
If OpenFileDialog1.ShowDialog <> DialogResult.Cancel Then
File.Copy(OpenFileDialog1.FileName, TextBox1.Text & "\" &
OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("
\")))
' MessageBox.Show("" &
OpenFileDialog1.FileName.Substring(OpenFileDialog1.FileName.LastIndexOf("
\")))
End If
Catch ex As Exception
MessageBox.Show("Not Copied" & ex.ToString)
End Try
MessageBox.Show("File succesfully copied")
End Sub
End Class

Window program to display Graphics shapes


Public Class Form1
Dim g As Graphics
Dim down As Point
Dim up As Point
Dim r As Rectangle
Dim ch As Char
Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ToolStripLabel1.Click
ch = "R"
End Sub
Private Sub ToolStripLabel2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ToolStripLabel2.Click
ch = "E"
End Sub
Private Sub ToolStripLabel3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ToolStripLabel3.Click
ch = "L"
End Sub
Private Sub ToolStripLabel4_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ToolStripLabel4.Click
ch = "B"
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
g = Me.CreateGraphics()
End Sub

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


System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
down = New Point(e.X, e.Y)
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
up = New Point(e.X, e.Y)
r = New Rectangle(Math.Min(up.X, down.X), Math.Min(up.Y, down.Y),
Math.Abs(up.X - down.X), Math.Abs(up.Y - down.Y))
Select Case ch
Case "R"
g.DrawRectangle(Pens.YellowGreen, r)
Case "E"
g.DrawEllipse(Pens.YellowGreen, r)
Case "L"
g.DrawLine(Pens.YellowGreen, up, down)
Case "B"
g.FillEllipse(Brushes.Blue, r)
End Select
End Sub
End Class

/* ADO application for Select Query*/


Imports System.Data.OleDb
Public Class Form1
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
cn = New OleDbConnection("Data
Source=C:\Users\Raghura.B.Naik\Desktop\VB_ADO\EMP_2013.mdb;
Provider=Microsoft.Jet.OLEDB.4.0")
cn.Open()
cmd = New OleDbCommand("select Ename,Job from Emp1", cn)
dr = cmd.ExecuteReader()
While (dr.Read())
TextBox2.Text = dr(0)
TextBox3.Text = dr(1)
End While
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
dr.Close()
cn.Close()
End Sub
End Class

/* ADO application for Insert Query*/


Imports System.Data.OleDb
Public Class Form1
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
cn = New OleDbConnection("Data
Source=C:\Users\Raghura.B.Naik\Desktop\VB_ADO\EMP_2013.mdb;
Provider=Microsoft.Jet.OLEDB.4.0")
cmd = New OleDb.OleDbCommand("insert into Emp1 values(?,?,?,?)", cn)
cmd.Parameters.Add(New OleDb.OleDbParameter("@Sno",
CInt(TextBox1.Text.Trim)))
cmd.Parameters.Add(New OleDb.OleDbParameter("@Sname",
TextBox2.Text.Trim))
cmd.Parameters.Add(New OleDb.OleDbParameter("@Scollege",
TextBox3.Text.Trim))
cmd.Parameters.Add(New OleDb.OleDbParameter("@Sno",
CInt(TextBox4.Text.Trim)))
cmd.Connection.Open()
Dim n As Integer
n = cmd.ExecuteNonQuery()
If n = 1 Then
MessageBox.Show("inserted successfully")
Else
MessageBox.Show("insertion failed")
End If
cmd.Connection.Close()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.ToString)
End Try
cn.Close()
End Sub
End Class

/* ADO application for Update Query*/


Imports System.Data.OleDb
Public Class Form1
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
cn = New OleDbConnection("Data
Source=C:\Users\Raghura.B.Naik\Desktop\VB_ADO\EMP_2013.mdb;
Provider=Microsoft.Jet.OLEDB.4.0")
cmd = New OleDb.OleDbCommand("update Emp1 set sal = sal + " &
Val(TextBox1.Text), cn)
cmd.Connection.Open()
Dim n As Integer
n = cmd.ExecuteNonQuery()
If n > 1 Then
MessageBox.Show("Update successful")
Else
MessageBox.Show("Update failed")
End If
cmd.Connection.Close()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.ToString)
End Try
cn.Close()
End Sub
End Class

/* ADO application for Delete Query*/


Imports System.Data.OleDb
Public Class Form1
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim i As Integer
Dim str As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
cn = New OleDbConnection("Data
Source=C:\Users\Raghura.B.Naik\Desktop\VB_ADO\EMP_2013.mdb;
Provider=Microsoft.Jet.OLEDB.4.0")
cmd = New OleDb.OleDbCommand("delete from emp1 where Eno = " &
Val(TextBox1.Text), cn)
cmd.Connection.Open()
Dim n As Integer
n = cmd.ExecuteNonQuery()
If n = 1 Then
MessageBox.Show("Deleted successfully")
Else
MessageBox.Show("Deletion failed")
End If
cmd.Connection.Close()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.ToString)
End Try
cn.Close()
End Sub
End Class

S-ar putea să vă placă și