1

みなさん、こんにちは

私は次のことを疑問に思っています(苦労しています):

私は 5 つの flowLayoutPanels と 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 に答える