1

大家好 希望一切都好

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

我有 5 个 flowLayoutPanel 和 5 个 PictureBoxes 我希望能够在运行时将任何图片框移动到任何 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:自定义图片框

4

2 回答 2

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

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

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

于 2011-09-10T20:42:49.493 回答
1

好吧,这可以解决您想做的事情。您还必须对流程面板启用 allowdrop

    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

希望这可以帮助你:)(对不起我的英语不好)

于 2017-04-18T17:34:21.460 回答