CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Control Arrays?

  1. #1
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150

    Angry Control Arrays?

    I've created a user control and I'm having trouble with creating an array of these user controls at runtime.

    This is what I'm looking to do......
    My application as three different views. If it is View1, it goes out to a database and grabs the caption properties and hidden properties from table tblView1 and loads the properties in for each new control I have for this view. Any ideas?

    tblView1
    Account#, fldAcctNum
    Co#, fldCoNum
    ....

    tblView2
    Control1, fldCtl1
    Control2, fldCtl2
    Control3, fldCtl3

    and so on.....

    If you're confused.....SO AM I!!!

  2. #2
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236

    Re: Control Arrays?

    Control Array Changes in Visual Basic .NET
    See Also
    Event Changes in Visual Basic .NET | Control Changes in Visual Basic .NET |
    Setting the Tab Order on Windows Forms

    In Visual Basic 6.0, control arrays could be used to specify a group of
    controls that shared a set of events. The controls had to be of the same
    type, and they had to have the same name.

    In Visual Basic .NET, control arrays are no longer supported. Changes to the
    event model make control arrays unnecessary. Just as control arrays in
    Visual Basic 6.0 could share events, the event model in Visual Basic .NET
    allows any event handler to handle events from multiple controls. In effect,
    this allows you to create groups of controls of disparate types that share
    the same events.

    For example, you might add two Button controls (Button1 and Button2) and a
    CheckBox control (CheckBox1) to a form, then create an event handler to
    handle the Click event for all three controls:

    Private Sub MixedControls_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click, Button2.Click, CheckBox1.Click
    Another feature of Visual Basic 6.0 control arrays was the ability to
    reference a control by its Index property. Although Visual Basic .NET
    controls do not have an Index property, you can duplicate the functionality
    using another common property such as the TabIndex or Tag property.

    For example, you might set the TabIndex property for a group of controls
    using the new visual tab ordering capabilities of Windows Forms, and then
    use the TabIndex in a Select Case statement:

    Private Sub MixedControls_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click, Button2.Click, CheckBox1.Click
    Select Case sender.TabIndex
    Case 0
    MsgBox("Button 1")
    Case 1
    MsgBox("Button 2")
    Case 2
    MsgBox("CheckBox 1")
    End Select
    End Sub

    Use build-in help - that helps

  3. #3
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    OK...that helps.

    BUT....what I'm having trouble doing is creating an unspecified amount of controls. Lets say nBtn = 6, how do I programmatically add these six controls?

    I know you can....
    Dim btn1 as new Button()
    Dim btn2 as new Button()
    Dim btn3 as new Button()
    Dim btn4 as new Button()
    Dim btn5 as new Button()
    Dim btn6 as new Button()

    then add them, but it won't always be that I'm adding 6 controls. Let's say next time I want to add 3 nBtn controls. Is there a way to go "For i = 1 to n....create 3 controls"?

    ??????????????????????????????
    for i = 1 to n
    dim btn(i) as new Button()
    btn(i).Text = "Button " & i
    next

    for i = 1 to n
    Contols.Add(btn(i))
    next
    ??????????????????????????????


    Thanks again!

  4. #4
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    Try this:
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            For i = 1 To 10
                Dim btn As New Button()
                Me.Controls.Add(btn)
                btn.Show()
                btn.Left = i * btn.Width
                btn.Name = i
                btn.Text = btn.Name
            Next
        End Sub
    Got the idea?

  5. #5
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    Ok..Ok..looks good!

    So...then if I wanted to create an array I would just set btn.Tag = i?

    HUGE THANKS!!

  6. #6
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    Lets modify the code a little:
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer
            For i = 1 To 10
                Dim btn As New Button()
                Me.Controls.Add(btn)
                btn.Show()
                btn.Left = i * btn.Width
                btn.Name = "but" & i
                btn.Text = btn.Name
            Next
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim ctl As Control
            For Each ctl In Me.Controls
                If ctl.Name = "but1" Then
                    MessageBox.Show("You got me!")
                    Exit Sub
                End If
            Next
            MessageBox.Show("Did you click Button1?")
        End Sub
    Honestly, I've never played with that in .NET

  7. #7
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    Do you have any ideas how to get all the User Controls to share functionality?....I'm still having problems. Also, I can make a reference to my User Control, but I cannot find it in the Toolbox and/or added to during Design time. Any idears?

    MY CODE:
    ' works fine
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim i, y As Integer
    y = 0
    For i = 1 To 7
    Dim userCtl As New ctlChkTxtDrop()
    Me.Controls.Add(userCtl)
    userCtl.Show()
    userCtl.Location = New Point(0, y)
    userCtl.Name = i
    userCtl.ChkTxtDropCaption = "User Control #" & i
    userCtl.Tag = userCtl.Text
    y = y + 30
    Next
    End Sub
    Last edited by nolc; May 23rd, 2002 at 10:48 AM.

  8. #8
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    I was a bit busy yesterday
    I used a button instead of your control.
    Replace Dim userCtl As New Button()
    with Dim userCtl As New ctlChkTxtDrop().
    And userCtl.Text = "User Control #" & i with userCtl.ChkTxtDropCaption = "User Control #" & i
    That should work.

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i, y As Integer
            y = 0
            For i = 1 To 7
                Dim userCtl As New Button()
                Me.Controls.Add(userCtl)
    
                userCtl.Show()
                userCtl.Location = New Point(0, y)
                userCtl.Name = i
    
                userCtl.Text = "User Control #" & i
                userCtl.Tag = userCtl.Text
                y = y + 30
    
                AddHandler userCtl.Click, AddressOf UserCtl_Clicks 'Add an Even Handle
            Next
        End Sub
    
        Sub UserCtl_Clicks(ByVal sender As System.Object, ByVal e As System.EventArgs)
            MessageBox.Show("Click from " & sender.text)
        End Sub

  9. #9
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    Thanks Akim.
    This is what I came up with yesterday, too. BUT once I find out what control has been clicked I would like to find the following ctlChkTxtDrop Control's Locations. My control is a Checkbox with a textbox underneath it with Visible = False & my control Height = 24. If the Checkbox Checked value = True...make the Textbox visible, expands the Control Height = 48 and move the following ctlChkTxtDrop controls Y cordinates down 30. Any ideas?

    It's basically like a Search Engine. If ctlChkTxtDrop.Checked = True expand the Searchable Field below the CheckBox.

    EXAMPLE:
    ctlChkTxtDrop1.Checked = False
    ctlChkTxtDrop2.Checked = False
    ctlChkTxtDrop3.Checked = True
    ........ctlChkTxtDrop.TextBox1.Visible = True
    ctlChkTxtDrop4.Checked = False
    ctlChkTxtDrop5.Checked = False
    ctlChkTxtDrop6.Checked = True
    ........ctlChkTxtDrop.TextBox1.Visible = True




    CODE:
    Imports ctlChkTxtDropLib

    Public Class Form2
    Inherits System.Windows.Forms.Form

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles MyBase.Load
    Dim intIndex, Ycord As Integer
    Dim objControl As ctlChkTxtDrop
    'Dim objControl As Button

    Ycord = 0

    For intIndex = 1 To 5
    Dim i As Integer = intIndex
    objControl = New ctlChkTxtDrop()
    'objControl = New Button()

    objControl.Name = "userCtl" & i & "of5"
    objControl.ChkTxtDropCaption = "User Control #" & i
    objControl.Tag = i '- 1
    objControl.Location = New Point(0, Ycord)
    objControl.ChkTxtDropBackColor = Color.Blue
    AddHandler objControl.SizeChanged, AddressOf ctlChkTxtDrop_Click
    'AddHandler objControl.Click, AddressOf Button_OnClick
    Controls.Add(objControl)
    Ycord = Ycord + 30
    Next
    End Sub

    Private Sub ctlChkTxtDrop_Click(ByVal sender As Object, ByVal e As EventArgs)
    Select Case CType(sender, ctlChkTxtDrop).Name
    Case Is = "userCtl1"
    MsgBox("ctlChkTxtDrop1_Click")

    Case Is = "userCtl2"
    MsgBox("ctlChkTxtDrop2_Click")

    Case Is = "userCtl3"
    MsgBox("ctlChkTxtDrop3_Click")

    Case Is = "userCtl4"
    MsgBox("ctlChkTxtDrop4_Click")

    Case Is = "userCtl5"
    MsgBox("ctlChkTxtDrop5_Click")
    End Select
    End Sub

    Thanks in advanced!

  10. #10
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    I'll stick with my example (I don't have your control):

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i, y As Integer
            y = 0
            For i = 1 To 7
                Dim userCtl As New Button()
                Me.Controls.Add(userCtl)
    
                userCtl.Show()
                userCtl.Location = New Point(0, y)
                userCtl.Name = i
    
                userCtl.Text = "User Control #" & i
                userCtl.Tag = i
                y = y + 30
    
                AddHandler userCtl.Click, AddressOf UserCtl_Clicks 'Add an Even Handle
            Next
        End Sub
    
        Sub UserCtl_Clicks(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim strMess As String
            Dim userCtl As Button
            For Each userCtl In Me.Controls
                If userCtl.Tag = sender.tag + 1 Then
                    MessageBox.Show("Click from " & sender.text & vbCrLf & "Next Control coords: " & userCtl.Left & ";" & userCtl.Top)
                End If
            Next
    
        End Sub
    Is that what you want to achieve?

  11. #11
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    Kind of....but it won't work if I have other controls on the form. BUT I could put these controls in a Panel or Groupbox. I will also need the other button control's Y cordinates below the one clicked, so I can move the following button controls down 30.

    I think the best way of maybe doing this is storing the Y cords in an array in the Form Load section. Agree?

    Thanks again.....

  12. #12
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    How does the following sound?

    Code:
    'Declare Private colButtons As New Collection()  - Form level declaration
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i, y As Integer
            y = 0
            For i = 1 To 7
                Dim userCtl As New Button()
                Me.Controls.Add(userCtl)
    
                userCtl.Show()
                userCtl.Location = New Point(0, y)
                userCtl.Name = i
    
                userCtl.Text = "User Control #" & i
                userCtl.Tag = i
                y = y + 30
                colButtons.Add(userCtl, i)
                AddHandler userCtl.Click, AddressOf UserCtl_Clicks 'Add an Even Handle
            Next
        End Sub
    
        Sub UserCtl_Clicks(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If sender.tag < colButtons.Count Then
                MessageBox.Show("Click from " & sender.text & vbCrLf & "Next Control coords: " & colButtons.Item(sender.tag + 1).Left & ";" & colButtons.Item(sender.tag + 1).Top)
            End If
        End Sub
    In this case you can access to any control easily.

  13. #13
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    That will work! Thanks for all your help!

  14. #14
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    You're welcome

    Also, I just found that one (you might want to take a look):

    From MSDN
    URL is too long, so just delete "%20" and "<br>" from your address line to get to the right page (you'll see what I mean)

  15. #15
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150
    Hey Akim,
    Forget the Private Message....I was able to get to the correct URL once I deleted the %20 and <br>.

    thanks again my friend!

Page 1 of 2 12 LastLast

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

Featured