Criel
Criel

Reputation: 815

Button created dynamically properties not working vb.net

I'm confused as to why this isn't working...

Here's a brief description of what I've declared my buttons dynamically as the form loads because the form resizes itself depending on whats selected. I've named the buttons button1 and button2. I've added a handler to button2 to handle when the button gets clicked. GetButtonColl is called after the buttons are created and I verified it's populated with the buttons on the form. Why doesn't a simple .enable property work on these dynamically created buttons? Is there something I need to do different?

Here's a condensed version of my code:

Public buttonColl As New List(Of Button)

  '---------------------------------------------------------------------------------------------------------------
    ' Function: GetButtonColl
    ' Parameters: none
    ' Returns: none
    ' Description: Loops through all the controls on the form and searches for buttons that were created after the form has been resized
    ' and adds them to a List which will be used to quickly control the buttons properties throughout the session
    '----------------------------------------------------------------------------------------------------------------
    Public Sub GetButtonColl()

        For x As Integer = 0 To Me.Controls.Count - 1
            Dim b As Control = TryCast(Me.Controls(x), Control)
            If b IsNot Nothing AndAlso b.Tag Is Nothing Then
                If TypeOf (b) Is Button Then
                    buttonColl.Add(b)
                End If
            End If
        Next
    End Sub

Public Sub button1_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
           For Each bttn As Button In buttonColl
                If bttn.Name = "button2" Then
                    bttn.Enabled = False
                End If
            Next


        Catch ex As Exception
         End Try
End Sub

Here's how I'm creating my buttons through the code:

 btn = New System.Windows.Forms.Button
            With btn
                .Enabled = true
                .Location = New System.Drawing.Point(9, 332)
                .Size = New System.Drawing.Size(122, 26)
                .Name = "button1"
                .Text = "TestButton"
                .BackColor = System.Drawing.SystemColors.Control
                .Cursor = System.Windows.Forms.Cursors.Default
                .Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
                .ForeColor = System.Drawing.SystemColors.ControlText
                .RightToLeft = System.Windows.Forms.RightToLeft.No
                .TabIndex = TabIndex
                .UseVisualStyleBackColor = False
            End With
            Me.Controls.Add(btn)

Upvotes: 0

Views: 914

Answers (2)

OneFineDay
OneFineDay

Reputation: 9024

Simplify. The sender object is the button clicked on just cast it back.

Public Sub button1_Handler(ByVal sender As Object, ByVal e As System.EventArgs)
 Dim btn As Button = DirectCast(sender, Button)
 If btn.Name = "button2" Then
    btn.Enabled = False
 End If
End Sub

btn = New System.Windows.Forms.Button
With btn
 .Enabled = true
 .Location = New System.Drawing.Point(9, 332)
 .Size = New System.Drawing.Size(122, 26)
 .Name = "button2" 'wouldn't this be button2?
 .Text = "TestButton"
 .BackColor = System.Drawing.SystemColors.Control
 .Cursor = System.Windows.Forms.Cursors.Default
 .Font = New System.Drawing.Font("Arial", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 .ForeColor = System.Drawing.SystemColors.ControlText
 .RightToLeft = System.Windows.Forms.RightToLeft.No
 .TabIndex = TabIndex
 .UseVisualStyleBackColor = False
 Addhandler btn.Click, AddressOf button1_Handler 'add the delegate
End With
Me.Controls.Add(btn)

Also add the Button to the List when you create it, if you need it in a List.

Upvotes: 1

Bradley Uffner
Bradley Uffner

Reputation: 17001

Are the buttons directly contained within the form, or are they inside another panel? Looping through me.controls will not get grandchild controls, it isn't recursive.

Upvotes: 0

Related Questions