Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.

Similar presentations


Presentation on theme: "1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice."— Presentation transcript:

1 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice Hall, Pearson Education Inc., 7th Ed. 2008, 6th Ed. 2006 Liberty J., Learning Visual Basic.NET, O’Reilly, 2002 Any Visual Basic book available in AUBG library Course lecturer: Assoc. Prof. Svetla Boytcheva, PhD

2 2 INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 14 Title: Windows Based Applications 2 ListBox control Practical 2: Data, Assignment

3 3 Lecture Contents: §Windows applications – basic concepts l OOP l Event driven Programming §Form §Controls – label, edit box, button §Procedures - Event handlers §Visual Studio IDE §Three steps to develop Windows applications §Demo programs – Input/Output

4 4

5 5 Windows Applications Using the Visual Studio.NET Development Environment INF110 Visual Basic Programming

6 6 VBasic Applications Console Applications Windows Applications Web (ASP.NET) Applications

7 7 Fundamentals of Windows Applications Object Oriented Programming –Classes + Objects = Programs Event Driven Programming –The flow of the program is controlled by the user

8 8 Visual Basic is an event-driven language Therefore, a Visual Basic program is made up of many “subprograms”, known as event handlers. Each subprogram: has its own program code, can be executed independently, at the same time each can be linked together in one way or another.

9 9 VB.NET displays a Window-style screen (called a form) with: boxes into which users type information and/or in which users edit information; buttons that users click to initiate actions. The boxes and buttons are referred to as controls. The most common controls are: - Text boxes - Labels - Buttons - List boxes

10 10 The three steps in creating a VB.NET program 1.Create the interface; that is, generate, position, and size the objects. 2.Set properties; that is, configure the appearance of the objects. 3.Write the code that executes when events occur.

11 11 Solution explorer window Form window Toolbox window Properties window Menu bar Tool bar Main area

12 12 The Toolbox contains a list of controls (icons). Using the Toolbox, you may add controls to your “user interface”, i.e. you may develop a screen for your program.

13 13 Chapter 2 - VB 2008 by Schneider13 Auto Hide Hides Toolbox when not in use Vertical push pin icon indicates auto hide is disabled. Click the push pin to make it horizontal and enable auto hide. Push pin

14 14 Positioning and Aligning Controls VB provides the following tools: Proximity Lines – short line segments that help you place controls a comfortable distance from each other and from the sides of a form Snap lines – horizontal and vertical line segments that help you align controls

15 15 Chapter 2 - VB 2008 by Schneider15 Positioning Controls Proximity line

16 16 Chapter 2 - VB 2008 by Schneider16 Aligning Controls Snap line

17 17 Chapter 2 - VB 2008 by Schneider17 Aligning Controls Snap line

18 18 The ListBox control another control that has a number of lines to be displayed instead of a single line as with a text box.

19 19 Numeric Data How to show numeric data on the screen? One way to show a number on the screen is to use a list box - another control that has a number of lines to be displayed instead of a single line as with a text box. If n is a number or in other words a numeric literal, (instead of txtBox.Text = CStr(n) ) the statement lstBox.Items.Add(n) displays the number n as the last item in the list box. Add() is called a method. lstBox.Items.Clear() erases all items displayed in a list box. Clear() is another method.

20 20 Numeric Data – Literals How to show literal numeric data on the screen? Arguments of method Add() specified as literals Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click lstResults.Items.Clear() lstResults.Items.Add(3 + 2) lstResults.Items.Add(3 - 2) lstResults.Items.Add(3 * 2) lstResults.Items.Add(3 / 2) lstResults.Items.Add(3 \ 2) lstResults.Items.Add(3 ^ 2) lstResults.Items.Add(2 * (3 + 4)) End Sub

21 21 Alternative: Use With block Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click With lstResults.Items.Clear().Add(3 + 2).Add(3 - 2).Add(3 * 2).Add(3 / 2).Add(3 \ 2).Add(3 ^ 2).Add(2 * (3 + 4)) End With End Sub

22 22 Numeric Data – Variables How to show variable contents numeric data on the screen? Arguments of method Add() specified as variables As well as literal values, may also use variable names with list box functions (better to say methods). E.g. lstResults.Items.Add(timeElapsed) where timeElapsed is the name of some variable.

23 23 E.g. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim a As Double Dim b As Double = 3 a = 5 lstResults.Items.Clear() lstResults.Items.Add(a) lstResults.Items.Add(b) lstResults.Items.Add(a * (2 + b)) End Sub

24 24 Built-in Numeric Functions Math.Sqrt() calculates the square root of a number. Math.Int() finds the greatest integer number less than or equal to a value, specified as an argument. Math.Round() rounds a value. Plus many others. Math.Sqrt(9) returns 3 Int(9.7) returns 9 Math.Round(2.7) returns 3

