放到FlowLayoutPanel上

时间:2011-09-10 20:28:58

标签: vb.net drag-and-drop flowlayoutpanel

嗨,大家好希望一切顺利

我想知道(挣扎)以下内容:

我有5个flowLayoutPanel和5个PictureBox我希望能够在运行时将任何图片框移动到任何人的FLP上,并让布局面板将其添加到FLP.controls.Add()....

我已经参加了几个小时的活动,现在却吞噬了我的骄傲 -

我已经完成了下面的工作,但是我必须手动指定哪个PixBox与哪个FLP相交,我不想要25个if语句

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

2 个答案:

答案 0 :(得分:1)

你总是可以这样做:

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

这将减少您必须编写的代码量,如果您考虑如何利用事件处理程序传递引发标志的Object的事实,您可以进一步减少此数量,就像我在这里做的那样。

你也可以在处理程序中使用任意大量(我认为)的对象,只要它们引发相同的事件

答案 1 :(得分:1)

这可以解决你想做的事情。 你还必须启用allowdrop到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

希望这可以帮到你:)(抱歉我的英语不好)

相关问题