locked
Double click on the control and remove it ? RRS feed

  • Question

  • I found this which suggests a solution for detecting if a control is clicked.

    With the solution above, we can only find out what type of control is clicked. I need to get the name of the control when it is double clicked, so as to remove it.

    Monday, April 22, 2019 6:50 AM

Answers

  • Hi,

    You can put the need to delete in a collection:

    Public Class Form1
        Dim allcontrols As New List(Of Control)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            allcontrols.Add(Button1)
            allcontrols.Add(Button2)
            allcontrols.Add(Button3)
            For Each control As Control In allcontrols
                AddHandler control.Click, AddressOf control_Click
            Next
        End Sub
    
        Private Sub control_Click(ByVal sender As Object, ByVal e As System.EventArgs)
          
                Me.Controls.Remove(sender)
                sender.Dispose()
    
        End Sub
    
    
    
    End Class
    

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kevin993 Monday, April 22, 2019 8:41 AM
    Monday, April 22, 2019 8:31 AM

All replies

  • Hi,

    You can put the need to delete in a collection:

    Public Class Form1
        Dim allcontrols As New List(Of Control)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            allcontrols.Add(Button1)
            allcontrols.Add(Button2)
            allcontrols.Add(Button3)
            For Each control As Control In allcontrols
                AddHandler control.Click, AddressOf control_Click
            Next
        End Sub
    
        Private Sub control_Click(ByVal sender As Object, ByVal e As System.EventArgs)
          
                Me.Controls.Remove(sender)
                sender.Dispose()
    
        End Sub
    
    
    
    End Class
    

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Kevin993 Monday, April 22, 2019 8:41 AM
    Monday, April 22, 2019 8:31 AM

  •  I need to get the name of the control when it is double clicked, so as to remove it.

    I assume you are talking about detecting a double-click at run time, not at
    design time. Is that correct?

    What type of control are you talking about? Some controls have a double-click
    event for which you can write a handler For example a ListBox.

    Some controls don't have such an event, such as a Button.

    See:

    Control.DoubleClick Event 
    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.doubleclick?view=netframework-4.8

    - Wayne

    Monday, April 22, 2019 8:45 AM
  • Hi,

    You can put the need to delete in a collection:

    Public Class Form1
        Dim allcontrols As New List(Of Control)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            allcontrols.Add(Button1)
            allcontrols.Add(Button2)
            allcontrols.Add(Button3)
            For Each control As Control In allcontrols
                AddHandler control.Click, AddressOf control_Click
            Next
        End Sub
    
        Private Sub control_Click(ByVal sender As Object, ByVal e As System.EventArgs)
          
                Me.Controls.Remove(sender)
                sender.Dispose()
    
        End Sub
    
    
    
    End Class
    

    Best Regards,

    Alex



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    That was interesting to know that if we give sender as input to the remove function,

    Me.Controls.Remove(sender)

    there would be no problem! I was expecting a syntax error, because the input should be a control not an object lol

    Thanks

    Monday, April 22, 2019 8:46 AM

  •  I need to get the name of the control when it is double clicked, so as to remove it.

    I assume you are talking about detecting a double-click at run time, not at
    design time. Is that correct?

    What type of control are you talking about? Some controls have a double-click
    event for which you can write a handler For example a ListBox.

    Some controls don't have such an event, such as a Button.

    See:

    Control.DoubleClick Event 
    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.doubleclick?view=netframework-4.8

    - Wayne

    Yes , you are right. Some controls have a double-click event that can be used in the case of this thread and I meant exactly what you mentioned. I asked the question with double-click, because in my case the click event might do something different than removing.


    • Edited by Kevin993 Monday, April 22, 2019 8:54 AM
    Monday, April 22, 2019 8:53 AM