260 likes | 327 Views
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
E N D
Introduction to Computing Dr. Nadeem A Khan
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
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
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”
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
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
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
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
2-D Arrays Read about Matrix addition, transposition, and multiplication in the book
Control Arrays • Array of textboxes for example Text1(0) , Text1(1), Text1(2)…….
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
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)
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?
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
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
Control Array Event Procedure • In case of an ordinary text box Sub Text1_GotFocus ( ) … … End Sub
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
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
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
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
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