Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drag, Drop and GiveFeedback 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
Could someone tell me please how to get GiveFeedback to fire?

I have searched google for a good example using "drag drop net GiveFeedback", and nothing I can use comes back.
Is D&D that bad? It seems so easy until you try to change the cursor like I want to do.

I can't use the default D&D cursors. My users are used to seeing whatever icon is in the list, moved down to editing.

I'll repost code here that I posted in another thread (sorry).
If I am doing it wrong, then please tell me. I'm pulling my hair out over this. It should not be this hard.

LOL - VB6 was a lot easer to learn. But maybe I was a bit younger then. LOL

Thanks ahead of time.


I placed a label and a textbox on a form - no other code
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	TextBox2.AllowDrop = True
End Sub


Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
	Debug.WriteLine("Label1_MouseDown b4 Drop")

	' This is the start of a D&D operation
	' Start a drag.
	'                 Data to drag, What effects? 
	DoDragDrop(Label1.Text, DragDropEffects.Copy)

	'This will not fire until the D&D is finished.
	Debug.WriteLine("Label1_MouseDown after Drop")
End Sub


Private Sub TextBox2_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragOver
  Debug.WriteLine("TextBox2_DragOver")
End Sub


Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
	Debug.WriteLine("TextBox2_DragEnter")

	' This is the second phase of a D&D operation
	'Check for any data to drop

	If e.Data.GetDataPresent(DataFormats.Text) Then
		' There is Label data. Allow copy.
		e.Effect = DragDropEffects.Copy

		' Highlight the control.
		TextBox2.BorderStyle = BorderStyle.FixedSingle
	Else
		' There is no Label. Prohibit drop.
		e.Effect = DragDropEffects.None
	End If
End Sub


Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
	Debug.WriteLine("TextBox2_DragDrop")

	TextBox2.Text = e.Data.GetData(ataFormats.Text).ToString
End Sub


Private Sub TextBox2_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.DragLeave
	Debug.WriteLine("TextBox2_DragLeave")

End Sub


' Tried GiveFeedback with both the label and textbox.  Neither would fire when a breakpoint was placed inside of the sub.

Private Sub Label1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles Label1.GiveFeedback
	Debug.WriteLine("Label1_GiveFeedback")

	e.UseDefaultCursors = False
	If ((e.Effect And DragDropEffects.Move) = DragDropEffects.Move) Then
		Cursor.Current = Cursors.Hand
	Else
		Cursor.Current = Cursors.SizeAll
	End If
End Sub

Sometimes it's fun to mess around and see what things do. I learned a lot that way. But it's frustrating when there is a piece missing that I can't find.

Thanks and a star to whoever can get this to work.
And I promiss I'll write a FAQ on D&D.

I know it's something stupid; it always is.
 
Hmmm seems to fire OK for me. Let me write up a quick example and see if it works for you...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
OK - Create a new form with a Textbox (TextBox1) and a Label (Label1). Use the following code and drap the contents of the Textbox to the label:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Allow Label1 to accept Drop
        Label1.AllowDrop = True
    End Sub

    Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
        ' Start a drag.
        TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragDrop
        ' Drop the Text
        Label1.Text = e.Data.GetData(DataFormats.Text).ToString
    End Sub

    Private Sub Label1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragEnter
        ' Check for Text being copied
        If e.Data.GetDataPresent(DataFormats.Text) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub TextBox1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles TextBox1.GiveFeedback
        ' Prove the GiveFeedback event fired
        Debug.WriteLine("I have been fired!!!")
    End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
ca8msm, you are the best! [2thumbsup]

Wanna know what it was? [dazed]
I created a new project, and entered your code. It worked fine. Then I started tinkering with my code...

Wanna know?

Your code:
Code:
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)

My code:
Code:
DoDragDrop(TextBox1.Text, DragDropEffects.Copy)

See the difference? [bigglasses]

