按钮创建动态属性不工作vb.net

时间:2014-01-22 17:36:21

标签: vb.net winforms button

我很困惑为什么这不起作用......

以下是我在表单加载时动态声明我的按钮的简要说明,因为表单会根据所选内容自行调整大小。我已将按钮命名为button1和button2。我已经为button2添加了一个处理程序来处理单击按钮的时间。创建按钮后调用GetButtonColl,我验证它是否填充了表单上的按钮。为什么一个简单的.enable属性不适用于这些动态创建的按钮?有什么我需要做的不同吗?

以下是我的代码的精简版本:

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

以下是我通过代码创建按钮的方法:

 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)

2 个答案:

答案 0 :(得分:1)

简化。发件人对象是点击的按钮,只需将其投回。

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)

如果您在Button中需要,请在List创建List时添加{{1}}。

答案 1 :(得分:0)

按钮是否直接包含在表单中,还是在另一个面板中?循环遍历me.controls将不会获得孙子控件,它不是递归的。