|
|
VB & VB.Net Practical Questions |
|
|
& Answers |
|
1. Write a program to manage the general functions of a simple calculator. |
||
|
2. Create a program that demonstrates the three states of a check box. Place a checkbox control to the form and set ThreeState property to True. Set Text property of the control to “Do you think India will defeat Australia ?”.Then select CheckStateChanged event for the checkbox to evaluate the corresponding state. |
||
|
3. Write a program to change the background colors of controls in a form using For Each…Next statement. |
||
|
4. Program to initialize a System.Object array with item values and then add the entire collection to the CheckedListBox using Items.AddRange() within the Form Load function. When the Add Button is clicked, add the selected items from the CheckedListBox to the ComboBox. |
||
|
5. Create two context menu strips, one for Form control(Minimize, Maximize, Restore and Close) and another for textbox formatting (color and font). |
||
|
6. Store and retrieve a text document using RichTextbox control |
||
|
7. Create a customized (your own features) web browser. |
||
|
8. Write procedure and code to Create a usercontrol for validating Email address. |
||
|
10.Using the previous database add all student names into a ComboBox. The entire record of the student name selected in the ComboBox, has to be displayed. |
||
|
Answer to Question-1 Public Class SimpleCalculator
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
txtResult.Text = Val(txtNum1.Text) + Val(txtNum2.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click
txtResult.Text = Val(txtNum1.Text) - Val(txtNum2.Text)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button3.Click
txtResult.Text = Val(txtNum1.Text) * Val(txtNum2.Text)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button4.Click
txtResult.Text = Val(txtNum1.Text) / Val(txtNum2.Text)
End Sub
End Class
Answer to Question-2 Public Class CheckBoxThreeState
Private Sub CheckBox1_CheckStateChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles CheckBox1.CheckStateChanged
Select Case (CheckBox1.CheckState) Case CheckState.Unchecked MsgBox("No, India will not defeat Austraila ") Case CheckState.Checked MsgBox("Yes, India will defeat Austraila ") Case CheckState.Indeterminate MsgBox("Cannot Say ") End Select
End Sub End Class
Answer to Question-3
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load
For Each thisControl As System.Windows.Forms.Control _ In Me.Controls thisControl.BackColor = System.Drawing.Color.LightBlue Next thisControl End Sub End Class Answer to Question-4 Public Class CheckedlistandCombo
Private Sub CheckedlistandCombo_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim ItemObject(9) As System.Object ItemObject(0) = "C++" ItemObject(1) = "Java" ItemObject(2) = "VisualBasic" ItemObject(3) = "C# Dot et" ItemObject(4) = "ASP.Net" ItemObject(5) = "PHP" ItemObject(6) = "XML" ItemObject(7) = "Flash" ItemObject(8) = "Photoshop" ItemObject(9) = "HTML" CheckedListBox1.Items.AddRange(ItemObject) End Sub
Private Sub add() Dim i As Integer For i = 0 To (CheckedListBox1.Items.Count - 1) If CheckedListBox1.GetItemChecked(i) = True Then ComboBox1.Items.Add(CheckedListBox1.Items.Item(i)) End If Next
End Sub
Private Sub Butto1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click add() End Sub
End Class
Answer to Question-5
Public Class Contextmenu
Private Sub MinimizeToolStripMenuItem_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles MinimizeToolStripMenuItem.Click Me.WindowState = FormWindowState.Minimized End Sub
Private Sub MaximizeToolStripMenuItem_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles MaximizeToolStripMenuItem.Click Me.WindowState = FormWindowState.Maximized End Sub
Private Sub RestoreToolStripMenuItem_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles RestoreToolStripMenuItem.Click Me.WindowState = FormWindowState.Normal End Sub
Private Sub BackColorToolStripMenuItem1_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles BackColorToolStripMenuItem1.Click ColorDialog1.ShowDialog() Me.BackColor = ColorDialog1.Color End Sub
Private Sub ForeColorToolStripMenuItem_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles ForeColorToolStripMenuItem.Click ColorDialog1.ShowDialog() Me.ForeColor = ColorDialog1.Color End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click Application.Exit() End Sub
Private Sub FontToolStripMenuItem_Click_1(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click FontDialog1.ShowDialog() TextBox1.Font = FontDialog1.Font End Sub
Private Sub ColorToolStripMenuItem_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click ColorDialog1.ShowDialog() TextBox1.ForeColor = ColorDialog1.Color End Sub
End Class
Answer to Question-6 Public Class Ritchtextbox
Private Sub Ritchtextbox_Load(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles MyBase.Load RichTextBox1.SelectionBullet = True RichTextBox1.SelectionIndent = 8 RichTextBox1.SelectionHangingIndent = 15 RichTextBox1.SelectionRightIndent = 5 End Sub
Public Sub SaveMyFile() Dim saveFile1 As New SaveFileDialog() saveFile1.DefaultExt = "*.rtf" saveFile1.Filter = "RTF Files|*.rtf" If (saveFile1.ShowDialog() = _ System.Windows.Forms.DialogResult.OK) _ And (saveFile1.FileName.Length > 0) Then RichTextBox1.SaveFile(saveFile1.FileName) End If
End Sub Public Sub LoadMyFile() Dim openFile1 As New OpenFileDialog() openFile1.DefaultExt = "*.rtf" openFile1.Filter = "RTF Files|*.rtf" If (openFile1.ShowDialog() = _ System.Windows.Forms.DialogResult.OK) _ And (openFile1.FileName.Length > 0) Then RichTextBox1.LoadFile(openFile1.FileName) End If End Sub
Private Sub cmdopen_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdopen.Click LoadMyFile() End Sub
Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdsave.Click SaveMyFile() End Sub End Class
Answer to Question-7
Public Class WebBrowser Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click WebBrowser1.Navigate(cmburl.Text) End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As _ System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
cmburl.Text = WebBrowser1.Url.ToString cmburl.Items.Add(cmburl.Text)
End Sub End Class Answer to Question-8 Create a WindowsControl application named EmailValidator which contains a textbox named txtEmail and create a public function named IsValid() with return type Boolean, as shown below. Public Class EmailValidator
Public Function IsValid() As Boolean If (txtEmail.Text <> "") Then
If
txtEmail.Text.IndexOf("@")
< 0
Or _
And txtEmail.Text.Length - 6 <> txtEmail.Text.IndexOf(".") _ And txtEmail.Text.Length - 3 <> txtEmail.Text.IndexOf(".")) _ Then If txtEmail.Text.IndexOf(".") > 0 And _
txtEmail.Text.IndexOf(".") < _ txtEmail.Text.IndexOf("@") Then Return True End If Return False End If End If Return True End Function End Class
Then compile the code and test it. After that create another windows application and add the EmailValidator control on it. Then add a button control on your form and the codes in click events are shown below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click If EmailValidator1.IsValid() = False Then MsgBox("INvalid EmailId")
End If End Sub
Answer to Question-9
Create a SQLServer Data Base named School and having a table named Student which contains the fields Rollnumber(varchar(12)),Name(varchar(20)) and Mark(Float).
The following are the VB.NET Procedure to insert records to the table Student.
Imports System.Data Imports System.Data.SqlClient Public Class Form1
Dim cnn as New SqlConnection
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load connstr As String = "Data Source=SYSTEM-9B98B862\SQLEXPRESS; _ Initial Catalog=School;Integrated Security=True" 'Initialize the connection cnn = New SqlConnection(connstr) 'Oppen connection cnn.Open() End Sub
Private Sub clear() txtname.Text = "" txtmark.Text = "" txtroll.Text = "" txtroll.Focus() End Sub
Private Sub cmdadd_Click_1(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles cmdadd.Click Dim constr As String Dim cmd As SqlCommand If txtname.Text <> "" And txtroll.Text <> "" And _ txtsalary.Text <> "" Then constr = "insert Student (Rollnumber, Name, Mark) _ values( @roll,@name,@mark)" cmd = New SqlCommand(constr, cnn) cmd.Parameters.Add("roll", SqlDbType.NChar).Value = txtroll.Text cmd.Parameters.Add("name", SqlDbType.NChar).Value = txtname.Text cmd.Parameters.Add("mark", SqlDbType.Float).Value = txtmrk.Text
cmd.ExecuteNonQuery() cmbroll.Refresh() End If clear() End Sub End Class
Answer to Question-10 Imports System.Data Imports System.Data.SqlClient Public Class FrmStudent Dim cnn as New SqlConnection
Private Sub FrmStudent_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load connstr As String = "Data Source=SYSTEM-9B98B862\SQLEXPRESS; _ Initial Catalog=School;Integrated Security=True" 'Initialize the connection cnn = New SqlConnection(connstr) 'Oppen connection cnn.Open() setcmb() End Sub
Private Sub setcmb() Dim str As String Dim cmd As SqlCommand str = "select rollnumber from Studentdata" cmd = New SqlCommand(str, cnn) Dim reader As SqlDataReader = cmd.ExecuteReader() If cmbroll.Items.Count > 0 Then cmbroll.Items.Clear() cmbroll.Text = "" End If While reader.Read cmbroll.Items.Add(reader(0)) End While End Sub
Private Sub cmbroll_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbroll.SelectedIndexChanged cmbroll.Refresh() Dim constr As String Dim cmdselect As SqlCommand cnn.Close() cnn.Open() constr = "select * from Studentdata where Rollnumber=@roll" cmdselect = New SqlCommand(constr, cnn) cmdselect.Parameters.Add("roll", SqlDbType.NChar).Value = _ cmbroll.Text Dim readercmb As SqlDataReader = cmdselect.ExecuteReader() readercmb.Read() While readercmb.Read() txtname.Text = readercmb(1) txtmark.Text = readercmb(2) End While readercmb.Close() End Sub
End Class
|
||