dragdrop misunderstanding


P

Pascal

Hello
I am trying to understand the behavior of drag and drop between two labels.
I read the msdn :
Member name Description
All The data is copied, removed from the drag source, and
scrolled in the drop target.
Copy The data is copied to the drop target.
Link The data from the drag source is linked to the drop
target.
Move The data from the drag source is moved to the drop
target.
None The drop target does not accept the data.
Scroll Scrolling is about to start or is currently occurring
in the drop target.

So, this code, with a form, and 2 labels on it, should perform this behavior
: (label1.text disappears and label2.text contains "label1"), no ? (Or i
didn't understood : removed from the drag source)
###################
Public Class Form1
Dim m_AcceptMove = True
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
If m_AcceptMove = True Then
Label1.DoDragDrop(Label1.Text, DragDropEffects.All)
Else
Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)
End If
Me.BackColor = Color.OrangeRed
End Sub
Private Sub Label2_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragEnter
If e.Data.GetDataPresent(DataFormats.Text) Then
If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Label2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragDrop
If e.Data.GetDataPresent(DataFormats.Text) Then
If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If
Label2.Text = e.Data.GetData(DataFormats.Text).ToString
Label2.BackColor = Color.GreenYellow
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
###################
thanks for your help
pascal
 
Ad

Advertisements

L

Lloyd Sheen

Pascal said:
Hello
I am trying to understand the behavior of drag and drop between two
labels. I read the msdn :
Member name Description
All The data is copied, removed from the drag source,
and scrolled in the drop target.
Copy The data is copied to the drop target.
Link The data from the drag source is linked to the drop
target.
Move The data from the drag source is moved to the drop
target.
None The drop target does not accept the data.
Scroll Scrolling is about to start or is currently
occurring in the drop target.

So, this code, with a form, and 2 labels on it, should perform this
behavior : (label1.text disappears and label2.text contains "label1"), no
? (Or i didn't understood : removed from the drag source)
###################
Public Class Form1
Dim m_AcceptMove = True
Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
If m_AcceptMove = True Then
Label1.DoDragDrop(Label1.Text, DragDropEffects.All)
Else
Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)
End If
Me.BackColor = Color.OrangeRed
End Sub
Private Sub Label2_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragEnter
If e.Data.GetDataPresent(DataFormats.Text) Then
If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Label2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragDrop
If e.Data.GetDataPresent(DataFormats.Text) Then
If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If
Label2.Text = e.Data.GetData(DataFormats.Text).ToString
Label2.BackColor = Color.GreenYellow
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
###################
thanks for your help
pascal

Pascal,

I think the problem is that you might be asumming that the effect
happens without coding. Think of it as the following:

1st The DragDrop operation is started. At that time the source sets
what can be done as an effect.

2nd As the drag operation takes place various controls (which are dragged
over) give feedback to the OS telling it what they will allow (DragOver).

3rd When the actual Drop takes place the code in the DragDrop for the
control will set what actually happened as a return code for the DragDrop
operation.

4th The code (step 1) should then read the return from the doDragDrop
operation to find out what happened.

In your code you do not check the return from the operation and do something
appropriate. Lets say you use drag/drop to move items between lists. If
the return code is None then nothing happened (or the destination did not
set the return code). Move would mean that the operation added the items to
the destination list and expects you to delete them from the source list.
Copy means that operation added the items to the destination list but
expects that the items are now duplicated.

Both side of the operation need to know the rules and need to set the effect
(that can be done) and (that was done).

Hope this helps
LS
 
P

Pascal

Hi again and thanks for this good lesson I post again the source to be sure
I put your remarks at the right place. And ask some more questions at the
end.
#########
Public Class Form1

Dim m_AcceptMove = False
Dim bSuccessDrop As Boolean = False

Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.

If m_AcceptMove = True Then
Label1.DoDragDrop(Label1.Text, DragDropEffects.All)
Else
Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)
End If
Me.BackColor = Color.OrangeRed
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
If bSuccessDrop = True Then
Label1.Text = ""
End If
End Sub

