VWP – Visual & Windows Programming
Unit 3 – Decision Making, Repetition and multiple forms
1. Enter 5 numbers find out highest and second highest. For highest no check it is prime or not and for second highest calculate Fibonacci series.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
Public Class Program1 Dim no(5), no1, no2, flag As Integer Private Sub Program1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For index = 0 To 4 no(index) = Val(InputBox("Enter number " & (index + 1), "Input 5 numbers")) Next TextBox1.Text = "Numbers entered :" + vbNewLine + no(0).ToString + ", " + no(1).ToString + ", " + no(2).ToString + ", " + no(3).ToString + ", " + no(4).ToString + vbNewLine + vbNewLine Array.Sort(no) no1 = no(5) no2 = no(4) TextBox1.Text &= "Largest number : " + no1.ToString + vbNewLine + "Second largest number : " + no2.ToString + vbNewLine + vbNewLine For index = 2 To no1 / 2 If no1 Mod index <> 0 Then flag = 1 Else flag = 0 Exit For End If Next If flag = 1 Then TextBox1.Text &= no1.ToString + " is prime" + vbNewLine + vbNewLine Else TextBox1.Text &= no1.ToString + " is not prime" + vbNewLine + vbNewLine End If Dim a, b, c As Integer a = 0 b = 1 c = a + b TextBox1.Text &= c.ToString Do TextBox1.Text &= ", " + c.ToString a = b b = c c = a + b Loop While c <= no2 End Sub End Class |
2. Create an application with a textbox in which user can enter a sentence then displays
1) Number of vowels
2) Number of spaces
3) Number of digits
4) Number of special symbols
When user press “analysis” button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
Public Class Program2 Dim sentence() As Char Dim vowels, spaces, digits, symbols, length As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click vowels = 0 spaces = 0 digits = 0 symbols = 0 sentence = TextBox1.Text.ToCharArray length = sentence.GetLength(0) For index = 0 To length - 1 If sentence(index) = "a" Or sentence(index) = "e" Or sentence(index) = "i" Or sentence(index) = "o" Or sentence(index) = "u" Or sentence(index) = "A" Or sentence(index) = "E" Or sentence(index) = "I" Or sentence(index) = "O" Or sentence(index) = "U" Then vowels += 1 ElseIf Char.IsWhiteSpace(sentence(index)) Then spaces += 1 ElseIf Char.IsDigit(sentence(index)) Then digits += 1 ElseIf Char.IsSymbol(sentence(index)) Or Char.IsPunctuation(sentence(index)) Or Char.IsSeparator(sentence(index)) Then symbols += 1 End If Next TextBox2.Text = "Vowels = " + vowels.ToString + vbNewLine TextBox2.Text &= "Spaces = " + spaces.ToString + vbNewLine TextBox2.Text &= "Digits = " + digits.ToString + vbNewLine TextBox2.Text &= "Special symbols = " + symbols.ToString End Sub End Class |
3. Take one textbox. Enter paragraph with multiple lines and find out no. of vowels and no. of digits from paragraph.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
Public Class Program3 Dim sentence() As Char Dim vowels, digits, length As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click vowels = 0 digits = 0 sentence = TextBox1.Text.ToCharArray length = sentence.GetLength(0) For index = 0 To length - 1 If sentence(index) = "a" Or sentence(index) = "e" Or sentence(index) = "i" Or sentence(index) = "o" Or sentence(index) = "u" Or sentence(index) = "A" Or sentence(index) = "E" Or sentence(index) = "I" Or sentence(index) = "O" Or sentence(index) = "U" Then vowels += 1 ElseIf Char.IsDigit(sentence(index)) Then digits += 1 End If Next TextBox2.Text = "Vowels = " + vowels.ToString + vbNewLine TextBox2.Text &= "Digits = " + digits.ToString + vbNewLine End Sub End Class |
4. Write a program which will accept a string from user. And then reverse the string without using inbuilt functions for string reverse. Then check whether the string is palindrome or not. Find how many words in string. Also find out how many words starts from “a” character.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
Public Class Program4 Dim str, revstr As String Dim flag, length, words, acount As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click revstr = "" words = 1 acount = 0 str = TextBox1.Text length = Len(str) While length - 1 >= 0 revstr &= str.Chars(length - 1) length -= 1 End While If str = revstr Then TextBox2.Text = "String is palindrome" + vbNewLine Else TextBox2.Text = "String is not palindrome" + vbNewLine End If length = Len(str) If str.Substring(0, 1) = "a" Or str.Substring(0, 1) = "A" Then acount += 1 End If For index = 1 To length - 1 If str.Substring(index, 1) = " " Then words += 1 ElseIf (str.Substring(index, 1) = "a" Or str.Substring(index, 1) = "A") And str.Substring(index - 1, 1) = " " Then acount += 1 End If Next TextBox2.Text &= "Words = " + words.ToString + vbNewLine TextBox2.Text &= "Words starting from a = " + acount.ToString End Sub End Class |
5. Design a form having two text boxes, combo box and a label. Make the validation so that user can enter only numbers in both textboxes, if user has entered both numerical values then make the combo box visible. The combo box has options like ‘ADD’, ‘SUB’,’MUL’ and ‘DIV’. According to user’s choice from combo result will display in label.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
Public Class Program5 Private Sub Program5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox1.Items.Add("ADD") ComboBox1.Items.Add("SUB") ComboBox1.Items.Add("MUL") ComboBox1.Items.Add("DIV") End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Dim no1, no2 As Integer If IsNumeric(TextBox1.Text) Then no1 = Val(TextBox1.Text) End If If IsNumeric(TextBox2.Text) Then no2 = Val(TextBox2.Text) End If If no1 = 0 Or no2 = 0 Then MsgBox("Please enter only numeric values") End If If ComboBox1.SelectedIndex = 0 Then Label1.Text = no1 + no2 ElseIf ComboBox1.SelectedIndex = 1 Then Label1.Text = no1 - no2 ElseIf ComboBox1.SelectedIndex = 2 Then Label1.Text = no1 * no2 ElseIf ComboBox1.SelectedIndex = 3 Then Label1.Text = no1 / no2 End If End Sub End Class |
6. Design a form which has Annual salary with income tax facilities. If monthly salary entered then calculate annual salary. According to this income tax as per below conditions.
Rs.0 – Rs.50000 – No tax
Rs.50000 – Rs.60000 – 10% of annual income
Rs.60000 – Rs.150000 – 20% of annual income
More than Rs.150000 – 30% of annual income
Also print net annual salary after the deduction from tax in label.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
Public Class Program6 Dim tax, annual, monthly, net As Double Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click monthly = Val(TextBox1.Text) annual = monthly * 12 If monthly < 50000 Then tax = 0 ElseIf monthly >= 50000 And monthly < 60000 Then tax = 0.1 ElseIf monthly >= 60000 And monthly < 150000 Then tax = 0.2 Else tax = 0.3 End If tax = tax * annual net = annual - tax TextBox2.Text = "Annual Salary = Rs. " + annual.ToString + vbNewLine TextBox2.Text &= "Tax = Rs. " + tax.ToString + vbNewLine TextBox2.Text &= "Net Salary = Rs. " + net.ToString End Sub End Class |
7. Restrict a textbox to input only digits.
|
Public Class Program7 Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Char.IsDigit(e.KeyChar) = False And e.KeyChar <> Microsoft.VisualBasic.Chr(8) Then e.Handled = True End If End Sub End Class |
8. Create a form with two drop down lists (combo boxes) one for country name and another for President / Head of the country. When user selects a country name from combo box, corresponding President/Head name should be displayed in another combo box.
|
Public Class Program8 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Dim index As Integer index = ComboBox1.SelectedIndex ComboBox2.SelectedIndex = index End Sub Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged Dim index As Integer index = ComboBox2.SelectedIndex ComboBox1.SelectedIndex = index End Sub End Class |
9. Design a form to accept a text from user and then put two text boxes to input word to find and replace. If user clicks on find button, show index of the first occurrence of the word given in find textbox. If user clicks replace button, found word should be replaced with the word given for replace.
|
Public Class Program9 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim index As Integer index = TextBox1.Text.IndexOf(TextBox2.Text) TextBox1.SelectionStart = index TextBox1.SelectionLength = TextBox2.Text.Length TextBox1.Focus() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = TextBox1.Text.Replace(TextBox2.Text, TextBox3.Text) End Sub End Class |
10. Write a program to transfer an item from First Listbox to Second Listbox and from Second Listbox to First.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
Public Class Program10 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If ListBox1.SelectedIndex >= 0 Then ListBox2.Items.Add(ListBox1.SelectedItem) ListBox1.Items.Remove(ListBox1.SelectedItem) Else MsgBox("Select an item first") End If End Sub Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If ListBox2.SelectedIndex >= 0 Then ListBox1.Items.Add(ListBox2.SelectedItem) ListBox2.Items.Remove(ListBox2.SelectedItem) Else MsgBox("Select an item first") End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click While ListBox1.Items.Count > 0 ListBox2.Items.Add(ListBox1.Items.Item(0)) ListBox1.Items.RemoveAt(0) End While End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click While ListBox2.Items.Count > 0 ListBox1.Items.Add(ListBox2.Items.Item(0)) ListBox2.Items.RemoveAt(0) End While End Sub End Class |
11. Build a calculator with all arithmetic functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
Public Class Program11 Dim num As Double Dim op As Char Dim dotflag As Boolean = False Private Sub zero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zero.Click, one.Click, two.Click, three.Click, four.Click, five.Click, six.Click, seven.Click, eight.Click, nine.Click display.Text &= sender.text End Sub Private Sub add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add.Click, minus.Click, multiply.Click, divide.Click num = Val(display.Text) op = sender.text dotflag = False display.Clear() End Sub Private Sub equalto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equalto.Click If op = "+" Then display.Text = num + display.Text End If If op = "-" Then display.Text = num - display.Text End If If op = "x" Then display.Text = num * display.Text End If If op = "/" Then display.Text = num / display.Text End If End Sub Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click display.Clear() dotflag = False End Sub Private Sub dot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dot.Click If dotflag = False Then display.Text &= dot.Text dotflag = True End If End Sub End Class |
12. Print multiplication table into listbox. For multiplication take value using Numeric up down.
|
Public Class Program12 Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged TextBox1.Clear() Dim i As Integer i = NumericUpDown1.Value For index = 1 To 10 TextBox1.Text &= i & " x " & index & " = " & index * i & vbNewLine Next End Sub End Class |
Download All Programs
I hope that you found this article to be helpful.
Please share your thoughts in the Comments Section.
Please like our Facebook page to show your support.
Suggested Reading –
Related