25 25 Symbolic Data – string variables and string literals How to show symbolic data on the screen? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim today As String today = “Friday" lstOutput.Items.Clear() lstOutput.Items.Add("hello") lstOutput.Items.Add(today) End Sub

26 26 Symbolic Data – string variables and string literals How to show symbolic data on the screen? Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim today As String today = “Friday" With lstOutput.Items.Clear().Add("hello").Add(today) End With End Sub

27 27 String Properties and Methods: Length - property ToUpper – method Trim – method ToLower - method IndexOf - methodSubstring - method If Str is a string Str.Length is the number of characters in the string Str.ToUpper converts all characters to uppercase Str.ToLower converts all characters to lowercase Str.Trim removes all spaces from the front and back

28 28 Str.Substring(m, n) picks out the substring beginning at character in position m and having length of n. The start leftmost position is numbered zero /0/. “abcdefghijklmnopqrstuvwxyz” m = 4 n = 15 gives “efghijklmnopqrs” If n is omitted, the n is taken to mean the end of the string.

29 29 Str.IndexOf(SubStr) returns the value -1 if SubStr is not a substring of Str, otherwise returns the start position in Str of the substring SubStr.

30 30 "Visual".Length is:6 "Visual".ToUpper is:VISUAL "123 Hike".Length is:8 "123 Hike".ToLower is:123 hike "a" & " bcd ".Trim & "efg" is:abcdefg Example

31 31 "fanatic".Substring(0, 3) is:fan "fanatic".IndexOf("ati") is:3 "fanatic".Substring(4, 2) is:ti "fanatic".IndexOf("a") is:1 "fanatic".Substring(4) is:tic "fanatic".IndexOf("nt") is:–1 Example (cont.)

32 32 Using Text Boxes for Input and Output The contents of a text box is always a string Input example strVar = txtBox.Text assigns to variable strVar the value of the Text field. Output example txtBox.Text = strVar

33 33 Concatenation Combining two strings to make a new string - & quote1 = “To be, " quote2 = “or not to be." quote = quote1 & quote2 txtOutput.Text = quote & “ That is the question." Displays To be, or not to be. That is the question.

34 34 Example – parses a name Dim fullName, firstName, lastName As String Dim n As Integer fullName = txtName.Text n = fullName.IndexOf(" ") firstName = fullName.Substring(0, n) lastName = fullName.Substring(n + 1) With lstResults.Items.Clear().Add("First name: " & firstName).Add(“Last name has " & lastName.Length & “ letters.") End With

35 35 Data Conversion Because the contents of a text box is always a string, sometimes you must convert the input or output numVar = CDbl(txtBox.Text) txtBox.Text = CStr(numVar) CDbl - Converts a String to a Double CStr - Converts a number to a string

36 36 Also, CInt() converts a String to an Integer

37 37 The Empty String The string "", which contains no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = ""

38 38 Initial Value of a String By default the initial value is Nothing, null reference Strings can be given a different initial value as follows: Dim today As String = “Friday" Or assigned a value at runtime

39 39 Dim n As Integer Try n = CInt(TextBox1.Text) Catch ex As Exception MessageBox.Show("please, insert integer", "textbox1", MessageBoxButtons.OK, MessageBoxIcon.Error) Return End Try If CheckBox1.Checked Then n = n * n End If Label1.Text = n

40 Dim s As String = TextBox2.Text ListBox1.Items.Clear() For Each c In s ListBox1.Items.Add(c) Next 40

41 41 Windows Applications Demo programs by Schneider, Chapter 03 No 3.5 (input/output) INF110 Visual Basic Programming

42 42 Fundamentals Fundamentals of Programming in VB.NET

43 43 Numbers

44 44 Example 3.3-1 Program applies each of the five arithmetic operators to operands numbers 3 and 2. Design: a Form,2 controls: List Box, Button ObjectPropertySetting Form1Text3-3-1 lstResults btnComputeTextCompute

45 45 Example 3.3-1

46 46 Example 3.3-1 Option Strict On Public Class Form1 Inherits System.Windows.Forms.Form ‘Windows Form Designer generated Code Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click lstResults.Items.Clear() lstResults.Items.Add(3 + 2) lstResults.Items.Add(3 - 2) lstResults.Items.Add(3 * 2) lstResults.Items.Add(3 / 2) lstResults.Items.Add(3 ^ 2) lstResults.Items.Add(2 * (3 + 4)) End Sub Private Sub lstResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResults.SelectedIndexChanged End Sub End Class