Private Sub Label2_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).

If e.Data.GetDataPresent(DataFormats.Text) Then

If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Label2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code for
the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then

If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If

Label2.Text = e.Data.GetData(DataFormats.Text).ToString
Label2.BackColor = Color.GreenYellow
bSuccessDrop = True
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
#################
1) You told me about the DragOver, and I coded the DragEnter and It seems to
work too; Is there a difference between them?
2) What man see, whatever I choose for m_AcceptMove : true or false, is not
different. The visible behavior of the label depends only of the fourth
step, right? For for theese labels I can let "copy", no?

Thanks
pascal
 
L

Lloyd Sheen

Pascal said:
Hi again and thanks for this good lesson I post again the source to be
sure I put your remarks at the right place. And ask some more questions at
the end.
#########
Public Class Form1

Dim m_AcceptMove = False
Dim bSuccessDrop As Boolean = False

Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.

If m_AcceptMove = True Then
Label1.DoDragDrop(Label1.Text, DragDropEffects.All)
Else
Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)
End If
Me.BackColor = Color.OrangeRed
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
If bSuccessDrop = True Then
Label1.Text = ""
End If
End Sub

Private Sub Label2_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).

If e.Data.GetDataPresent(DataFormats.Text) Then

If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Label2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code
for the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then

If m_AcceptMove = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.Copy
End If

Label2.Text = e.Data.GetData(DataFormats.Text).ToString
Label2.BackColor = Color.GreenYellow
bSuccessDrop = True
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
#################
1) You told me about the DragOver, and I coded the DragEnter and It seems
to work too; Is there a difference between them?
2) What man see, whatever I choose for m_AcceptMove : true or false, is
not different. The visible behavior of the label depends only of the
fourth step, right? For for theese labels I can let "copy", no?

Thanks
pascal

Pascal,

The lines :
If bSuccessDrop = True Then
Label1.Text = ""
End If


are depending on a boolean which is fine but not the "real" way to do it.

In the DragDrop (Label2_DragDrop) you should be testing the e.AllowEffect.
You can see that change if you are dragging and then press the CTL key you
will see the drag icon change (a plus sign appears).

So:

In the DragDrop test the alloweffect. Then in the code perform your code.
Then set the e.effect to what was done.

The line Label1.DoDragDrop(Label1.Text, DragDropEffects.All) should return
the returned value. It returns a DragDropEffects which will tell you what
the destination says it did.

LS
 
P

Pascal

This is a try : is it what you was talking about ?
#############
Public Class Form1

Dim bSuccessDrop As Boolean = False

Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.

Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)
Me.BackColor = Color.OrangeRed
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.

End Sub

Private Sub Label2_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).

If e.Data.GetDataPresent(DataFormats.Text) Then

e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Label2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code for
the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then

If e.Effect = DragDropEffects.Copy Then
Label2.Text = e.Data.GetData(DataFormats.Text).ToString
Label2.BackColor = Color.GreenYellow
Label1.Text = ""
End If

Else
e.Effect = DragDropEffects.None
End If

End Sub
End Class
##############
 
Ad

Advertisements

L

Lloyd Sheen

Pascal said:
This is a try : is it what you was talking about ?
#############
Public Class Form1

Dim bSuccessDrop As Boolean = False

Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.

Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)
Me.BackColor = Color.OrangeRed
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.

End Sub

Private Sub Label2_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).

If e.Data.GetDataPresent(DataFormats.Text) Then

e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Label2_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Label2.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code
for the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then

If e.Effect = DragDropEffects.Copy Then
Label2.Text = e.Data.GetData(DataFormats.Text).ToString
Label2.BackColor = Color.GreenYellow
Label1.Text = ""
End If

Else
e.Effect = DragDropEffects.None
End If

End Sub
End Class
##############

Ok.

