Help Solve Addhandler for Drag and Drop using UserControl

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

GodspeedSupreme

Guest
Hi..

I need help fixing problem in my usercontrol.. where I added some handlers and the handlers didnt work, When I add Group and add cards.. drog and drop function between cards doesnt work.. and why is it when I add to many groups.. the flowlayoutpanel autosize its height. it suppose to only adjust its width.


Code Form1:

Public Class Form1

Private dragcursor As Cursor, dragtype As Type
Dim i As Integer = 1
Dim cards As Object

Private Sub FlowLayoutPanel1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs)
e.UseDefaultCursors = False
Cursor.Current = Me.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 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))
Me.dragcursor = New Cursor(bmp.GetHicon)
End Using
Me.dragtype = source.GetType
Me.FlowLayoutPanel1.DoDragDrop(source, DragDropEffects.Move)
Me.dragcursor.Dispose()

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 = "card" & i.ToString
cards.AllowDrop = True
AddHandler cards.DragEnter, AddressOf FlowLayoutPanel1_DragEnter
AddHandler cards.DragDrop, AddressOf flowLayoutPanel1_DragDrop
AddHandler cards.GiveFeedback, AddressOf FlowLayoutPanel1_GiveFeedback
Me.FlowLayoutPanel1.Controls.Add(cards)
i += 1
End Sub


End Class

Code Card UserControl:

Public Class Card
Dim panels As New List(Of cardPanel)
Dim labelPosdx As Size
Private dragcursor As Cursor, dragtype As Type

Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
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 = InputBox("Enter caption")
p.Parent.Controls.Add(newPanel)
panels.Add(newPanel)
p.Top = newPanel.Bottom + 6
AddHandler newPanel.MouseDown, AddressOf childs_MouseDown
ComboBox1.Items.Add(panels.Count - 1)
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



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))
Me.dragcursor = New Cursor(bmp.GetHicon)
End Using
Me.dragtype = source.GetType
Me.FlowLayoutPanel1.DoDragDrop(source, DragDropEffects.Move)
Me.dragcursor.Dispose()

End Sub


Private Sub Card_Load(sender As Object, e As EventArgs)


End Sub


Dim i As Integer = 1
Dim cards As Object

Private Sub FlowLayoutPanel1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs)
e.UseDefaultCursors = False
Cursor.Current = Me.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

End Class

Code cardPanel 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

Continue reading...
 
Back
Top