47 47 Example 3.3-1 Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click lstResults.Items.Clear() lstResults.Items.Add(3 + 2) lstResults.Items.Add(3 - 2) lstResults.Items.Add(3 * 2) lstResults.Items.Add(3 / 2) lstResults.Items.Add(3 ^ 2) lstResults.Items.Add(2 * (3 + 4)) End Sub

48 48 Example 3.3-1 Run the program. Test the program. Modify the program and rerun it again.

49 49 Numbers in scientific notation b * 10 r is usually written as bEr ==================================== 1.4*10^9 1.4E+9 1.4E9 1400000000 ==================================== 3*10^-7 3E-7.0000003 0.0000003

50 50 Example 3.3-2 Program illustrates scientific notation. Same design as example 3.3-1 Design: a Form,2 controls: List Box, Button ObjectPropertySetting Form1Text3-3-2 lstResults btnComputeTextCompute

51 51 Example 3.3-2

52 52 Example 3.3-2 Priavate Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click lstResults.Items.Clear() lstResults.Items.Add(1.2 * 10 ^ 34) lstResults.Items.Add(1.2 * 10 ^ 8) lstResults.Items.Add(1.2 * 10 ^ 3) lstResults.Items.Add(10 ^ -20) lstResults.Items.Add(10 ^ -2) End Sub

53 53 Example 3.3-2m Using With … End With Priavate Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click With lstResults.Items.Clear().Add(1.2 * 10 ^ 34).Add(1.2 * 10 ^ 8).Add(1.2 * 10 ^ 3).Add(10 ^ -20).Add(10 ^ -2) End With End Sub

54 54 Example 3.3-2 Run the program. Test the program. Modify the program and rerun it again.

55 55 Variables variables

56 56 Example 3.3-3 Program displays default value of a variable and the value of an expression. Same design as example 3.3-1 Design: Form, ListBox, Button ObjectPropertySetting Form1Text3-3-3 lstResults btnComputeTextCompute

57 57 Example 3.3-3

58 58 Example 3.3-3 Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim a As Double Dim b As Double = 3 lstResults.Items.Clear() lstResults.Items.Add(a) lstResults.Items.Add(b) a = 5 lstResults.Items.Add(a * (2 + b)) End Sub

59 59 Example 3.3-3 Run the program. Test the program. Modify the program and rerun it again.

60 60 Built-in Functions Math.Sqrt Int Math.Round

61 61 Example 3.3-4 Program evaluates each of the functions for a specific input given by the value of a variable n Same design as example 3.3-1 Design: Form, ListBox, Button ObjectPropertySetting Form1Text3-3-4 lstResults btnEvaluateTextEvaluate

62 62 Example 3.3-4 Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvaluate.Click Dim n As Double Dim root As Double n = 6.76 root = Math.Sqrt(n) With lstResults.Items.Clear().Add(root).Add(Int(n)).Add(Math.Round(n, 1)) End With End Sub

63 63 Example 3.3-4 Run the program. Test the program. Modify the program and rerun it again.

64 64 Example 3.3-5 Program evaluates each of the functions for argument specified as an expression. Same design as example 3.3-4 Design: Form, ListBox, Button ObjectPropertySetting Form1Text3-3-5 lstResults btnEvaluateTextEvaluate

65 65 Example 3.3-5 Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvaluate.Click Dim a As Double Dim b As Double a = 2 b = 3 With lstResults.Items.Clear().Add(Math.Sqrt(5 * b + 1)).Add(Int(a ^ b + 0.8)).Add(Math.Round(a / b, 3)) End With End Sub

66 66 Example 3.3-5 Run the program. Test the program. Modify the program and rerun it again.

67 67 Strings

68 68 Example 3-4.1 Variables and Strings Program shows how assignment statement and the.Add method are used with strings. Design: Form, ListBox, Button ObjectPropertySetting Form1Text3-4-1 lstOutput btnDisplayTextDisplay Strings

69 69 Example 3-4.1 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim today As String today = “Friday" With lstOutput.Items.Clear().Add("hello").Add(today) End With End Sub

70 70 Example 3-4.1 Run the program. Test the program. Modify the program and rerun it again.

71 71 Example 3-4.2 Using text boxes for Input and Output Program computes the sum of two numbers supplied by the user. Design: Form, 3 Labels, 3 Text Boxes, Button Examine the ReadOnly property for Text Boxes

72 72 Example 3-4.2 Text Box controls The ReadOnly property: Controls whether the text in the edit control can be changed by the user or not ReadOnly: True /gray background/ text can not be changed by the user ReadOnly: False /white background/ text can be changed by the user

73 73 Example 3-4.2

