Help Dragging file from one tabpage into another

Drag/drop problem when going from one tabpage to another

Page 1 of 1

9 Replies - 3319 Views - Last Post: 27 October 2009 - 03:23 PM Rate Topic: -----

#1 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Help Dragging file from one tabpage into another

Post icon  Posted 21 August 2009 - 06:28 PM

I cannot get the dragdrop event to fire when I drag a file from one tabpage to another tab page in the same tabcontrol.

I can drag a file from my application and drop into windows explorer and and then drag from windows explorer into my app. That all works fine. And when I drag from one tabpage up to the top of the control, my program uses the tabcontrols dragover event to swap tabs....works fine also. ...Drag drop event fires...no problems

Say I have a list box with files on one tabpage, a list box with files from a different directory on another tabpage, I want to be able to drag the file over and drop it into the other listbox.

I have done lots of testing on the variables in the dragdrop handlers to see if they are recognizing the switch in controls and they are. But the dragdrop event will not fire.

Does anyone have any insight on this kind of problem. I can upload some code if you need but it is lengthy, I could trim it up a bit. Thanks in advance :)

Is This A Good Question/Topic? 0
  • +

Replies To: Help Dragging file from one tabpage into another

#2 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Help Dragging file from one tabpage into another

Posted 21 August 2009 - 08:45 PM

I finally fixed it, after 2 days....I did not have the drag drop effects matching in my code...that was it :rolleyes: :crazy:
Was This Post Helpful? 0
  • +
  • -

#3 C66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-September 09

Re: Help Dragging file from one tabpage into another

Posted 09 September 2009 - 03:58 PM

Hi dzone41,

View Postdzone41, on 21 Aug, 2009 - 05:28 PM, said:

Say I have a list box with files on one tabpage, a list box with files from a different directory on another tabpage, I want to be able to drag the file over and drop it into the other listbox.

That is exactly what I would like to do. But somehow I can't get the change to the new tabpage when the file is dragged over it to work. Would you mind posting your code showing how that is done. That would be very kind. Thank you very much.

C66
Was This Post Helpful? 0
  • +
  • -

#4 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Help Dragging file from one tabpage into another

Posted 09 September 2009 - 05:22 PM

Give me a couple hours, can't get to it right now, but it's no problem
Was This Post Helpful? 0
  • +
  • -

#5 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Help Dragging file from one tabpage into another

Posted 09 September 2009 - 06:49 PM

	Private Function TabControlsMouseHover(ByVal pt As Point)

		Dim TabC As TabControl = TabControl2
		Dim intC As Integer = TabC.TabCount - 1
		Dim intT As Integer
		Dim TR As Rectangle
		For intT = 0 To intC
			TR = TabC.GetTabRect(intT)
			If TabC.GetTabRect(intT).Contains(pt) Then
				Return intT
			End If
		Next

	End Function



	Private Sub Cont_DragOver(ByVal sender As Object, ByVal e As _
	System.Windows.Forms.DragEventArgs) Handles TabControl2.DragEnter
		
	Dim pt = New Point(e.X, e.Y)
		pt = PointToClient(pt)
		pt.Y = 18 ' this locks in the Y value ...you are only
'											   concered with the X position
		Dim TC As TabControl = TabControl2
		Dim TP As Integer
		TP = TabControlsMouseHover(pt)
		TabControl2.SelectedIndex = TP
		TabControl2.Focus()

	End Sub


The Function does not return a value on all code paths, but I have not encountered any problems with this yet.
Also if you drag into the tabs section of the TabControl but drag into the wrong tab, you cannot just drag over to the next tab, you have to create another dragenter event by moving out of the tabs and then back in.
Other than that seems to work fine. Have had no exceptions at all

This post has been edited by dzone41: 09 September 2009 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 C66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-September 09

Re: Help Dragging file from one tabpage into another

Posted 10 September 2009 - 11:26 AM

Thanks a lot, dzone 41, for your reply. My mistake was that I did not set the TabControl.SelectedIndex property.