Mine compiled ok without typing in the ControlName.DoDragDrop(..
I just used DoDragDrop(..

Just checked the help. Nowhere does it say you have to procede the DoDragDrop with the control's name.

I ASSUMED since the first parameter of the DoDragDrop keyword was the control's name, and that it compiled ok, that it was not needed.
In fact, I passed right over it.

I just wish I could glue back all the hair I lost. [bigsmile]
Thank you so much.
 
You also had the line:
Code:
TextBox2.Text = e.Data.GetData(ataFormats.Text).ToString
I sat there confused for a while trying to think what an "ataFormats" could be!

Glsd to help - hopefully the Cursor change will be relatively straightforward now.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Ah, it should have been DataFormats.Text. LOL

Thanks again for all the help. And I am going to write a FAQ on this, with the possible problems.
 
Ok, new problem. This never ends.
The give feedback event only changes the cursor while it's over the drop zone, i.e control.

I wanted to change the cursor as soon as the mouse started moving.

This should not be this hard. [wink]
 
I have a feeling you may have to override the cursor property. e.g.
Code:
    Public Overrides Property Cursor() As Cursor
        Get

        End Get
        Set(ByVal Value As Cursor)

        End Set
    End Property
Haven't tried it myself though but you may want to give it a go and see how you get on.


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I thought I'd try it for myeslef. Anyway looks like you don't need to override the cursor. Replace the GiveFeedback event in my above code with:
Code:
    Private Sub TextBox1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles TextBox1.GiveFeedback
        ' Prove the GiveFeedback event fired
        Debug.WriteLine("I have been fired!!!")
        e.UseDefaultCursors = False
        Label1.Cursor = Cursors.Hand
    End Sub
And you should see the Hand cursor when you drag over Label1. You'll obviously have to change any other cursors (like the actual form) and remeber to reset them when you drop the Text but as far as I can see it is pretty easy.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Yes, this fires and works. But it only changes the cursor while it's over the textbox that it is dropping on.

It used to work where as soon as you "picked up" the "thing" to drag, that the cursor changed into a hand.
And this is what I want.

 
Why would you want to change it into a hand as soon as you pick it up. You can't drop it on itself so IMO it should be set as Cursors.No - anything else is bad design in my eyes as it will confuse the user (it would confuse me anyway).

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Ah. Here is the scenario.
We write software for 9-11. I have a list box full of police and fire units. Each line in the grid may be a unit.
So each line has a unit name and description, also an icon that is a man, truck, police car, etc.

When the user wants to assign a man to a pilice car, they drag the man from one row in the grid to another row, and the grid is updated to sho the man IN the car.

Also there is some database stuff going on in the background that connects the two objects.

So I would want to grab a row and drop it on itself. And without any visual feedback, the dispatcher would have no idea what unit they picked up.

Understand? :)
 
OK - didn't realise it would be used in a grid (thought it would be just be Textbox and Labels).

Let's see what else we can do...

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Right - what about setting it on the mousedown event? Here's the full modified code again:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Allow Label1 to accept Drop
        Label1.AllowDrop = True
        TextBox1.AllowDrop = True
    End Sub

    Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
        ' Start a drag.
        TextBox1.Cursor = Cursors.Hand
        TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
    End Sub

    Private Sub Label1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragDrop
        ' Drop the Text
        Label1.Text = e.Data.GetData(DataFormats.Text).ToString
    End Sub

    Private Sub Label1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragEnter
        ' Check for Text being copied
        If e.Data.GetDataPresent(DataFormats.Text) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub TextBox1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles TextBox1.GiveFeedback
        ' Prove the GiveFeedback event fired
        Debug.WriteLine("I have been fired!!!")
        e.UseDefaultCursors = False
        Label1.Cursor = Cursors.Hand
    End Sub
I also added the following event which may be quite useful whilst you are testing:
Code:
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        TextBox1.Cursor = Cursors.Default
        Label1.Cursor = Cursors.Default
        Me.Cursor = Cursors.Default
    End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I'm playing with it now. The first time you do it, then it works fine.
The next time as soon as you move the mouse over either box, it turns into a hand. I need to shut it down after the drop.
 
Got it:
Code:
Private Sub Label1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragDrop
	' Drop the Text
	Label1.Text = e.Data.GetData(DataFormats.Text).ToString
	Label1.Cursor = Cursors.Default
	TextBox1.Cursor = Cursors.Default
End Sub
 
I said above you would have to "remeber to reset them when you drop the Text". I guess thats the problem.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Oops - guess you posted whilst I was reading and responding to you post!

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Yes, you did say that, didn't you. LOL
I think in my lust for finally having the answer, that I did not read it.
This has been bugging me for 3 weeks now. I'm real close, if not there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top