Label1.DoDragDrop(Label1.Text, DragDropEffects.Copy)

This tells the OS (which does most of the work for you) that you can only
Copy the information which you put into the dataobject.

What you are missing is :

dim retCode as DragDropEffects =Label1.DoDragDrop(Label1.Text,
DragDropEffects.Copy)

Then when it returns you can query the return code to see what the source
control should do.

In the destination code (DragDrop) you should set the e.effect to whatever
the destination wants (the code should check what can be done first as you
are doing but using the e.alloweffects property. So you will add a line to
the code after :

Label2.BackColor = Color.GreenYellow


Now for the big problem in your code. You have not seperated the source and
destination as far as code goes. In your DragDrop you have the line:

Label1.Text = ""

To see what can happen here start your app, select some text from this
message and drag it over Label2. When you drop it the text in Label1 should
disappear.

Drag and drop has two entities. Source is where the information comes from
and destination is where it is to be copied or moved. The source and
destination do not have to be in the same code module and not even in the
same application. That is one of the cool things about it.

The mousemove for the source does the following:

1. Sets what can happen
2. Sets the data into the dataobject
3. Starts the DoDragDrop operation
4. Checks the return code from the DoDragDrop and responds (in this case
it should clear the text if the return code is Copy.

The DragDrop for the destination does the following:

1. Checks to see if it can deal with the allowed effect
2. Checks to see if the dataobject contains something it can deal with
3. Does whatever it is to do
4. Sets the return code

LS
 
P

Pascal

Thanks for this reply, i will spend time on it to be sure I all
understand... on sturday or sunday, too much things to do now... I'll be
back ;0)

pascal
 
P

Pascal

hello
First of all, I think I should explain what I want to do so you can tell me
if this is possible.

|label1|label2|label3|label4| etc...=the labels-problems
|label5|label6|label7|label8| etc...=the labels-solution

This is a software program that generates random numbers, but respecting
aconstraint of size. These numbers appear on the labels 1 to 4. By "drag and
drop" students should place the contents of these labels in ascending order
(or low) in labels 5 to 8.
To provide a visual aid to students, I would like the label (1 to 4) change
in appearance if it is already used in one of the labels (5 to 8).
Labels 1 to 4 have the the property AllowDrop = false.
Labels 5 to 8 have the AllowDrop property = true.
If the content of the drop comes from the labels-solution, then the content
of the label-source must be clear: this looks like a move. If the content of
the drop comes from the labels-problems, then the content of the
label-source must be copied.
So the label 5 can receive some content from label 8, for example. That
complicates the way to bring a visual aid ...
Moreover, I tried to create a user control inherited from a label that would
be able to handle everything that is described above.
It is therefore much to learn at the same time, for an "old néophite" like
me .
I managed to do most of what I described with VB 2008 Express, except for
the ability of software to provide a visual aid.
I hope these explanations illuminate my first request.
pascal
 
L

Lloyd Sheen

Pascal said:
hello
First of all, I think I should explain what I want to do so you can tell
me if this is possible.

|label1|label2|label3|label4| etc...=the labels-problems
|label5|label6|label7|label8| etc...=the labels-solution

This is a software program that generates random numbers, but respecting
aconstraint of size. These numbers appear on the labels 1 to 4. By "drag
and drop" students should place the contents of these labels in ascending
order (or low) in labels 5 to 8.
To provide a visual aid to students, I would like the label (1 to 4)
change in appearance if it is already used in one of the labels (5 to 8).
Labels 1 to 4 have the the property AllowDrop = false.
Labels 5 to 8 have the AllowDrop property = true.
If the content of the drop comes from the labels-solution, then the
content of the label-source must be clear: this looks like a move. If the
content of the drop comes from the labels-problems, then the content of
the label-source must be copied.
So the label 5 can receive some content from label 8, for example. That
complicates the way to bring a visual aid ...
Moreover, I tried to create a user control inherited from a label that
would be able to handle everything that is described above.
It is therefore much to learn at the same time, for an "old néophite" like
me .
I managed to do most of what I described with VB 2008 Express, except for
the ability of software to provide a visual aid.
I hope these explanations illuminate my first request.
pascal

Pascal,

Have you done what I outlined the last time you posted this?

I gave you the steps to do what you need to do. What is not working?

LS
 
P

Pascal

I turned round, and I mix everything. All these concepts are too abstract
for me, and I admit that I'm lost. Below is the attempt of "userctrl" at the
stage where it is not working!
Even with your step lesson, I don't know how to test the retCode....
Sorry.... Few hours more and I will give up : too much hours consumming for
a poor result ! I have to yield to the fact. arrgh!

Thanks to have spend your time on it !
pascal

Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
<Category("Common control"), Browsable(True), Description("Ca c'est du label
de chez scalpa")> _
<ToolboxBitmap(GetType(Lbl_ClassLibrary.MonLabel), "MonLabel.bmp")> _
Public Class MonLabel
Inherits Windows.Forms.Label
#Region " Private declarations "

Dim b_SuccessDrop As Boolean = False
Dim b_LblProb As Boolean = False
Dim retCode As DragDropEffects
#End Region
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.MinimumSize = New Size(80, 25)
Me.Dock = DockStyle.Fill
Me.TextAlign = ContentAlignment.MiddleCenter
Me.BackColor = SystemColors.Control

End Sub
#Region "Events"
'copier le contenu du label quand clic gauche
Private Sub Me_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.
retCode = Me.DoDragDrop(Me.Text, DragDropEffects.Copy)

Me.DoDragDrop(Me.Text, DragDropEffects.Copy)
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
'I don't know what to do here to test retCode
Me.BackColor = Color.OrangeRed


End Sub
Private Sub Me_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Me_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code for
the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then
If e.Effect = DragDropEffects.Copy Then
Me.Text = e.Data.GetData(DataFormats.Text).ToString
Me.BackColor = Color.GreenYellow
If e.Effect = retCode And b_LblProb = True Then
Me.Text = ""
Else
'nothing
End If
End If

Else
e.Effect = DragDropEffects.None
End If
End Sub


#End Region

#Region "Properties"
'Private label1.Text As String
<Category("Monlabel"), _
Description("Le contenu de MonLabel")> _
Public Property LblText() As String
Get
Return Me.Text
End Get
Set(ByVal value As String)
Me.Text = value
End Set
End Property
'''-----------------------------------------------------------------------------
''' <summary>
''' Sets or returns move or not.
''' </summary>
''' <history>
''' [scalpa]
''' </history>
'''-----------------------------------------------------------------------------
<Category("Monlabel"), _
Description("Renseigne sur le succès d'un DragAndDrop"), _
DefaultValue(GetType(Boolean), "False")> _
Public Property bSuccessDrop() As Boolean
Get
Return b_SuccessDrop
End Get
Set(ByVal value As Boolean)
'If m_AcceptMove <> value Then
b_SuccessDrop = value
' End If
End Set
End Property
'''-----------------------------------------------------------------------------
''' <summary>
''' Sets or returns problème or not.
''' </summary>
''' <history>
''' [scalpa]
''' </history>
'''-----------------------------------------------------------------------------
<Category("Monlabel"), _
Description("Label problème ou non?"), _
DefaultValue(GetType(Boolean), "False")> _
Public Property bLblProb() As Boolean
Get
Return b_LblProb
End Get
Set(ByVal value As Boolean)
b_LblProb = value
End Set
End Property

#End Region
End Class
 
Ad

Advertisements

P

Pascal

ultimate shot...
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
<Category("Common control"), Browsable(True), Description("Ca c'est du label
de chez scalpa")> _
<ToolboxBitmap(GetType(Lbl_ClassLibrary.MonLabel), "MonLabel.bmp")> _
Public Class MonLabel
Inherits Windows.Forms.Label
#Region " Private declarations "

Dim b_SuccessDrop As Boolean = False
Dim b_LblProb As Boolean = False
Dim retCode As DragDropEffects
#End Region
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.MinimumSize = New Size(80, 25)
Me.Dock = DockStyle.Fill
Me.TextAlign = ContentAlignment.MiddleCenter
Me.BackColor = SystemColors.Control

End Sub
#Region "Events"
'copier le contenu du label quand clic gauche
Private Sub Me_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.

If b_LblProb = True Then 'A label-problem is causing the DragDrop,
so we copy
retCode = Me.DoDragDrop(Me.Text, DragDropEffects.Copy)
Else 'It is a "label-solution" that caused the DragDrop, so we moved
retCode = Me.DoDragDrop(Me.Text, DragDropEffects.Move)
End If

'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
'I don't know what to do here to test retCode
If retCode = DragDropEffects.Copy Then
'nothing happens
ElseIf retCode = DragDropEffects.Move Then
Me.BackColor = Color.OrangeRed
Me.Text = ""
End If

End Sub
Private Sub Me_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Me_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code for
the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then 'is there some data?
If b_LblProb = True Then 'We are over a label-problem so do
nothing

Else 'We are over a label-solution so copy or move depending on
from where are the data
If e.Effect = DragDropEffects.Copy Then
Me.Text = e.Data.GetData(DataFormats.Text).ToString
Me.BackColor = Color.GreenYellow
ElseIf e.Effect = DragDropEffects.Move Then
Me.Text = e.Data.GetData(DataFormats.Text).ToString

End If
End If


Else
e.Effect = DragDropEffects.None
End If
End Sub


#End Region

#Region "Properties"
'Private label1.Text As String
<Category("Monlabel"), _
Description("Le contenu de MonLabel")> _
Public Property LblText() As String
Get
Return Me.Text
End Get
Set(ByVal value As String)
Me.Text = value
End Set
End Property
'''-----------------------------------------------------------------------------
''' <summary>
''' Sets or returns move or not.
''' </summary>
''' <history>
''' [scalpa]
''' </history>
'''-----------------------------------------------------------------------------
<Category("Monlabel"), _
Description("Renseigne sur le succès d'un DragAndDrop"), _
DefaultValue(GetType(Boolean), "False")> _
Public Property bSuccessDrop() As Boolean
Get
Return b_SuccessDrop
End Get
Set(ByVal value As Boolean)
'If m_AcceptMove <> value Then
b_SuccessDrop = value
' End If
End Set
End Property
'''-----------------------------------------------------------------------------
''' <summary>
''' Sets or returns problème or not.
''' </summary>
''' <history>
''' [scalpa]
''' </history>
'''-----------------------------------------------------------------------------
<Category("Monlabel"), _
Description("Label problème ou non?"), _
DefaultValue(GetType(Boolean), "False")> _
Public Property bLblProb() As Boolean
Get
Return b_LblProb
End Get
Set(ByVal value As Boolean)
b_LblProb = value
End Set
End Property

#End Region
End Class
 
L

Lloyd Sheen

Pascal said:
ultimate shot...
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
<Category("Common control"), Browsable(True), Description("Ca c'est du
label de chez scalpa")> _
<ToolboxBitmap(GetType(Lbl_ClassLibrary.MonLabel), "MonLabel.bmp")> _
Public Class MonLabel
Inherits Windows.Forms.Label
#Region " Private declarations "

Dim b_SuccessDrop As Boolean = False
Dim b_LblProb As Boolean = False
Dim retCode As DragDropEffects
#End Region
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
Me.MinimumSize = New Size(80, 25)
Me.Dock = DockStyle.Fill
Me.TextAlign = ContentAlignment.MiddleCenter
Me.BackColor = SystemColors.Control

End Sub
#Region "Events"
'copier le contenu du label quand clic gauche
Private Sub Me_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.

If b_LblProb = True Then 'A label-problem is causing the DragDrop,
so we copy
retCode = Me.DoDragDrop(Me.Text, DragDropEffects.Copy)
Else 'It is a "label-solution" that caused the DragDrop, so we
moved
retCode = Me.DoDragDrop(Me.Text, DragDropEffects.Move)
End If

'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
'I don't know what to do here to test retCode
If retCode = DragDropEffects.Copy Then
'nothing happens
ElseIf retCode = DragDropEffects.Move Then
Me.BackColor = Color.OrangeRed
Me.Text = ""
End If

End Sub
Private Sub Me_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).
If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub Me_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code
for the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then 'is there some
data?
If b_LblProb = True Then 'We are over a label-problem so do
nothing

Else 'We are over a label-solution so copy or move depending on
from where are the data
If e.Effect = DragDropEffects.Copy Then
Me.Text = e.Data.GetData(DataFormats.Text).ToString
Me.BackColor = Color.GreenYellow
ElseIf e.Effect = DragDropEffects.Move Then
Me.Text = e.Data.GetData(DataFormats.Text).ToString

End If
End If


Else
e.Effect = DragDropEffects.None
End If
End Sub


#End Region

#Region "Properties"
'Private label1.Text As String
<Category("Monlabel"), _
Description("Le contenu de MonLabel")> _
Public Property LblText() As String
Get
Return Me.Text
End Get
Set(ByVal value As String)
Me.Text = value
End Set
End Property

'''-----------------------------------------------------------------------------
''' <summary>
''' Sets or returns move or not.
''' </summary>
''' <history>
''' [scalpa]
''' </history>

'''-----------------------------------------------------------------------------
<Category("Monlabel"), _
Description("Renseigne sur le succès d'un DragAndDrop"), _
DefaultValue(GetType(Boolean), "False")> _
Public Property bSuccessDrop() As Boolean
Get
Return b_SuccessDrop
End Get
Set(ByVal value As Boolean)
'If m_AcceptMove <> value Then
b_SuccessDrop = value
' End If
End Set
End Property

'''-----------------------------------------------------------------------------
''' <summary>
''' Sets or returns problème or not.
''' </summary>
''' <history>
''' [scalpa]
''' </history>

'''-----------------------------------------------------------------------------
<Category("Monlabel"), _
Description("Label problème ou non?"), _
DefaultValue(GetType(Boolean), "False")> _
Public Property bLblProb() As Boolean
Get
Return b_LblProb
End Get
Set(ByVal value As Boolean)
b_LblProb = value
End Set
End Property

#End Region
End Class

So what happens when you do this?

LS
 
P

Pascal

Hello again
I gave up with the previous code, too difficult in an usercontrol for me.
I try again from the beginning, and will try to manage the behavior of the
label directly in the code of the form. Can you post a sample code which
show me How works what you described before?
the goal is :
The backcolor of label2 will change only if the drop is consummed from
label1 to Label2
in another way label1.text = "12" and label2 is empty, at the beginning. If
I drag label1 over label2 nothing happens until I drop label1on label2. And
because now label2.text= label1.text, label1.backcolor= color.olive
thanks
pascal
 
L

Lloyd Sheen

Pascal said:
Hello again
I gave up with the previous code, too difficult in an usercontrol for me.
I try again from the beginning, and will try to manage the behavior of the
label directly in the code of the form. Can you post a sample code which
show me How works what you described before?
the goal is :
The backcolor of label2 will change only if the drop is consummed from
label1 to Label2
in another way label1.text = "12" and label2 is empty, at the beginning.
If I drag label1 over label2 nothing happens until I drop label1on label2.
And because now label2.text= label1.text, label1.backcolor= color.olive
thanks
pascal

Pascal,

Nothing is supposed to happen unless you drop the object. That is the
way Drag/Drop works. Simply draging something over something else just asks
the object which is being dragged over to indicate what functions it will
accept.

LS
 
P

Pascal

This code do the job for the string but not for the backcolor!!! I think i
don't test the good thing.

#Region "Drag & Drop"
Dim retCode As DragDropEffects
Dim couleur As Color

Private Sub LblProb_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Label1.MouseDown, Label2.MouseDown, Label3.MouseDown,
Label4.MouseDown, Label5.MouseDown, _
Label6.MouseDown, Label7.MouseDown, Label8.MouseDown,
Label9.MouseDown, Label10.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.
retCode = DragDropEffects.Copy
sender.DoDragDrop(sender.Text, DragDropEffects.Copy)
couleur = sender.backcolor
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
If retCode = DragDropEffects.Copy Then
sender.BackColor = couleur
ElseIf retCode = DragDropEffects.Move Then
sender.BackColor = couleur
sender.text = ""
End If

End Sub
Private Sub LblSoluce_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Label11.MouseDown, Label12.MouseDown, Label13.MouseDown,
Label14.MouseDown, Label15.MouseDown, _
Label16.MouseDown, Label17.MouseDown, Label18.MouseDown,
Label19.MouseDown, Label20.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.
retCode = DragDropEffects.Move
couleur = sender.backcolor
sender.DoDragDrop(sender.Text, DragDropEffects.Move)
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
If retCode = DragDropEffects.Copy Then
sender.BackColor = couleur
ElseIf retCode = DragDropEffects.Move Then
sender.BackColor = couleur
sender.text = ""
End If

End Sub

Private Sub LblSoluce_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) _
Handles Label11.DragEnter, Label12.DragEnter, Label13.DragEnter,
Label14.DragEnter, Label15.DragEnter, _
Label16.DragEnter, Label17.DragEnter, Label18.DragEnter,
Label19.DragEnter, Label20.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).

If e.Data.GetDataPresent(DataFormats.Text) Then
If retCode = DragDropEffects.Copy Then
e.Effect = DragDropEffects.Copy
ElseIf retCode = DragDropEffects.Move Then
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub LblSoluce_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) _
Handles Label11.DragDrop, Label12.DragDrop, Label13.DragDrop,
Label14.DragDrop, Label15.DragDrop, _
Label16.DragDrop, Label17.DragDrop, Label18.DragDrop,
Label19.DragDrop, Label20.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code
for the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then
retCode = e.Effect
If e.Effect = DragDropEffects.Copy Then
sender.Text = e.Data.GetData(DataFormats.Text).ToString
ElseIf e.Effect = DragDropEffects.Move Then
sender.Text = e.Data.GetData(DataFormats.Text).ToString
End If

