Results 1 to 3 of 3

Thread: can VB.net support array of controls?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    6

    can VB.net support array of controls?

    I want to use an array of the listbox control in VB.net , I use the code below:

    dim controltmp() as new listbox()

    But I have no idea of how to catch events such as click and double click of the controls in the array!

    can anybody help?

    Thanks in advance

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you dim them as New listboxes they wont be added to the form or anything. Its easier to just add as many as you need on the form at designtime then at runtime put them into an array. Or if you only need for them to have the same event handler than there is no need for the array. Look up the Handles or AddHandler keyword for an explaination of handling events.

    VB Code:
    1. 'adding existing controls to an array
    2. dim ctrl() as Listbox={Listbox1,Listbox2,ListBox3,Listbox4}
    3.  
    4. 'The Handles keyword at the end of the event handler method determines what handles the event
    5. Private Sub Listbox_Click(sender as object, e as eventargs) [b]Handles[/b] Listbox1.Click
    6.  
    7. 'just add more controls to the end of that statement
    8. Private Sub Listbox_Click(sender as object, e as eventargs) Handles Listbox1.Click,Listbox2.Click,ListBox3.Click,ListBox4.Click

  3. #3
    Member
    Join Date
    Jan 2003
    Location
    Beer City
    Posts
    33
    You can make an array of controls.

    I have used this whenever the data was going to determine the control to show. for example if the data is the answer to a true false question I show radio buttons. if it is a nemeric I show a text box.

    'first set an array as an object.

    Public ctr_exam(10, 1) As Object

    dim row as integer
    dim column as integer
    'pretend dataset rows
    For row = 1 to 10

    'then you can redim as you need to grow. use the
    'Preserve keyword so the array remembers what was already loaded
    ReDim Preserve ctr_exam(10, CurrentRow)
    'pretend dataset columns
    for column = 1 to 3
    'pretend data
    if row+column > 7 then
    ' if the data is small put in a radio button
    'i customized the radio button control but you will get the idea
    'Set this location of the array as a Radio Button
    ctr_exam(Column, Row) = New Rdo_Button.DynamicRadioButtonPair()

    'fill the properties of the button

    With ctr_exam(CurrentColumn, CurrentRow)
    .fk_Exam = myfk_Exam
    .Tag = dreader(0)
    .Size = New System.Drawing.Size(275, 35)
    .Name = "ExamLabel" & CurrentRow * CurrentColumn
    .PassWord = passing
    .FailWord() = failing
    .Location = New System.Drawing.Point(left, ((CurrentRow) * 36))
    .fk_ApplicantPositionEvent = dreader(0)
    .exam = exam
    left += 278
    If MyResult = 1 Then
    .PassCheck = True
    .Color = Color.LightSlateGray

    ElseIf MyResult = 0 Then
    .FailCheck = True
    .Color = Color.LightSlateGray
    End If
    .Active = True
    End With
    'now for the other controls
    else
    ' again a custom text box but a plain one would work.
    ctr_exam(CurrentColumn, CurrentRow) = New DynamicTextBox()

    With ctr_exam(CurrentColumn, CurrentRow)
    .fk_Exam = myfk_Exam
    .Tag = dreader(0)
    .Size = New System.Drawing.Size(100, 35)
    .Name = "ExamLabel" & CurrentRow * CurrentColumn
    .Location = New System.Drawing.Point(left, 8 + ((CurrentRow) * 36))
    left += 127
    .fk_ApplicantPositionEvent = dreader(0)

    If Not IsDBNull(MyResult) AndAlso MyResult < 999999 Then
    .text = MyResult
    .backColor = Color.LightSlateGray
    End If
    End With
    'add it to the panel


    End If
    Next

    'add Contorl to the panel
    Me.Panel2.Controls.AddRange(New System.Windows.Forms.Control() {ctr_exam(CurrentColumn, CurrentRow), MyNameLabel(CurrentRow)})



    next




    i hope this helps a bit

    tal mcmahon
    its called the "F1" key-- use it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width