Drag & Drop de plusieurs boutons sur plusieurs panels

cs_RvaN Messages postés 4 Date d'inscription mardi 2 décembre 2003 Statut Membre Dernière intervention 1 février 2007 - 1 févr. 2007 à 23:49
cs_Amazing Messages postés 35 Date d'inscription lundi 23 juin 2003 Statut Membre Dernière intervention 17 avril 2010 - 4 févr. 2007 à 23:10
Bonjour à tous,

Je sollicite votre aide. Je suis en train de réaliser un projet : une form avec 4 panels. Dans chacun des panels, il y a plusieurs boutons qui font référence à des tables. Je dois avoir la possibilité de déplacer un bouton d'un panel vers un autre panel avec un drag&drop. On peut aussi rajouter des boutons dans les panels.
Je sais qu'on a la possibilité de faire une fonction pour chaque panel et une fonction de mousedown pour chaque bouton. Mais mon problème est que si on rajoute un bouton, on ne pourra pas le déplacer. De plus, j'aimerais que le code soit un peu plus propre en faisant des boucles (for..each) et addhandle. Mais je ne sais pas comment il saurait lequel des boutons j'ai cliquer dessus. 
Quelqu'un pourrait il m'aider?

PS : Je développe en vb .net 2003

Merci pour votre aide.

Erwan

1 réponse

cs_Amazing Messages postés 35 Date d'inscription lundi 23 juin 2003 Statut Membre Dernière intervention 17 avril 2010 2
4 févr. 2007 à 23:10
Salut,

Plusieurs questions dans ton post

-->'Mais je ne sais pas comment il saurait lequel des boutons j'ai cliquer dessus. '

Dim BtnDemo
As Button

Dim BtnDemo1
As Button

Public
Sub
New()

BtnDemo =
New Button
BtnDemo1 =
New Button

AddHandler BtnDemo.Click,
AddressOf ClicSurBouton

AddHandler BtnDemo1.Click,
AddressOf ClicSurBouton

End
Sub

