[VB.NET] Array Control


Deskripsi
     HELPPP !!! , how to create array control in VB.net ??? and how detect from event's ??  for example clicked ? it's totally different from vb 6. how we do that ??? that's simple :)

Howto
    1. make sure the object must have name with the number. for example telp1, telp2 ,telp3 etc....
    2. to access them together we need create variable array as a collection , look below
       Dim buttonCollection() as button = {telp1,telp2,telp3.......}

       *Legend : - buttonCollection is the name of variable array
                       - telp1,telp2....etc is a button , the type must same as buttonCollection type. if you create textbox type , the collection must textbox type too (textboxCollection as textbox).
                       - Index always from 0
             to use : buttonCollection( i ).method 
           
    3. some case from event , method above not usefull if you need detect object control array from event for
        example from click, to do that look at step below :
         - create function click (free name) , with handles all object button.
         - use CTYPE
         - create control number based on controlname
        look code below :

        Private Sub Detect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
        telp1.Click, telp2.Click, telp3.Click, telp4.Click, telp5.Click, telp6.Click ...etc

        Dim controlName As String 'buffer namecontrol
        Dim controlNumber As Double 'buffer nomorcontrol

        controlName = CType(sender, Button).Name
        controlNumber = controlName.Remove(0, 4) 'substring , remove 4 character from index 0

        If CType(sender, Button).BackColor = Color.Red Then
            controlInterface(controlNumber, "green") ' another sub
            CType(sender, Button).BackColor = Color.Green
        Else
            controlInterface(controlNumber, "red") ' another sub
            CType(sender, Button).BackColor = Color.Red
        End If
        End Sub

        Here's the explanation :
        Detect_Click : you can use name instead "detect". free :)
        ByVal sender As Object = always return object which handle.                                                  
        handles telp1.click .... telp6.click = list of an object will be handle.
     
        this is main explanation :
        CType(sender, Button).BackColor 
        if i click telp1.click , the sender will return value telp1 as an object, to direct use we can use Ctype. look at the 2 mark (yellow and blue), we must specify type of Ctype like yellow mark(button or textbox or label dst ....). the blue mark will generate method based on yellow mark. that's the key :)

to more understand  you must try :) , hope's this help !

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih