4

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 Label is at the top variable section of the form. However when I put Dim lblDebug1 As New Label in the subroutine the dispose request doesn't work. Is there someway that I can keep creating and disposing a control?

In the below sub, booleanDebug is used to switch back and forth between creating it and disposing it. Thanks in advance.

Dim lblDebug1 As New Label

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

2 Answers 2

3

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.

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
7
  • I put Public lblDebug1 As New Label at the top of the form, but I think Dim.. made it global as well. I put lblDebug1.Visible = True on the list of properties to set. However, like before, if first created it, then disposed of it, and then when it went to create it again, it said I can't change the visibility of a Disposed object. That is after it executed Me.Controls.Add(lblDebug1)
    – David K
    Sep 18, 2013 at 4:23
  • Is the Dim statement inside the method or is it declared elsewhere, perhaps at the top of the form somwhere? Sep 18, 2013 at 14:41
  • Sorry for the delay...@Christ, When Public lblDebug1 As New Label is at the top of the form, the subroutine successfully creates and then removes the control. but it doesn't create it a second time.When Dim lblDebug1 As New Label is in the subroutine the dispose request doesn't work. @jcwrequests ,I've posted it on Gist, here is the link gist.github.com/Dave1235/6633144. It is called CreateDisposeCreatelabel . I'm not sure I did it right however. Thanks in advance
    – David K
    Sep 20, 2013 at 4:06
  • Okay I think I understand and when I get on my laptop latter I will put some code together for you. Right now I am on my tablet. Sep 20, 2013 at 20:29
  • I revised my answer let me know if I can add anything else to make it clearer. Sep 20, 2013 at 21:40
1

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:

'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

'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
1
  • Thanks for your time Joel. Choice 2 looks like it will suit my needs.
    – David K
    Sep 20, 2013 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.