vb.net 多次从表单中动态创建和删除控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18861653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:09:56  来源:igfitidea点击:

Dynamically create and remove a control from a form, many times

vb.netcontrolsdisposedynamically-generated

提问by David K

The below subroutine, when called using a mouse click, successfully creates and then removes a control. but it doesn't create it a second time. I'm assuming it is because the label is not longer dimensioned as public. ie Dim lblDebug1 As New Labelis at the top variable section of the form. However when I put Dim lblDebug1 As New Labelin the subroutine the dispose request doesn't work. Is there someway that I can keep creating and disposing a control?

下面的子程序,当使用鼠标单击调用时,成功创建然后删除控件。但它不会第二次创建它。我假设这是因为标签不再标注为公开尺寸。即Dim lblDebug1 As New Label位于表单的顶部变量部分。但是,当我放入Dim lblDebug1 As New Label子程序时,处置请求不起作用。有什么办法可以让我继续创建和处理控件吗?

In the below sub, booleanDebugis used to switch back and forth between creating it and disposing it. Thanksin advance.

在下面的子中,booleanDebug用于在创建和处置之间来回切换。提前致谢

Dim lblDebug1 As New Label

booleanDebug = Not booleanDebug
  If booleanDebug Then
      Me.Controls.Add(lblDebug1)
      lblDebug1.BackColor = Color.BlueViolet
  Else
      lblDebug1.Dispose()
  End If

采纳答案by jcwrequests

Ensure the label has a global context. Within the form that owns it and that you have all the appropriate size and coordinates information and visibility set.

确保标签具有全局上下文。在拥有它的表单中,您拥有所有适当的大小和坐标信息以及可见性集。

Here is some sample code that worked for me. First just create a new windows form then add a button control in the middle of the form then use the following code.

这是一些对我有用的示例代码。首先创建一个新的windows窗体,然后在窗体中间添加一个按钮控件,然后使用以下代码。

Public Class Main
Private labelDemo As Windows.Forms.Label

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.SuspendLayout()

    If labelDemo Is Nothing Then

        labelDemo = New Windows.Forms.Label
        labelDemo.Name = "label"
        labelDemo.Text = "You Click the Button"
        labelDemo.AutoSize = True
        labelDemo.Left = 0
        labelDemo.Top = 0
        labelDemo.BackColor = Drawing.Color.Violet
        Me.Controls.Add(labelDemo)

    Else
        Me.Controls.Remove(labelDemo)
        labelDemo = Nothing
    End If

    Me.ResumeLayout()

End Sub
End Class

回答by Joel Coehoorn

Once you've Disposed a control, you can't use it any more. You have two choices here:

一旦你处置了一个控件,你就不能再使用它了。您在这里有两个选择:

Choice 1:Just Remove the control from the form rather than disposing it:

选择 1:只需从表单中删除控件而不是处置它:

'Top of the file
Dim lblDebug1 As New Label

'Button click
booleanDebug = Not booleanDebug
If booleanDebug Then 
    lblDebug1.BackColor = Color.BlueViolet
    Me.Controls.Add(lblDebug1)       
Else
    Me.Controls.Remove(lblDebug1)
End If

Choice 2:Create a new control object each time

选择2:每次创建一个新的控制对象

'Top of the file
Dim lblDebug1 As Label
'               ^   No "New". 
'We just want an object reference we can share at this point, no need for an instance yet

'Button click
booleanDebug = Not booleanDebug
If booleanDebug Then
    lblDebug1 = New Label()
    lblDebug1.BackColor = Color.BlueViolet
    Me.Controls.Add(lblDebug1)
Else
    lblDebug1.Dispose()
End If