Control Possiblility ?

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

Is it possible for a control to raise an event to remove itself ?

Specifically...

I have a FlowLayoutPanel that contains several instances of the same user
control.
Within each "user" control, I would like to place a buton that can remove
itself..

Please note that I want to "remove" the control as opposed to "clearing" it.
The clear does not shrink the overall panel space allocated in the
FlowLayoutPanel.

Thanks !
 
Rob said:
Is it possible for a control to raise an event to remove itself ?

Specifically...

I have a FlowLayoutPanel that contains several instances of the same user
control.
Within each "user" control, I would like to place a buton that can remove
itself..

Please note that I want to "remove" the control as opposed to "clearing"
it.
The clear does not shrink the overall panel space allocated in the
FlowLayoutPanel.

Thanks !

I would think it is ok since all you really have to do is remove the control
form the panels control collection. You don't really HAVE to get rid of the
control itself; just remove it from the collection of included controls so
it is no longer seen by the user.
 
My reason for thinking this...

I can program a button on a user control to clear it... and it works...

Me.Controls.Clear

But, it appears I cannot issue...

Me Controls.Remove

Am I missing something ?
 
Rob,

When I create a windows forms application and add a button to it, the
following is fine:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Controls.Remove(Button1)
End Sub

End Class



So I think perhaps there is some other issue at play here. It would be
useful to see some code.



Robin
 
Hi Robin,

I see your example does work, so I just am not understanding how to do it...

Logically I would have thought the following would work, but I know it is
wrong.

Since the control is dynamically created at run time (user control within a
FlowLayoutPanel), I do not have the option of hardcoding it as you did.

So I tried

' declare an new instance
Dim c as new control

' find out which instance of the control
c=Me.Control

' remove it
controls.remove(c)


Rob
 
Rob,


When you write Controls.Remove(Me), you are trying to remove the form
instance (more than likely) form the controls collection. "Me" isn't the
object that is the subject of the handler (the button), it is the object
that is handling the event (the form).

Presumably if you are creating the controls manually, you are assigning
handlers manually as well. Here is an example in full. Create a Windows
Forms project and add a button to it at the top called "Button1" (name it
"Add"). With the code below, whenever you click "Add", a new button will be
created and docked at the bottom of the form. A handler will also be added
so that when the new button is clicked, it removes itself from the form
controls collection.



Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim newButton As New Button

newButton.Dock = DockStyle.Bottom
newButton.Text = Controls.Count.ToString

Controls.Add(newButton)

AddHandler newButton.Click, AddressOf AnyButton_Click

End Sub

Private Sub AnyButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim theButton As Button = CType(sender, Button)

RemoveHandler theButton.Click, AddressOf AnyButton_Click

Controls.Remove(theButton)

End Sub

End Class



Robin
 
Thanks for the reply Robin,

A couple things...

I added some code to your code (see below)...

I noticed that the controls that get added by Button1 are "nameless" ???

I see that your code works very nicely... I am however, still having a
problem adapting it to my needs as my "button" is really a button within a
user control within a FlowLayoutPanel. ( I am having trouble referencing
the click event of the button within the user control.) Should it work in
this scenario as well (if I can figure it out) ?

FYI - As I add each new user control to the FlowLayoutPanel, I do give it
a name, that is why I was hoping somehow to use the name as the "primary
key" to remove the control.

Thanks,
Rob


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim newButton As New Button
newButton.Dock = DockStyle.Bottom
newButton.Text = Controls.Count.ToString

Controls.Add(newButton)
AddHandler newButton.Click, AddressOf AnyButton_Click
' ADDED********************
Dim i As Integer = 0
Dim str As String

While i < Me.Controls.Count
Dim c As Control = Me.Controls(i)
str = c.Name
MsgBox(str)
i = i + 1

End While
' ********************************
End Sub
 
Hi Robin,

I think it is finally working.... 40 hours later...

Your reply made me think about what "Me" was...

To refresh...

Button on main form adds a "user control" to a FlowLayoutPanel
The "user control" contains a Button (Button2) that if clicked, the desired
action is to remove the entire user control from the FlowLayoutPanel

The solution that appears to work now...

Controls.Remove(Button2.Parent)
Dispose()

Thanks again for your help !
 
Back
Top