How to pass a variable to a parameter ?

R

Rob

lets assume I want to remove a control from a form that was dynamically
created...

If the button was added at design time, I can do the following...

me.Controls.Remove(Button4)

How may I pass a variable in the parameter position ? i.e.,
me.Controls.Remove(strVariable)
 
M

Mattias Sjögren

How may I pass a variable in the parameter position ? i.e.,
me.Controls.Remove(strVariable)

I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.


Mattias
 
R

Rob

I tried this...

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()

Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls

delme = (ctrl.Name)
Call wipeout(delme)

Next

End Sub
End Class


Public Sub wipeout(ByRef delme As Object)

Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub
 
R

rowe_newsgroups

I tried this...

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()

Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls

delme = (ctrl.Name)
Call wipeout(delme)

Next

End Sub
End Class

Public Sub wipeout(ByRef delme As Object)

Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub

Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe
 
R

Rob

Thanks so much for your help Seth !

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

I have a button that adds user controls to the FlowLayoutPanel... so I added
8 of them... ran the code and 4 were deleted... ran again... 2 more were
deleted...

Public Sub Wipeout(ByVal c As Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

- Rob


I tried this...

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()

Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls

delme = (ctrl.Name)
Call wipeout(delme)

Next

End Sub
End Class

Public Sub wipeout(ByRef delme As Object)

Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub

Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe
 
R

rowe_newsgroups

Thanks so much for your help Seth !

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

I have a button that adds user controls to the FlowLayoutPanel... so I added
8 of them... ran the code and 4 were deleted... ran again... 2 more were
deleted...

Public Sub Wipeout(ByVal c As Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

- Rob










Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

That's because as we remove a control it throws off the for each
loop's counter causing it to skip every other control. Now that I
realized my stupid mistake (hey it is early here) try this instead:

Private Sub Button3_Click2(ByVal sender As System.Object, ByVal e
As System.EventArgs) 'Handles Button3.Click
Dim i As Integer = 0
While i < Me.FlowLayoutPanel1.Controls.Count
Dim c As Control = Me.FlowLayoutPanel1.Controls(i)
If (c.Name = "ControlToDelete") Then
Wipeout(c)
Else
i += 1
End If
End While
End Sub

That way we only increment the counter when we don't remove a control.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

I see you are just deleting all the FlowLayoutPanels controls right?
If that's all you need to do you can just replace that whole method
with:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As
Me.FlowLayoutPanel1.Controls.Clear()
End Sub

Thanks,

Seth Rowe
 
R

Rob

Seth,

That worked perfectly ! Thank You very much !

Thanks for this as well...
However, eventually I will be adding some criteria as to which controls get
removed...

Rob


Thanks so much for your help Seth !

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

I have a button that adds user controls to the FlowLayoutPanel... so I
added
8 of them... ran the code and 4 were deleted... ran again... 2 more were
deleted...

Public Sub Wipeout(ByVal c As Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

- Rob










Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

That's because as we remove a control it throws off the for each
loop's counter causing it to skip every other control. Now that I
realized my stupid mistake (hey it is early here) try this instead:

Private Sub Button3_Click2(ByVal sender As System.Object, ByVal e
As System.EventArgs) 'Handles Button3.Click
Dim i As Integer = 0
While i < Me.FlowLayoutPanel1.Controls.Count
Dim c As Control = Me.FlowLayoutPanel1.Controls(i)
If (c.Name = "ControlToDelete") Then
Wipeout(c)
Else
i += 1
End If
End While
End Sub

That way we only increment the counter when we don't remove a control.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

I see you are just deleting all the FlowLayoutPanels controls right?
If that's all you need to do you can just replace that whole method
with:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As
Me.FlowLayoutPanel1.Controls.Clear()
End Sub

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top