3

我有一个表单 (Form1),它有 30 个控件。当我点击一个按钮时,我想删除这 30 个按钮并将其他控件放在表单上。现在,我的问题是这太慢了。

我有这个列表,其中包含要删除的控件,并使用 For Each 遍历它们。

Private Sub ClearControls()
    'removing the controls from Me.Controls
    For Each Control As Control In ListToDelete
        Me.Controls.Remove(Control)
    Next
    ListToDelete = New List(Of Control)
End Sub

现在,如果您查看表单,您会看到控件被一一删除。此操作大约需要 0.4 秒(使用内置秒表计时),这太长了。

是否有任何解决方案可以更快地删除控件,或者只能逐个删除控件?

也许一个重要的事实是一切都与数据库相连。这些控件是由我自己定义的一个类(TableDrawer)创建的,它创建一个矩形或圆形(取决于数据库中的信息)。我将自制控件添加到表单中,当我想删除它们时,需要 0.4 秒才能获取表单上的其他控件 - 以及我的数据库中的信息。

希望这可以解决一些问题,我希望你能帮助我......它真的必须快一点(我希望得到 0.1 秒或更低)

4

5 回答 5

6

Hiding the Panel first seems to make the controls disappear quicker than just clearing the Panel. See this code:

Option Strict On

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Panel1.Visible = False

        If Not Panel1.Controls.OfType(Of Button).Any() Then
            For x As Integer = 1 To 10
                For y As Integer = 1 To 10
                    Dim btn As New Button()
                    btn.Size = New Size(45, 45)
                    btn.Location = New Point((x - 1) * 45, (y - 1) * 45)
                    btn.Text = (x * y).ToString()
                    Panel1.Controls.Add(btn)
                    btn.Visible = True
                Next
            Next
        End If

        Panel1.Visible = True
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Panel1.Visible = False
        Panel1.Controls.Clear()
        Panel1.Visible = True
    End Sub
End Class

This code has 2 buttons, and a Panel. Button1 generates 100 buttons, places them on a Panel. Button2 hides the panel before removing them. Perhaps you can experiment with this idea.

于 2012-09-27T14:35:24.427 回答
2

这不是删除往往需要时间 - 它每次都重新绘制表单。尝试用调用来包围你的删除代码SuspendLayoutResumeLayout

Private Sub ClearControls()
    'removing the controls from Me.Controls
    Me.SuspendLayout()
    For Each Control As Control In ListToDelete
        Me.Controls.Remove(Control)
    Next
    Me.ResumeLayout()
    ListToDelete = New List(Of Control)
End Sub
于 2012-09-27T13:24:53.260 回答
1

将控件放在面板容器控件中。移除面板容器会移除所有子控件。

于 2012-09-27T13:25:31.443 回答
1

kindly never used remove and panel.removeat to remove any controls. It won't be able to delete last control in panel layout.Especially for panel.removeat will return out of index error once delete the last end control in panels. I'm also wondering need to know why this is problem appears?

Stored all the control name in string array, find those controls in panel and delete them, Try below code will help you delete all control in panel for one short. Try using with find and removeBykey function will make your task easier.

Dim ctrllist() as string
Dim counts = 0

For each control in Me.panel1.controls
redim Preserve ctrllist(0 to counts) 
ctrllist(counts)=control.name  
counts+=1
Next

For counts=Lbound(ctrllist) to Ubound(ctrlllist)
  If me.panel1.controls.find(ctrllist(counts),True).Length>0 then

     me.panel1.controls.removeBykey(ctrllist(counts))

  End If
Next

Hope it will help.

于 2013-08-19T15:21:14.667 回答
0

Thanks user1884888! The technique helps me.

If I use Me.ScrollPanelControl.Controls.Clear() then the application goes unresponsive and there's no choice to terminate it from Task Manager but using this technique helps me.

This code is to help some one having the same problem.

                While (True)
                    Dim count = Me.ScrollPanelControl.Controls.Count
                    If count <= 0 Then
                        Exit While
                    End If
                    Dim firstCtrl = CType(Me.ScrollPanelControl.Controls(0), MyControl)
                    If Not firstCtrl.IsMoving Then
                        If Me.ScrollPanelControl.Controls.Find(firstCtrl.Name, True).Length > 0 Then
                            Me.ScrollPanelControl.Controls.RemoveByKey(firstCtrl.Name)
                        End If
                    ElseIf count > 1 Then
                        firstCtrl = CType(Me.ScrollPanelControl.Controls(1), MyControl)
                        If Me.ScrollPanelControl.Controls.Find(firstCtrl.Name, True).Length > 0 Then
                            Me.ScrollPanelControl.Controls.RemoveByKey(firstCtrl.Name)
                        End If
                    Else
                        Exit While
                    End If
                End While
于 2013-09-17T08:20:14.397 回答