My code now looks like this
Public Sub TabControl_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles TabControl_Folders.DragOver
		Dim tc As TabControl = sender
		Dim pt As Point = tc.PointToClient(New Point(e.X, e.Y))
		Dim IndexOld As Integer = tc.SelectedIndex
		Dim IndexNew As Integer = GetTabPageIndex(pt, tc)
		If IndexNew <> IndexOld Then
			tc.SelectedIndex = IndexNew
			tc.Focus()
			Dim tr As Rectangle = tc.GetTabRect(IndexNew)
			Windows.Forms.Cursor.Position = tc.PointToScreen(New Point(tr.X + (tr.Width / 2), tr.Y + (tr.Height / 2)))
		End If
	End Sub

The extra two lines after tc.Focus() serve to put the mouse back into the middle of the selected Tab. This is essential for multiline TabControls where the change in the selected tab page affects the order of tabs as they appear on the screen. Without these lines dragging onto one of the tabs in the upper tab rows results in endless flicker.

Also, the above handler does allow dragging form tab to tab as it handles the DragOver event rather than the DragEnter event.

The GetTabPageIndex function looks like this.
	Public Function GetTabPageIndex(ByVal pt As Point, ByVal tc As TabControl) As Integer
		For index As Integer = 0 To tc.TabCount - 1
			If tc.GetTabRect(index).Contains(pt.X, pt.Y) Then Return index
		Next
	End Function

This does return a result on all paths. ;)
Was This Post Helpful? 0
  • +
  • -

#7 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Help Dragging file from one tabpage into another

Posted 10 September 2009 - 01:35 PM

That worked much better than mine....Thanks
Was This Post Helpful? 0
  • +
  • -

#8 gjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-September 09

Re: Help Dragging file from one tabpage into another

Posted 15 September 2009 - 11:18 AM

View Postdzone41, on 21 Aug, 2009 - 07:45 PM, said:

I finally fixed it, after 2 days....I did not have the drag drop effects matching in my code...that was it :rolleyes: :crazy:


drgevent.Effect = DragDropEffects.All;

Thx for the direction bud!!!!
Was This Post Helpful? 0
  • +
  • -

#9 C66  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-September 09

Re: Help Dragging file from one tabpage into another

Posted 27 October 2009 - 01:18 PM

View PostC66, on 10 Sep, 2009 - 10:26 AM, said:

... The GetTabPageIndex function looks like this.
	Public Function GetTabPageIndex(ByVal pt As Point, ByVal tc As TabControl) As Integer
		For index As Integer = 0 To tc.TabCount - 1
			If tc.GetTabRect(index).Contains(pt.X, pt.Y) Then Return index
		Next
	End Function


This actually contains an error as the result of GetTabPageIndex is undefined if the mouse cursor is outside all TabRects. A better version (with checking for droppable data, but that is a separate aspect) looks like this:

	Private Sub tcFolders_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles tcFolders.DragOver
		If e.Data.GetDataPresent(DataFormats.FileDrop) And (tcFolders.TabCount > 0) Then
			e.Effect = If(e.KeyState = 9, DragDropEffects.Copy, DragDropEffects.Move)
		Else
			e.Effect = DragDropEffects.None
		End If

		Dim tc As TabControl = sender
		Dim pt As Point = tc.PointToClient(New Point(e.X, e.Y))
		Dim IndexOld As Integer = tc.SelectedIndex
		Dim IndexNew As Integer = tc.SelectedIndex

		For index As Integer = 0 To tcFolders.TabCount - 1
			If tcFolders.GetTabRect(index).Contains(pt) Then
				IndexNew = index
				Exit For
			End If
		Next

		If IndexNew <> IndexOld Then
			tc.SelectedIndex = IndexNew
			tc.Focus()
			Dim tr As Rectangle = tc.GetTabRect(IndexNew)
			Windows.Forms.Cursor.Position = tc.PointToScreen(New Point(tr.X + (tr.Width / 2), tr.Y + (tr.Height / 2)))
		End If
	End Sub

Was This Post Helpful? 0
  • +
  • -

#10 dzone41  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 216
  • Joined: 21-August 09

Re: Help Dragging file from one tabpage into another

Posted 27 October 2009 - 03:23 PM

Yea your right, the only real difference of course besides not using a function is that you assign a value to Index imediately. I kept the function, and added Return Index after the For..Next loop, so no matter what I have a number.
Same solution, different methods. High five \0/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1