CannibalSmith ([info]cannibalsmith) rakstīja [info]koderi kopienā,
@ 2007-04-15 16:22:00

Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Garastāvoklis: angry

Control Arrays in Visual Basic .NET
Hyaaargh!! Kāpēc VB.NET nav control arrays!? Situācija: man ir kaudze ar ProgressBar'iem - Progress1, Progress2, utt.

Me.Controls("Progress" & index).Maximum = whatever
Error: Maximum is not a member of Control.

Kā lai eleganti sanumurē viņus? Es negribu darīt šādi:

Private Progresi(99) as ProgressBar
Sub Form_Load(...)
    Progresi(0) = Progress1
    Progresi(1) = Progress2
    utt.

Risinājums

Formu dizainerī nosauc kontrolus, ko vēlies likt masīvā, vienā nosaukumā, bet ar numuru galā (Label1, Label2, ...). Tad izveido ControlArray instanci un konstruktoram iebaro norādi uz formas Controls kolekciju un kopīgo daļu kontrolu nosaukumā ("Label"). Pēc tam rīkojies kā ar parastu masīvu.
Imports System.Windows.Forms
Imports System.Windows.Forms.Control

Public Class ControlArray(Of T As Control)

    Private MyArray() As Control
    Public ReadOnly Length As Integer

    Public Sub New(ByRef Controls As ControlCollection, ByVal Name As String)
        ReDim MyArray(Controls.Count - 1)
        For i As Integer = 1 To Controls.Count
            MyArray(i - 1) = Controls(Name & i)
            If MyArray(i - 1) Is Nothing Then
                ReDim Preserve MyArray(i - 2)
                Length = i - 1
                Exit For
            End If
        Next
    End Sub

    Default Public ReadOnly Property At(ByVal Index As Integer) As T
        Get
            Return MyArray(Index)
        End Get
    End Property

End Class


(Lasīt komentārus)

Nopūsties:

No:
Lietotājvārds:
Parole:
Ievadi te 'qws' (liidzeklis pret spambotiem):
Temats:
Tematā HTML ir aizliegts
  
Ziņa:

Gandrīz jau aizmirsu pateikt – šis lietotājs ir ieslēdzis IP adrešu noglabāšanu. Operatore Nr. 65.
Neesi iežurnalējies. Iežurnalēties?