Else
e.Effect = DragDropEffects.None
End If

End Sub


#End Region
 
Ad

Advertisements

L

Lloyd Sheen

Pascal said:
This code do the job for the string but not for the backcolor!!! I think i
don't test the good thing.

#Region "Drag & Drop"
Dim retCode As DragDropEffects
Dim couleur As Color

Private Sub LblProb_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Label1.MouseDown, Label2.MouseDown, Label3.MouseDown,
Label4.MouseDown, Label5.MouseDown, _
Label6.MouseDown, Label7.MouseDown, Label8.MouseDown,
Label9.MouseDown, Label10.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.
retCode = DragDropEffects.Copy
sender.DoDragDrop(sender.Text, DragDropEffects.Copy)
couleur = sender.backcolor
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
If retCode = DragDropEffects.Copy Then
sender.BackColor = couleur
ElseIf retCode = DragDropEffects.Move Then
sender.BackColor = couleur
sender.text = ""
End If

End Sub
Private Sub LblSoluce_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles Label11.MouseDown, Label12.MouseDown, Label13.MouseDown,
Label14.MouseDown, Label15.MouseDown, _
Label16.MouseDown, Label17.MouseDown, Label18.MouseDown,
Label19.MouseDown, Label20.MouseDown
'1st The DragDrop operation is started. At that time, the
source sets what can be done
' as an effect.
retCode = DragDropEffects.Move
couleur = sender.backcolor
sender.DoDragDrop(sender.Text, DragDropEffects.Move)
'4th The code (step 1) should then read the return from the
doDragDrop
' operation to find out what happened.
If retCode = DragDropEffects.Copy Then
sender.BackColor = couleur
ElseIf retCode = DragDropEffects.Move Then
sender.BackColor = couleur
sender.text = ""
End If