74 74 Example 3-4.2 Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim sum As Double sum = CDbl(txtFirstNum.Text) + CDbl(txtSecondNum.Text) txtSum.Text = CStr(sum) End Sub

75 75 Example 3-4.2 Run the program. Test the program. Modify the program and rerun it again.

76 76 Example 3-4.3 Concatenation Program illustrates concatenation of strings. Design: Form, Text Box, Button

77 77 Example 3-4.3

78 78 Example 3-4.3 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim quote1, quote2, quote As String quote1 = "The ballgame isn't over, " quote2 = "until it's over." quote = quote1 & quote2 txtOutput.Text = quote & " Yogi Berra " End Sub

79 79 Example 3-4.3 Run the program. Test the program. Modify the program and rerun it again.

80 80 Example 3-4.4 Concatenation Program concatenates a string with a number. Design: Form, Text Box, Button

81 81 Example 3-4.4

82 82 Example 3-4.4 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim str As String, numOfKeys As Double str = "The piano keyboard has " numOfKeys = 88 txtOutput.Text = str & numOfKeys & " keys." End Sub

83 83 Example 3-4.4 Run the program. Test the program. Modify the program and rerun it again. How many keys has a standard PC keyboard?

84 84 Example 3-4.5 String Properties and Methods: Length property ToUpper, ToLower, Trim, IndexOf, Substring methods Program uses variables and expressions with properties and methods. Design: Form, Text Box, Button

85 85 Example 3-4.5 Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvaluate.Click Dim str1, str2 As String str1 = "Quick as " str2 = "a wink" With lstResults.Items.Clear().Add(str1.Substring(0, 7)).Add(str1.IndexOf("c")).Add(str1.Substring(0, 3)).Add((str1 & str2).Substring(6, 6)).Add((str1 & str2).ToUpper).Add(str1.Trim & str2).Add("The average " & str2.Substring(str2.Length - 4) & " lasts.1 second.") End With End Sub

86 86 Example 3-4.5

87 87 Example 3-4.5 Run the program. Test the program. Modify the program and rerun it again. Change the following statements:.Add((str1 & str2).ToUpper).Add(str1.Trim & str2) To.Add((str1 & str2).ToUpper() ).Add(str1.Trim() & str2) Moral: Subs and Functions with no arguments may be called with or without parentheses ()

88 88 Example 3-4.6 String Properties and Methods: Length property ToUpper, ToLower, Trim, IndexOf, Substring methods Program parses a name. Design: Form, Text Box, Button

89 89 Example 3-4.6

90 90 Example 3-4.6 Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click Dim fullName, firstName, lastName As String Dim n As Integer fullName = txtName.Text n = fullName.IndexOf(" ") firstName = fullName.Substring(0, n) lastName = fullName.Substring(n + 1) With lstResults.Items.Clear().Add("First name: " & firstName).Add("Your last name has " & lastName.Length & " letters.") End With End Sub

91 91 Example 3-4.6 Run the program. Test the program. Modify the program and rerun it again.

92 92 Example 3-4.7 Internal documentation Program rewrites example 3-4-6 with inclusion of comments. Design: Form, Text Box, Button

93 93 Example 3-4.7 Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click 'Determine a person's first name and the length of the second name Dim fullName, firstName, lastName As String Dim n As Integer 'Location of the space separating the two names fullName = txtName.Text n = fullName.IndexOf(" ") firstName = fullName.Substring(0, n) lastName = fullName.Substring(n + 1) 'Display the desired information in a list box With lstResults.Items.Clear().Add("First name: " & firstName).Add("Your last name has " & lastName.Length & " letters.") End With End Sub

94 94 Example 3-4.7 Run the program. Test the program. Modify the program and rerun it again.

95 95 Task A Text Box Walkthrough Create a Text Box control Activate the properties window Trace the effect of modifying some properties: –Appearance: Text, BackColor, ForeColor, Font, BorderStyle –Design: (Name) –Layout: Location, Size

96 96 Task A List Box Walkthrough Create a List Box control Activate the properties window Trace the effect of modifying some properties: –Appearance: Text, BackColor, ForeColor, Font, BorderStyle –Design: (Name) –Layout: Location, Size

97 97 Task A Label Walkthrough Create a Label control Activate the properties window Trace the effect of modifying some properties: –Appearance: Text, BackColor, ForeColor, Font –Design: (Name) –Layout: Location, Size

98 98 Task A Button Walkthrough Create a Button control Activate the properties window Trace the effect of modifying some properties: –Appearance: Text, TextAlign, BackColor, ForeColor, Font –Design: (Name) –Layout: Location, Size

99 99 Questions?

100 100 Thank You For Your Attention!


Download ppt "1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice."

Similar presentations


Ads by Google