如何快速创建千个按钮?

时间:2015-11-13 01:39:40

标签: vb.net

我有一个可以创建4000个新按钮的程序但是我花了30秒才完成它。反正有没有让它更快?

For x = 1 To 4000

    Dim btnNew As New Button()

    btnNew.Width = 14
    btnNew.Height = 11

    btnNew.Location = New Point(a, b)
    Me.Controls.Add(btnNew)
    btn(k) = btnNew

Next

1 个答案:

答案 0 :(得分:4)

您确实需要做三件事:

  1. 调用SuspendLayout / ResumeLayout(以保存所有多个布局)
  2. 您可以改为调用AddRange。
  3. 如果容器支持BeginUpdate / EndUpdate,则使用这两个(以保存重新绘制)。
  4. 所以试试:

    Me.SuspendLayout()
    Try
        For x As Integer = 1 To 4000
    
            Dim btnNew As New Button()
    
            btnNew.Width = 14
            btnNew.Height = 11
    
            btnNew.Location = New Point(a, b)
            Me.Controls.Add(btnNew)
            btn(k) = btnNew
    
        Next
    Catch ex As Exception
        Throw
    Finally
       Me.ResumeLayout()
    End Try