End Sub

Private Sub LblSoluce_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) _
Handles Label11.DragEnter, Label12.DragEnter, Label13.DragEnter,
Label14.DragEnter, Label15.DragEnter, _
Label16.DragEnter, Label17.DragEnter, Label18.DragEnter,
Label19.DragEnter, Label20.DragEnter
'2nd As the drag operation takes place various controls (which
are dragged over) give feedback
' to the OS telling it what they will allow (DragOver).

If e.Data.GetDataPresent(DataFormats.Text) Then
If retCode = DragDropEffects.Copy Then
e.Effect = DragDropEffects.Copy
ElseIf retCode = DragDropEffects.Move Then
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub LblSoluce_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) _
Handles Label11.DragDrop, Label12.DragDrop, Label13.DragDrop,
Label14.DragDrop, Label15.DragDrop, _
Label16.DragDrop, Label17.DragDrop, Label18.DragDrop,
Label19.DragDrop, Label20.DragDrop
'3rd When the actual Drop takes place the code in the DragDrop
for the
' control will set what actually happened as a return code
for the DragDrop
' operation.
If e.Data.GetDataPresent(DataFormats.Text) Then
retCode = e.Effect
If e.Effect = DragDropEffects.Copy Then
sender.Text = e.Data.GetData(DataFormats.Text).ToString
ElseIf e.Effect = DragDropEffects.Move Then
sender.Text = e.Data.GetData(DataFormats.Text).ToString
End If

