• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

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
0
sharizod
Asked:
sharizod
  • 4
  • 4
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
In your Load() method, you are adding Panel1 to "PicHost":

        Pichost.Controls.Add(Panel1)

Then, a few lines down, you also add it to the Form:

        frmReference.Controls.Add(Panel1)

This supersedes the previous assignment as a control can only be contained by ONE container.  When you add it to the form, it is no longer part of the custom "ListBoxExt" Panel.  You are merely handling the events on Panel1 in your custom  Panel.

Therefore, to get Panel1 to move with the custom Panel, you'd have to manually set its Location() whenever the main custom Panel moves (this could be done with code in the custom Panel):

    Private Sub ListBoxExt_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Panel1.Location = Me.Location
    End Sub
0
 
sharizodAuthor Commented:
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!!!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sharizodAuthor Commented:
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.
0
 
sharizodAuthor Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry...I hadn't even tried moving it with the mouse.  I'll take a look.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

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

Cheers!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now