Link to home
Create AccountLog in
Avatar of sharizod
sharizodFlag for Canada

asked on

Can't seem to programmatically move a custom "control" when parent is set to a picturebox

Hi Experts,

I am stumped.  I cannot seem to move my custom "control" programmatically (just a class inheriting from a control class really) when I set the parent to a picturebox.  I do this because I need to be able to have the controls background to be translucent or see-through in varying degrees.  Sample app code is attached.  Perhaps it is something simple but I have looked at it up, down, sideways, and backwards, and can't seem to make it do what I want which is to simply move the whole control programmatically through code.  Currently, only one of the elements contained in the "control" gets moved and not anything else.  I also tried putting it into a custom usercontrol figuring I'd see a difference but had the same results.  Sample is attached.

TIA
 WindowsApplication1.zip
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of sharizod

ASKER

Brilliant!! Thank you sooo much!!!!  I removed the second add and inserted the one line into the move event and it works
like a charm!!  I just would have expected since the parents of all relevant controls were being set, that moving the container
for all those contained controls would move with it?    Again, thanks a bunch!!!
"I just would have expected since the parents of all relevant controls were being set, that moving the container
for all those contained controls would move with it?"

Once you add those controls to the PictureBox (or Form) the Parent is no longer your custom Panel so moving the Panel will have no effect on those controls.

In this case, the "Parent" is the Control that is visually containing the controls...not the class that holds the reference.

Hope that makes sense...
I'm sorry to be a pest, but I thought I had all the problems sorted out but it appears as though that panel1 gets left behind when using the mouse
to move it instead of the button.

Further, compare the way the snapping of the moveable panel is before hitting the move button (cursor starts at upper left of the panel when
dragging) and after (cursor starts in the middle of the panel when dragging it), the panel snaps to a different part on drag of the panel to some
other location on the picturebox.  This out distance gets worse the more the panel move until eventually the cusror is dragging the panel but the
panel does not appear on top of the panel!  I just take a read of the current x&y of the mouse cursor and feed that in to a new point.  Why is it
not snapping the mouse cursor to the same point on the panel all the time?  Ideally, it would not snap the panel to the cursor at all but move it
using the point wherever it is on the top label of the control.
wow.  One sentence in that last paragraph didn't come our as intended!  What I meant to say was that the more the panel is moved across the form, the worse the
distance between the current cursor position and the panel itself gets (when mouse dragging the panel) until, eventually, the cursor no longer appears to be on top of the panel.
Sorry...I hadn't even tried moving it with the mouse.  I'll take a look.
In your Load() event of ListBoxExt, add a line to also wire up the MouseDown() event of Label1:

        AddHandler Panel1.Scroll, AddressOf Panel1_Scroll
        AddHandler Label1.MouseMove, AddressOf Panel1_MouseMove
       AddHandler Label1.MouseDown, AddressOf Panel1_MouseDown ' <-- Also Handle the MouseDown() Event
        AddHandler Panel1.Resize, AddressOf Panel1_Resize

Now, add a variable to hold the position when the mouse is pressed down:

    Private pt As Point

Finally, the MouseDown() and MouseMove() should look like:

    Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            pt = New Point(e.X, e.Y)
        End If
    End Sub

    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If m_Moveable = True AndAlso e.Button = Windows.Forms.MouseButtons.Left Then
            Panel1.Location = New Point(Panel1.Location.X + (e.X - pt.X), Panel1.Location.Y + (e.Y - pt.Y))
            Me.Location = Panel1.Location
        End If
    End Sub

All, together, that would be:
Public Sub Load(ByVal frmReference As Form, ByVal Pichost As PictureBox)
        m_frmReference = frmReference
        m_PicHost = Pichost
        Panel1 = New Panel
        Panel2 = New Panel
        Label1 = New Label

        Pichost.Controls.Add(Panel1)
        Panel1.Controls.Add(Label1)
        Panel1.Controls.Add(Panel2)

        Panel2.Top = Label1.Height

        Label1.BackColor = Color.FromArgb(150, 0, 0, 102)
        Label1.Parent = Panel1
        Label1.AutoSize = False
        Label1.Dock = DockStyle.Top
        Label1.ForeColor = Color.White
        Label1.TextAlign = ContentAlignment.MiddleCenter

        Label1.AllowDrop = True
        Panel2.AllowDrop = True

        Dim myFont As New Font("Verdana", 10, FontStyle.Bold)
        Label1.Font = myFont

        Me.Parent = Pichost
        Me.BackColor = Color.Transparent
        Panel1.BackColor = Color.Transparent
        Panel1.Parent = Pichost
        Panel2.Parent = Panel1
        Panel2.Anchor = AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top + AnchorStyles.Bottom
        myControlArray = New LabelArray(Panel2)
        Panel2.BackColor = Color.FromArgb(150, 190, 190, 190)
        Panel2.AutoScroll = True

        Panel1.BringToFront()

        AddHandler Panel1.Scroll, AddressOf Panel1_Scroll
        AddHandler Label1.MouseMove, AddressOf Panel1_MouseMove
        AddHandler Label1.MouseDown, AddressOf Panel1_MouseDown ' <-- Also Handle the MouseDown() Event
        AddHandler Panel1.Resize, AddressOf Panel1_Resize

        AddHandler Panel2.DragDrop, AddressOf Panel2_DragDrop
        AddHandler Panel2.DragOver, AddressOf Panel2_DragOver

        SetStyle(ControlStyles.AllPaintingInWmPaint Or _
                    ControlStyles.DoubleBuffer Or _
                    ControlStyles.ResizeRedraw Or _
                    ControlStyles.UserPaint, _
                    True)
    End Sub

    Private pt As Point

    Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            pt = New Point(e.X, e.Y)
        End If
    End Sub

    Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If m_Moveable = True AndAlso e.Button = Windows.Forms.MouseButtons.Left Then
            Panel1.Location = New Point(Panel1.Location.X + (e.X - pt.X), Panel1.Location.Y + (e.Y - pt.Y))
            Me.Location = Panel1.Location
        End If
    End Sub

Open in new window

Thanks Idle Mind (definitely not that idle !!:)  That was just what I needed!!

Cheers!