Help Fix Drag and Drop between Flowlayoutpanel, When UserControl is Added

  • Thread starter Thread starter GodspeedSupreme
  • Start date Start date
G

GodspeedSupreme

Guest
Hi..

I got problems where Im going to put addhandler to enable drop and drop between flowlayoutpanels..

In my form1.. I have docked a flowlayoutpanel.. for testing purposes.. I have a button in my flowlayoutpanel1, which is named "Add group", When I press Add Group, It calls a UserControl which I created.

In my UserControl which is named "Card" A groupbox is docked into the usercontrol and then a flowlayoutpanel is docked into the groupbox, and 1 panel is in my flowlayoutpanel which is named "Add Card", the panel is actually a click button and uses an "Component" which is named "cardPanel" everytime I click the panel.

So back in my form 1, I would press "Add Group" it would call the UserControl which is "Card" and within the card there is the the click button which is actually a "panel" , everytime I click the panel.. it creates a panel inside the flowlayoutpanel of the "UserControl (Card)"

Sample Pic:

8edd5861a39f60c2c48d78f0f97c539b._.png



The Problem is.. I dont know how to properly put the dragenter, dragdrop and mousedown handler evertime I add a group, Sample1 should be draggable to Sample2, and if I add another group, it should also be draggable in Card3.. and its added controls can also be draggable to card1 and card2

Pic of my project:

cee699735a3b21900a2480f8ed3c1ead._.png



Codes of Form1:

Public Class Form1


Dim i As Integer = 1

Private Sub FlowLayoutPanel1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs)
e.UseDefaultCursors = False
Cursor.Current = dragcursor
End Sub

Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
If e.AllowedEffect = DragDropEffects.Move AndAlso e.Data.GetDataPresent(dragtype) Then
e.Effect = DragDropEffects.Move
End If
End Sub

Private Sub flowLayoutPanel1_DragDrop(sender As Object, e As DragEventArgs)
Dim source As Control = CType(e.Data.GetData(dragtype), Control)
Dim _destination As Control = Me.FlowLayoutPanel1.GetChildAtPoint(Me.FlowLayoutPanel1.PointToClient(New Point(e.X, e.Y)))
source = DirectCast(e.Data.GetData(GetType(Panel)), Panel)
Dim _destination As FlowLayoutPanel = DirectCast(sender, FlowLayoutPanel)
Dim _source As FlowLayoutPanel = DirectCast(source.Parent, FlowLayoutPanel)

If _source IsNot _destination Then
Add control to panel
_destination.Controls.Add(source)

Reorder
Dim p As Point = _destination.PointToClient(New Point(e.X, e.Y))
Dim item = _destination.GetChildAtPoint(p)
Dim index As Integer = _destination.Controls.GetChildIndex(item, False)
_destination.Controls.SetChildIndex(source, index)

Invalidate to paint!
_destination.Invalidate()
_source.Invalidate()
Else
Just add the control to the new panel.
No need to remove from the other panel, this changes the Control.Parent property.
Dim p As Point = _destination.PointToClient(New Point(e.X, e.Y))
Dim item = _destination.GetChildAtPoint(p)
Dim index As Integer = _destination.Controls.GetChildIndex(item, False)
_destination.Controls.SetChildIndex(source, index)
_destination.Invalidate()
End If
End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cards As New Card
cards.Location = New System.Drawing.Point(58, 38)
cards.Size = New System.Drawing.Size(206, 324)
cards.Name = "Group" & i.ToString
cards.GroupBox1.Text = "card" & i.ToString

FlowLayoutPanel1.Controls.Add(cards)
AddHandler cards.FlowLayoutPanel1.GiveFeedback, AddressOf FlowLayoutPanel1_GiveFeedback
AddHandler cards.FlowLayoutPanel1.DragEnter, AddressOf FlowLayoutPanel1_DragEnter
AddHandler cards.FlowLayoutPanel1.DragDrop, AddressOf flowLayoutPanel1_DragDrop
AddHandler cards.FlowLayoutPanel1.MouseDown, AddressOf childs_MouseDown


i += 1

End Sub

Private Sub childs_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

Dim source As Control = CType(sender, Control)
Using bmp As New Bitmap(source.Width, source.Height)
source.DrawToBitmap(bmp, New Rectangle(Point.Empty, source.Size))
dragcursor = New Cursor(bmp.GetHicon)
End Using
dragtype = source.GetType
FlowLayoutPanel1.DoDragDrop(source, DragDropEffects.Move)
dragcursor.Dispose()

End Sub

End Class



Codes in cardPanel(an Component):

Public Class cardPanel
Inherits Panel

Private _percentage As Integer
Public Property percentage() As Integer
Get
Return _percentage
End Get
Set(ByVal value As Integer)
_percentage = value
End Set
End Property

Private _text As String
Public Shadows Property Text() As String
Get
Return _text
End Get
Set(ByVal value As String)
_text = value
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(212, 157, 93)), New Rectangle(0, 0, CInt((MyBase.Width / 100) * percentage), 6))
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(Text, MyBase.Font, Brushes.Black, New Rectangle(Point.Empty, MyBase.Size), sf)
MyBase.OnPaint(e)
End Sub



End Class


Codes for my Card (an UserControl):

Public Class Card
Dim panels As New List(Of cardPanel)
Dim labelPosdx As Size
Dim x As Integer
Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
Dim retString As String = InputBox("Enter Some Info", "Info...", "", -1, -1)
If retString.Trim <> "" Then
Dim p As Panel = DirectCast(sender, Panel)
Dim newPanel As New cardPanel
newPanel.Left = 6
newPanel.Top = If(p.Parent.Controls.OfType(Of cardPanel).Count > 0, p.Parent.Controls.OfType(Of cardPanel).Max(Function(cp) cp.Bottom) + 6, 19)
newPanel.Width = p.Width
newPanel.Height = 36
newPanel.BackColor = Color.White
newPanel.percentage = 0
newPanel.Text = retString
newPanel.Name = "Card" & x.ToString
p.Parent.Controls.Add(newPanel)
panels.Add(newPanel)
p.Top = newPanel.Bottom + 6
x += 1

End If
End Sub

Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
Dim p As Panel = DirectCast(sender, Panel)
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
e.Graphics.DrawString("Add card", p.Font, Brushes.Black, New Rectangle(Point.Empty, p.Size), sf)

End Sub



End Class



Codes of my Module:

Module Module1
Public dragcursor As Cursor, dragtype As Type
End Module


the Sample App Download Link: mediafirelink

I need you help.. Thank you for you time and assistance.

Continue reading...
 
Back
Top