Else
e.Effect = DragDropEffects.None
End If

End Sub


#End Region

Pascal,

First thing to change are your source code options. Put as the first
line in your code:

Option Strict On
Option Explicit On

What this will do for you is ensure that there will not be run time
errors because you are assuming things.

The line couleur = sender.backcolor is an example. Sender is an object.
It may or may not have a backcolor property. In your case it most likely
does but this is bad practice. It may not be the source of your problem but
then again it might. There may be some extra coding to make this work but
in the long run it will save you debugging time.
retCode = DragDropEffects.Copy
sender.DoDragDrop(sender.Text, DragDropEffects.Copy)

Those lines are a real problem. Please don't take this wrong but what is
your programming experience? DoDragDrop is a function which returns a
value. The value in this case is the DragDropEffects return code set in the
destination. So you should have code:

dim retCode as DragDropEffects
retCode = sender.DoDragDrop(sender.Text, DragDropEffects.Copy)

Then you can test the retCode.

LS
 
P

Pascal

Hi Lloyd
Thanks again for theese comments.
I am 47 and I have no experience in programming... It's just a desire to
make small programs allowing my young students make progress in mathematics.
There are programs for academic learning, but they are often expensive,
complicated as they deal with different skills together. I did not study
informatic-science; what I know, I learned it alone, and each time, because
I needed for my job: to teach them better! That's the little story. The code
of these small programs would scream a programmer of business!
Imagine that all the discussions we have had, is equivalent to a
conversation between a Chinese and a Frenchman who does not know this
language!
I'll try to put your remarks into practice, thank you.
pascal
 
P

Pascal

In this case, how to deal with all theese labels in a unique sub
(LblProb_MouseDown) without using sender.DoDragDrop(sender.Text,
DragDropEffects.Copy)?
 
Ad

Advertisements

L

Lloyd Sheen

Pascal said:
In this case, how to deal with all theese labels in a unique sub
(LblProb_MouseDown) without using sender.DoDragDrop(sender.Text,
DragDropEffects.Copy)?

Pascal,
I am working up a small example which I will post later today sometime.

LS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top