1 / 12

CIS 338: Control Arrays

CIS 338: Control Arrays. Dr. Ralph D. Westfall June, 2011. What Is a Control Array?. it is a group of controls: part of a collection in a .NET form all identified as elements of Me.Controls array each has a different Index e.g., Me.Controls(0) , Me.Controls(1) etc.

meadj
Download Presentation

CIS 338: Control Arrays

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. CIS 338: Control Arrays Dr. Ralph D. Westfall June, 2011

  2. What Is a Control Array? • it is a group of controls: • part of a collection in a .NET form • all identified as elements of Me.Controls array • each has a different Index • e.g., Me.Controls(0) , Me.Controls(1) etc.

  3. Dropped From, Added To VB.NET • control arrays were in VB 6, but not in earlier versions of .NET • however you can set up controls in earlier .NET code that work like VB6 control arrays • link • link2

  4. VB.NET Control Array Code Dim i As Integer For i = 0 To Me.Controls.Count – 1 MsgBox(Me.Controls(i).ToString) Next

  5. Following Slides Need Updating

  6. Control Array Disadvantages • since all members use the same procedure, may need more complicated logic to handle events Private Sub Command1_Click(Index As _ Integer) Select Case Index Case 0 'user clicked cmdTest(0) button Print "zero" Case 1 'user clicked cmdTest(1) button Print "one" End Select End Sub

  7. Creating a Control Array (VB6) • create first control, and at the very first • change the name • change any other properties that you want to be the same on all elements e.g., Text • select it, then [Ctrl] C or Edit>Copy • [Ctrl] V or Edit>Paste to duplicate • click Yes on dialog box • move copied control where you want it

  8. Programming Control Arrays Dim nI as Integer Private Sub Text1_Change(Index As Integer) For nI = Text1.LBound To Text1.UBound If nI = Index Then Text1(nI).BackColor = "&H000000FF" End If Next nI End Sub • LBound is lowest Index in array (usually 0) • UBound is highest Index in array

  9. Using Parallel Arrays Sub Text1_GotFocus(Index As Integer) Label1(Index).Font.Bold = True End Sub • two arrays: Text1 and Label1 • when Text1(0) gets focus, Label1(0) (same Index) becomes bold • can use LostFocus event to remove bold

  10. Adding Controls with Code nI = Text1.UBound Load Text1(nI + 1) Text1(nI + 1).Visible = True Text1(nI + 1).Top = _ Text1(nI).Top + _ Text1(nI).Height + 120 Text1(nI + 1).Left = _ Text1(nI).Left

  11. Deleting Controls with Code Private Sub Command2_Click() Dim nI As Integer nI = Text1.UBound Unload Text1(nI) End Sub • at run time, can only delete items created by code (previous slide) • can delete any items at design time

  12. Advantages of a Control Array • more efficient – uses less memory • all members work with one procedure • to add controls to program when it is running, must use control arrays • easier to change properties via code • can use loops • gets around the limit of 254 control names per form (but who needs that many??)

More Related