1 / 26

Introduction to Computing

Introduction to Computing. Dr. Nadeem A Khan. Lecture 28. Lab and Lab quiz on Thursday. Practice Problem. Two Dimensional Arrays. 2-D Arrays. Declaration formats: Dim arrayname(m1 To n1, m2 To n2 ) As varType ReDim arrayname(m1 To n1, m2 To n2 ) As varType

Download Presentation

Introduction to Computing

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. Introduction to Computing Dr. Nadeem A Khan

  2. Lecture 28

  3. Lab and Lab quiz on Thursday

  4. Practice Problem

  5. Two Dimensional Arrays

  6. 2-D Arrays • Declaration formats: Dim arrayname(m1 To n1, m2 To n2) As varType ReDim arrayname(m1 To n1, m2 To n2) As varType => Scope (Form-level/ Local) established in the same way as for 1-D arrays

  7. 2-D Arrays • Declaration format examples: Private Dim rm(1 To 4, 1 To 4) As String Public Dim rm( ) As String Dim rm( ) As String ReDim rm(1 To 4, 1 To 4) ReDim rm(1 To 4, 3 To 4) As String

  8. 2-D Arrays • Value assignment: Dim student(1 To 2, 1 To 3) as String Let student(1, 3) = “Aslam” Let student(2, 1) = “Bushra”

  9. 2-D Arrays • Example 1: Program to store and access from the data file DISTANCE.TXT having inter-city distances 0, 2054, 802, 738 2054, 0, 2786, 2706 802, 2786, 0, 100 738, 2706, 100, 0

  10. 2-D Arrays Example 1 (Contd.) Dim rm(1 To 4, 1 To 4) As Single ‘General Declaration Sub Form_Load Dim row As Integer, col As Integer Open “DISTANCE.TXT” For Input As #1 For row=1 To 4 For col=1 To 4 Input #1, rm(row, col) Next col Next row Close #1 End Sub

  11. 2-D Arrays Example 1 (Contd.) Sub Command1_Click( ) Dim row As Integer, col As Integer Let row=Val(Text1.Text) Let col = Val(Text2.Text) If (row>=1 And row<=4) And (col>=1 And col<=4) Then Call ShowMileage(rm(), row, col) Else MsgBox ”Range 1 to 4 only”, , “Error” End If Text1.SetFocus End Sub

  12. 2-D Arrays Example 1 (Contd.) Sub ShowMileage (rm( ) As Single, row As Integer, _ col As Integer) Picture1.Cls Picture1.Print “The road mileage is:” rm(row, col) End Sub

  13. 2-D Arrays Read about Matrix addition, transposition, and multiplication in the book

  14. Control Arrays

  15. Control Arrays • Array of textboxes for example Text1(0) , Text1(1), Text1(2)…….

  16. Control Arrays • How to create an array of a control? (at design time) 1. Add one instance of the desired control on the form 2. Set the index property of this control to 0 3. Set other properties that will be shared by all 4. Copy and Paste as many instances you want to create

  17. Control Arrays • How to work with them? All are valid statements: Let Text1(0).Text = “Hello” Let Label1(6).Caption= “Department 6” Let lblMonth(4).Visible =True Let lblMonth(i).Top= lblMonth(i-1).Top+460 (assuming i variable have some value)

  18. Control Arrays • A departmental store has five departments. Write a program to request the amount of sales for each department and display the total sales?

  19. Control Arrays Example 1 Sub Form_Load ( ) Dim depNum As Integer For depNum=0 To 4 lblDepart(depNum).Caption = “Depart”+ Str(depNum+1) Next depNum End Sub

  20. Control Arrays Example 1 Sub Command1_Click Dim depNum As Integer, sales As Single sales=0 For depNum=0 To 4 Let sales =sales+Val(txtSales(depNum).Text) Next depNum Picture1.Cls Picture1.Print “Total sales:” + Format$(sales, “Currency”) End Sub

  21. Control Array Event Procedure • In case of an ordinary text box Sub Text1_GotFocus ( ) … … End Sub

  22. Control Array Event Procedure • In case of an array of text boxes Sub Text1_GotFocus (Index As Integer ) … … End Sub => Index will tell which element of Text1 array got the focus

  23. Control Array Event Procedure • Use index for assigning separate actions in each case e. g: Sub Text1_GotFocus (Index As Integer ) Select Case Index Case 0 action when Text1(0) gets the focus Case 1 action when Text1(1) gets the focus . . End Select End Sub

  24. Control Arrays • How to create an array of a control? (at run-time) 1. Add one instance of the desired control on the form at the design time 2. Remaining elements can be created at run-time

  25. Control Arrays • Creating an array of a control at runtime: Load controlName(num) • creates the element controlName(num) • copies all the properties of controlName(0) except visibility • visibility is set to 0

  26. Control Arrays Example: Sub Form_Load ( ) Dim i As Integer, monthNames As String monthNames = “FebMarAprMayJunJulAugSepOctNovDec” For i=1 To 11 Load lblMonth(i) Load txtInfo(i) lblMonth(i).Top =lblMonth(i-1).Top+txtInfo(0).Height txtInfo(i).Top = txtInfo(i-1).Top + txtInfo(0).Height lblMonth(i).Caption=Mid(monthNames, 3*i-2 , 3) lblMonth(i).Visible =True txtInfo(i).Visible=True Next i End Sub

More Related