Private
Sub ClicSurBouton(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
' Handles Button1.Click
Select
Case
CType(sender, Button).Name

Case
Is =
"BtnDemo"
'TODO'
Case
Is =
"BtnDemo1"
'TODO'
End
Select
End
SubPour tes questions sur les drapdrop ,je resume ,en 4 points
1 propriete .allowdrop=true du control qui recoit le drop
2 indiqué le controle qui fait le dragdrop
Dim dropEffect
As DragDropEffects =PictureBoxExemple.DoDragDrop(PictureBoxExemple, DragDropEffects.Move)

3 Receptionné le drapdrop dans le controle choisi:
Private
Sub Panel1_DragDrop(
ByVal sender
As
Object,
ByVal e
As System.Windows.Forms.DragEventArgs)
Handles Panel1.DragDrop

End
Sub
4  recupere les données dans e.Data.GetData()

Ci-dessous ,petite extrait, modifié pour ce post, d'un code que j'avais fait qui utilise le dragdrop :

Public

Class Form1

Inherits System.Windows.Forms.Form#

Region " Code généré par le Concepteur Windows Form "

Public
Sub
New()

MyBase.New()

'Cet appel est requis par le Concepteur Windows Form.InitializeComponent()

'Ajoutez une initialisation quelconque après l'appel InitializeComponent()

End
Sub

'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.

Protected
Overloads
Overrides
Sub Dispose(
ByVal disposing
As
Boolean)

If disposing
Then

If
Not (components
Is
Nothing)
Thencomponents.Dispose()

End
If

End
If

MyBase.Dispose(disposing)

End
Sub

'Requis par le Concepteur Windows Form

Private components
As System.ComponentModel.IContainer

'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form

'Elle peut être modifiée en utilisant le Concepteur Windows Form.

'Ne la modifiez pas en utilisant l'éditeur de code.

Friend
WithEvents Panel1
As System.Windows.Forms.Panel

Friend
WithEvents PictureBoxExemple
As System.Windows.Forms.PictureBox<System.Diagnostics.DebuggerStepThrough()>

Private
Sub InitializeComponent()

Me.PictureBoxExemple =
New System.Windows.Forms.PictureBox

Me.Panel1 =
New System.Windows.Forms.Panel

Me.SuspendLayout()

'

'PictureBoxExemple

'

Me.PictureBoxExemple.BackColor = System.Drawing.SystemColors.ActiveBorder

Me.PictureBoxExemple.Location =
New System.Drawing.Point(14, 43)

Me.PictureBoxExemple.Name = "PictureBoxExemple"

Me.PictureBoxExemple.TabIndex = 0

Me.PictureBoxExemple.TabStop =
False

'

'Panel1

'

Me.Panel1.AllowDrop =
True

Me.Panel1.BackColor = System.Drawing.Color.FromArgb(
CType(255,
Byte),
CType(224,
Byte),
CType(192,
Byte))

Me.Panel1.Location =
New System.Drawing.Point(435, 52)

Me.Panel1.Name = "Panel1"

Me.Panel1.Size =
New System.Drawing.Size(200, 173)

Me.Panel1.TabIndex = 1

'

'Form1

'

Me.AutoScaleBaseSize =
New System.Drawing.Size(5, 13)

Me.ClientSize =
New System.Drawing.Size(718, 266)

Me.Controls.Add(
Me.Panel1)

Me.Controls.Add(
Me.PictureBoxExemple)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(
False)

End
Sub#

End
Region

Dim dragBoxFromMouseDown
As
New Rectangle

Private
Sub PictureBox_MouseDown(
ByVal sender
As System.Object,
ByVal e
As System.Windows.Forms.MouseEventArgs)
Handles PictureBoxExemple.MouseDown

Dim dragSize As Size SystemInformation.DragSizedragSize

New Size(
CInt(
Me.PictureBoxExemple.Width / 2),
CInt(
Me.PictureBoxExemple.Height / 2))dragBoxFromMouseDown =

New Rectangle(
New Point(
CInt(e.X - (dragSize.Width / 2)), _

CInt(e.Y - (dragSize.Height / 2))), dragSize)

End
Sub

Private
Sub PictureBox_MouseMove(
ByVal sender
As System.Object,
ByVal e
As System.Windows.Forms.MouseEventArgs)
Handles PictureBoxExemple.MouseMove

If (Rectangle.op_Inequality(dragBoxFromMouseDown, Rectangle.Empty)
And _

Not dragBoxFromMouseDown.Contains(e.X, e.Y))
Then

Dim dropEffect
As DragDropEffects = PictureBoxExemple.DoDragDrop(PictureBoxExemple, DragDropEffects.Move)

Select
Case dropEffect

Case DragDropEffects.Move

Case DragDropEffects.None

End
Select

End
If

End
Sub

Private
Sub PictureBox_MouseUp(
ByVal sender
As System.Object,
ByVal e
As System.Windows.Forms.MouseEventArgs)
Handles PictureBoxExemple.MouseUpdragBoxFromMouseDown = Rectangle.Empty

End
Sub

Private
Sub Panel1_DragEnter(
ByVal sender
As
Object,
ByVal e
As System.Windows.Forms.DragEventArgs)
Handles Panel1.DragEntere.Effect = DragDropEffects.Move

End
Sub

Private
Sub Panel1_DragDrop(
ByVal sender
As
Object,
ByVal e
As System.Windows.Forms.DragEventArgs)
Handles Panel1.DragDrop

If e.Data.GetDataPresent(
GetType(PictureBox))
ThenPanel1.BackColor =

CType(e.Data.GetData(
GetType(PictureBox)), PictureBox).BackColor

End
IfdragBoxFromMouseDown = Rectangle.Empty

Windows.Forms.Cursor.Current = Cursors.Default

End
Sub

Private
Sub Panel1_GiveFeedback(
ByVal sender
As
Object,
ByVal e
As System.Windows.Forms.GiveFeedbackEventArgs)
Handles Panel1.GiveFeedbacke.UseDefaultCursors =

False

dragBoxFromMouseDown = Rectangle.Empty

End
SubEnd

ClassCordialement

Amazing
0
Rejoignez-nous