1 / 198

INF110 Visual Basic Programming AUBG Springl semester 2011

elroy
Download Presentation

INF110 Visual Basic Programming AUBG Springl semester 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INF110Visual Basic ProgrammingAUBG Spring semester 2011Reference 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 libraryCourse lecturer: Assoc. Prof. Svetla Boytcheva, PhD

  2. INF110 Visual Basic Programming AUBG Springl semester 2011 Lecture 15 Title: Windows Based Applications 3 Practical 3: Procedures Subs and Functions

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

  4. INF110 Visual Basic Programming Windows Applications Demo programs by Schneider, Chapter 04

  5. Fundamentals • Fundamentals of Programming in VB .NET

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

  7. Coffee Shop Application

  8. Public Class Form1 Dim coffee As Double = 0.6 Dim tea As Double = 0.45 Dim water As Double = 0.55 Private Sub Button_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_exit.Click Me.Close() End Sub

  9. Private Sub CheckBox_Tea_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox_Tea.CheckedChanged If CheckBox_Tea.Checked Then NumericUpDown_Tea.Enabled = True NumericUpDown_Tea.Value = 1 Label_Tea.Text = tea Else NumericUpDown_Tea.Enabled = False NumericUpDown_Tea.Value = 0 Label_Tea.Text = "" End If End Sub

  10. Private Sub NumericUpDown_Coffee_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown_Coffee.ValueChanged If NumericUpDown_Coffee.Value = 0 Then NumericUpDown_Coffee.Enabled = False Label_Coffee.Text = "" CheckBox_Coffee.CheckState = CheckState.Unchecked Else Label_Coffee.Text = CStr(coffee * NumericUpDown_Coffee.Value) End If End Sub

  11. Private Sub Button_Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Calc.Click Dim sum As Double = 0 TextBox1.Text = String.Empty TextBox1.Text = "Order:" & System.Environment.NewLine If CheckBox_Tea.Checked Then TextBox1.Text &= NumericUpDown_Tea.Value & " X Tea = " & Label_Tea.Text + System.Environment.NewLine sum += tea * NumericUpDown_Tea.Value End If If CheckBox_Coffee.Checked Then TextBox1.Text &= NumericUpDown_Coffee.Value & " X Coffee = " & Label_Coffee.Text + System.Environment.NewLine sum += coffee * NumericUpDown_Coffee.Value End If If CheckBox_Water.Checked Then TextBox1.Text &= NumericUpDown_Water.Value & " X Water = " & Label_Water.Text + System.Environment.NewLine sum += water * NumericUpDown_Water.Value End If TextBox1.Text &= "----------------------------" & System.Environment.NewLine & "Total:" & CStr(sum) End Sub

  12. Private Sub Clear_Form() Label_Coffee.Text = "" Label_Tea.Text = "" Label_Water.Text = "" NumericUpDown_Coffee.Value = 0 NumericUpDown_Coffee.Enabled = False NumericUpDown_Tea.Value = 0 NumericUpDown_Tea.Enabled = False NumericUpDown_Water.Enabled = False NumericUpDown_Water.Value = 0 CheckBox_Coffee.CheckState = CheckState.Unchecked CheckBox_Tea.CheckState = CheckState.Unchecked CheckBox_Water.CheckState = CheckState.Unchecked TextBox1.Text = String.Empty End Sub

  13. Private Sub Button_New_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_New.Click Clear_Form() End Sub

  14. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Clear_Form() End Sub

  15. Gas Station

  16. Public Class Form1 Dim gas95 As Double = 2.42 Dim gas98 As Double = 2.61 Dim diesel As Double = 2.55 Dim liter_price, total_price As Double Private Sub Button_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click Me.Close() End Sub

  17. Private Sub clear_form() liter_price = gas95 RadioButton_95.Checked = True NumericUpDown1.Value = 0 TextBox1.ReadOnly = True TextBox2.ReadOnly = True TextBox1.Text = 0 TextBox2.Text = liter_price End Sub

  18. Private Sub Button_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Clear.Click clear_form() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load clear_form() End Sub

  19. Private Sub RadioButton_95_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton_95.CheckedChanged If RadioButton_95.Checked Then liter_price = gas95 TextBox2.Text = liter_price TextBox1.Text = NumericUpDown1.Value * liter_price End If End Sub

  20. Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged TextBox1.Text = NumericUpDown1.Value * liter_price End Sub

  21. General procedures • Chapter 4 General Procedures • Training session on user defined procedures within Windows applications • Sub procedures • Function procedures

  22. Sub procedures • Sub procedures

  23. Example 4-1.1 • Problem: Program calculates the sum of two numbers 2 and 3 • Problem Design: Composition of two tasks • Task#1: Explain purpose of program • Task#2: Display numbers and their sum

  24. Example 4-1.1 • Program calculates the sum of two numbers 2 and 3 with no use of user-specified procedures

  25. Example 4-1.1, with no subs Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Display the sum of two numbers Dim num1, num2 As Double lstResult.Items.Clear() 'Explain the task performed by the program lstResult.Items.Add("This program displays a sentence") lstResult.Items.Add("identifying two numbers and their sum.") lstResult.Items.Add("") num1 = 2 num2 = 3 lstResult.Items.Add("The sum of " & num1 & " and " _ & num2 & " is " & num1 + num2 & ".") End Sub

  26. Example 4-1.1 • Program calculates the sum of two numbers 2 and 3 using one only Sub procedure: • ExplainPurpose() to perform task#1: Explain purpose of program

  27. Example 4-1.1

  28. Example 4-1.1 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Display the sum of two numbers Dim num1, num2 As Double lstResult.Items.Clear() ExplainPurpose() lstResult.Items.Add("") num1 = 2 num2 = 3 lstResult.Items.Add("The sum of " & num1 & " and " _ & num2 & " is " & num1 + num2 & ".") End Sub Sub ExplainPurpose() 'Explain the task performed by the program lstResult.Items.Add("This program displays a sentence") lstResult.Items.Add("identifying two numbers and their sum.") End Sub

  29. Example 4-1.2 • Program calculates the sum of two numbers 2 and 3 using two Sub procedures • ExplainPurpose() to perform task#1: Explain purpose of program • Sum(2, 3) to perform task#2: Display numbers and their sum

  30. Example 4-1.2 Same GUI as previous task

  31. Example 4-1.2 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Display the sum of two numbers lstResult.Items.Clear() ExplainPurpose() lstResult.Items.Add("") Sum(2, 3) End Sub Sub Sum(ByVal num1 As Double, ByVal num2 As Double) 'Display numbers and their sum lstResult.Items.Add("The sum of " & num1 & " and " _ & num2 & " is " & (num1 + num2) & ".") End Sub Sub ExplainPurpose() 'Explain the task performed by the program lstResult.Items.Add("This program displays a sentence") lstResult.Items.Add("identifying two numbers and their sum.") End Sub

  32. Example 4-1.3 • Program extension of previous example. • Program calculates the sum of three pairs of two numbers 2,3 4,6 7,8 using same two Sub procedures • ExplainPurpose() to perform task#1: Explain purpose of program • Sum( , ) to perform task#2: Display numbers and their sum

  33. Example 4-1.3

  34. Example 4-1.3 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click 'Display the sum of two numbers lstResult.Items.Clear() ExplainPurpose() lstResult.Items.Add("") Sum(2, 3) Sum(4, 6) : Sum(7, 8) End Sub Sub Sum(ByVal num1 As Double, ByVal num2 As Double) 'Display numbers and their sum lstResult.Items.Add("The sum of " & num1 & " and " _ & num2 & " is " & (num1 + num2) & ".") End Sub Sub ExplainPurpose() 'Explain the task performed by the program lstResult.Items.Add("This program displays a sentence") lstResult.Items.Add("identifying two numbers and their sum.") End Sub

  35. Example 4-1.4 • Program calculates the population density of a state. • Program passes a string and two numbers to CalculateDensity() Sub procedure • State name • State population – number of people • State area in square miles • Sub procedure is called twice

  36. Example 4-1.4

  37. Example 4-1.4 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click 'Calculate the population densities of states lstDensity.Items.Clear() CalculateDensity("Hawaii", 1212000, 6471) lstDensity.Items.Add("") CalculateDensity("Alaska", 627000, 591000) End Sub Sub CalculateDensity(ByVal state As String, ByVal pop As Double, ByVal area As Double) Dim rawDensity, density As Double 'The density (number of people per square mile) 'will be displayed rounded to one decimal place rawDensity = pop / area density = Math.Round(rawDensity, 1) 'Round to one decimal place lstDensity.Items.Add("The density of " & state & " is " & density) lstDensity.Items.Add("people per square mile.") End Sub

  38. Example 4-1.4 Call the sub procedure CalculateDensity("Hawaii", 1212000, 6471) with data for your country

  39. Sub procedures • Variables and Expressions as Arguments

  40. Example 4-1.5 • This is a variation of the 2 numbers addition program. It requests the two numbers as input from the user via text boxes. • Names of actual arguments x, y • Names of formal parameters num1, num2

  41. Example 4-1.5

  42. Example 4-1.5 Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click 'This program requests two numbers and 'displays the two numbers and their sum. Dim x, y As Double x = CDbl(txtFirstNum.Text) y = CDbl(txtSecondNum.Text) Sum(x, y) End Sub Sub Sum(ByVal num1 As Double, ByVal num2 As Double) 'Display numbers and their sum txtResult.Text = "The sum of " & num1 & " and " & num2 _ & " is " & (num1 + num2) & "." End Sub

  43. Example 4-1.6Now or to postpone to lecture 43 “Input/Output” • This is a variation of the population density of a state program. It obtains input from a file DEMOGRAPHICS.TXT whose contents includes state name, population and area each on separate line: Hawaii 1212000 6471 Alaska 627000 591000 • Arguments s, p, a • Parameters state, pop, area

  44. Example 4-1.6

  45. Example 4-1.6 Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click 'Calculate the population densities of states Dim state As String, pop, area As Double Dim s As String, p, a As Double Dim sr As IO.StreamReader = IO.File.OpenText("DEMOGRAPHICS.TXT") lstDensity.Items.Clear() state = sr.ReadLine pop = CDbl(sr.ReadLine) area = CDbl(sr.ReadLine) CalculateDensity(state, pop, area) lstDensity.Items.Add("") s = sr.ReadLine p = CDbl(sr.ReadLine) a = CDbl(sr.ReadLine) sr.Close() CalculateDensity(s, p, a) End Sub ‘ continuation on next slide

  46. Example 4-1.6 (cont.) Sub CalculateDensity(ByVal state As String, _ ByVal pop As Double, ByVal area As Double) 'The density (number of people per square mile) 'will be displayed rounded to one decimal place Dim rawDensity As Double, density As Double rawDensity = pop / area density = Math.Round(rawDensity, 1) 'Round to one decimal place lstDensity.Items.Add("The density of " & state & " is " & density) lstDensity.Items.Add("people per square mile.") End Sub

  47. Sub procedures • Sub procedures calling other sub procedures

  48. Example 4-1.7 • This program contains two procedures: • FirstPart() • SecondPart() • Sub procedure FirstPart() calls Sub procedure SecondPart().

More Related