User control that can hover and can be attached to form in vb.net

Viewed 144

When I moves window on screen, user control is not moving.Is there any way we can add the control to the window except we create control at run time in form window. I have shared the code below.

I've created a custom CustomBox using links link1 and link2

User Control Code

Private mList As CustomBox
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  
       mList = New CustomBox(Me)
       mList.Location = New Point(5, 10)
       mList.Size = New Size(50, Me.ClientSize.Height + 50)
        
       For ix As Integer = 0 To 100 - 1
            mList.Items.Add(ix)
       Next
    End Sub
1 Answers

EDITED:

Instead of passing Me (i.e. user control instance) as parent, pass the Me.ParentForm as the parent. In case you still require the user control instance for any other purpose, just pass it as another parameter.

The other thing you need to manage is the offset. For that you can simply add the user control's location to wherever you want to place it.

Here is the updated code:

Public Class UserControl1
    Private mList As CustomBox

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        mList = New CustomBox(Me.ParentForm)
        mList.Location = Me.Location + (New Point(5, 10))
        mList.Size = New Size(50, Me.ClientSize.Height + 50)

        For ix As Integer = 0 To 100 - 1
            mList.Items.Add(ix)
        Next
    End Sub
End Class

Here's how it looks with above code changes.

https://imgur.com/8lbAfjU

Related