【问题标题】:Button created dynamically properties not working vb.net动态创建的按钮属性不起作用 vb.net
【发布时间】:2014-01-22 17:36:21
【问题描述】:

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

以下是我在表单加载时动态声明按钮的简要说明,因为表单会根据所选内容自行调整大小。我已将按钮命名为 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)

【问题讨论】:

  • button1_Handler 没有处理程序(没有处理程序...),所以除非您动态添加处理程序(代码显示无),否则它可能没有运行。添加断点查看。
  • 我已调试并确认我为 button1 拥有的处理程序命中了 button1_handler 子例程。我在动态创建 button1 控件的函数中添加了处理程序

标签: vb.net winforms button


【解决方案1】:

简化。 sender 对象是单击的按钮,只需将其回退即可。

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)

如果您需要在List 中创建List,还可以将Button 添加到List

【讨论】:

    【解决方案2】:

    按钮是直接包含在表单中,还是在另一个面板中?循环通过me.controls 不会得到孙控件,它不是递归的。

    【讨论】:

    • 我已经更新了我的第一篇文章,展示了如何将按钮添加到表单中。我可以通过循环 me.controls 来获取按钮
    • @Criel,我更新了我的答案 - 你错过了点击事件的代表。
    猜你喜欢
    • 2012-09-17
    • 2014-08-27
    • 1970-01-01
    • 2017-12-19
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多