开发者

Drop onto FlowLayoutPanel

开发者 https://www.devze.com 2023-04-04 04:35 出处:网络
Hi guys Hope all is well I am wondering(struggling) the following: I have 5 flowLayoutPanels and 5 PictureBoxes i want to be able to move anyone of the picture boxes over anyone the FLP at run time

Hi guys Hope all is well

I am wondering(struggling) the following:

I have 5 flowLayoutPanels and 5 PictureBoxes i want to be able to move anyone of the picture boxes over anyone the FLP at run time and have the layout panel add it to FLP.controls.Add()....

I've been at it for Hours and now ill swallow my pride -

I have done the following To get it working, but here i have to manually specify which PixBox intersects with which FLP and i dont want 25 if statements

开发者_开发技巧Private Sub cpbPic1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cpbPic1.MouseUp
    If (flpDock1.HasChildren = False) Then 'Test to see if panel is filled
        If CBool(CustomPictureBox.IntersectingObjects(cpbPic1, flpDock1)) Then
            flpDock1.Controls.Add(cpbPic1) 'Add Pic to Panel
    End If
End Sub

cpb: CustomPictureBox


you could always do this:

Private Sub cpbPic1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cpbPic1.MouseUp, cpbPic2.MouseUp, cpbPic3.MouseUp,cpbPic4.MouseUp,cpbPic5.MouseUp
    If Not flpDock1.HasChildren Then 'Test to see if panel is filled
        If CBool(CustomPictureBox.IntersectingObjects(TryCast(sender,CustomPictureBox), flpDock1)) Then
            flpDock1.Controls.Add(TryCast(sender,CustomPictureBox)) 'Add Pic to Panel
    End If
End Sub

This will reduce the amount of code you'll have to write significantly, you can further reduce this amount if you think about how to utilize the fact that the event handler passes the Object which raises the flag, like I did here.

Also you can use arbitrary big amount (i think) of objects in a handler as long as they raise the same event


well this can be a work around for what you want to do. you also have to enable allowdrop to the flowpanels

    Private Function FindControl(ByVal ControlName As String, ByVal CurrentControl As Control) As Control 
' get the control you need
    Dim ctr As Control
    For Each ctr In CurrentControl.Controls
        If ctr.Name = ControlName Then
            Return ctr
        Else
            ctr = FindControl(ControlName, ctr)
            If Not ctr Is Nothing Then
                Return ctr
            End If
        End If
    Next ctr
End Function

Private Sub me_DragEnter(sender As Object, e As DragEventArgs) Handles FLP1.DragEnter,FLP2.DragEnter,FLP3.DragEnter
' call the copy effect
    If (e.Data.GetDataPresent(DataFormats.Text)) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub
Private Sub me_DragDrop(sender As Object, e As DragEventArgs) Handles FLP1.DragDrop,FLP2.DragDrop,FLP3.DragDrop
' get the FLp you're gonna drop the control onto
    Dim c As control =FindControl(e.Data.GetData(DataFormats.Text), me)
    sender.Controls.Add(c)
    end sub


    Private Sub Pictureboxs_MouseDown(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown, PB.MouseDown
    sender.DoDragDrop(sender.Name, DragDropEffects.Copy)

End Sub

hope that this helps you :) (sorry for my bad english)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号