471,082 Members | 748 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,082 software developers and data experts.

Drag-Drop

I'm working for the first time with the DoDragDrop method. I've got almost
everything worked out, but I need some help with the last bit.

There are two listboxes on my form, lstGroups and lstStudents. I want to be
able to drag a name from lstStudents and drop it on one of the names in
lstGroups to move it to that group. I've got the dragging part working;
it's just the dropping that isn't there yet. I'm working with what I can
find from the msdn library and another sample project I found. I'm trying
to convert the code from the samples to the code I need for my project.
This is the lstGroups.DragDrop sub I have so far:

Private Sub lstGroups_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lstGroups.DragDrop
Dim Pt As Point = lstGroups.PointToClient(New Point(e.X, e.Y))
Dim DestinationFolder As String = lstGroups.GetItemText(Pt)
End Sub

I want DestinationFolder to be assigned the text of the group name (in
lstGroups) where the student's name is dropped. The code above is giving me
the x/y coordinates of Pt. Where do I go from here?

Thanks a lot,
Nathan
Nov 20 '05 #1
14 1971
You know, if it weren't for you MVPs (and the other folks on this NG as
well) I would have uninstalled VS and given up on this a long time ago.
Thanks for the help.

There is one other problem I have now. The lstStudents.MouseDown event that
starts the drag-drop seem to have "overridden" the
lstStudents.SelectedIndexChanged and lstStudents.DoubleClick events. These
events don't work unless I comment out the drag-drop subs. Any idea what's
wrong?

Nov 20 '05 #2
In article <e7**************@TK2MSFTNGP10.phx.gbl>,
nk*********************@softhome.net says...
You know, if it weren't for you MVPs (and the other folks on this NG as
well) I would have uninstalled VS and given up on this a long time ago.
Thanks for the help.
Thanks for noticing! :)
There is one other problem I have now. The lstStudents.MouseDown event that
starts the drag-drop seem to have "overridden" the
lstStudents.SelectedIndexChanged and lstStudents.DoubleClick events. These
events don't work unless I comment out the drag-drop subs. Any idea what's
wrong?


I looked around Google and found this post that uses
SystemInformation.DragSize to determine whether a mousedown/mousemove
should start a drag/drop or just be handled as a mousedown and a
mousemove. It might help you out.

http://tinyurl.com/3eyuw

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #3
Thanks for the link. I don't have any experience with other languages, but
I did the best I could at interpreting the code there to fit what I have.
As I have it (code is posted below), I can select a student's name, but
nothing with the drag-drop works. Maybe you can figure out what's wrong?

\\
Dim DragRectangle as Rectangle = Rectangle.Empty
Dim Index as Integer

Private Sub lstStudents_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstStudents.MouseDown
lstStudents.SelectedIndex = lstStudents.IndexFromPoint(e.X, e.Y)

If lstStudents.SelectedIndex = -1 Then ' ??? Is this the correct
interpretation from the code in the link? If so, I don't understand why
Dim RecSize As Size = SystemInformation.DragSize
DragRectangle = New Rectangle(New Point(e.X - CInt(RecSize.Width /
2), e.Y - CInt(RecSize.Height / 2)), RecSize)
Else
DragRectangle = Rectangle.Empty
End If

End Sub

Private Sub lstStudents_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstStudents.MouseUp
DragRectangle = Rectangle.Empty
End Sub

Private Sub lstStudents_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstStudents.MouseMove
If DragRectangle.IsEmpty And DragRectangle.Contains(e.X, e.Y) Then
lstStudents.DoDragDrop(lstStudents.Items(Index),
DragDropEffects.Move)
End If
End Sub

Private Sub lstStudents_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles lstStudents.SelectedIndexChanged
EnableButtons() 'This enables or disenables certain buttons
according to whether a student's name is selected
End Sub

\\
Nov 20 '05 #4
In article <e7**************@TK2MSFTNGP11.phx.gbl>,
nk*********************@softhome.net says...
If lstStudents.SelectedIndex = -1 Then ' ??? Is this the correct
interpretation from the code in the link? If so, I don't understand why


No. In C#, the "!=" means not equal to. So change your VB.NET code to:

If lstStudents.SelectedIndex <> -1 Then

See if that works.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #5
That clears up the problem. I had already tried the using .SelectedIndex
<>-1, but after reading your post I went back through the C code to see if I
had made that error anywhere else. Sure enough, I did in
lstStudents.MouseMove. The drag/drop

Nov 20 '05 #6
Oops... that last one got sent by mistake. I got the drag-drop to work, but
it still messes up the lstStudents.DoubleClick event. I want to be able to
double-click a student's name to select that student and close the dialog
box, equivalent to clicking the form's Select button. If I double click an
item in the list, then click the item again, the student is selected and the
form closes. If I double-click one student, then click another, that
student is selected and the form closes. Here is my current code for
lstStudents:

Private Sub lstStudents_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstStudents.MouseDown
'lstStudents.SelectedIndex = lstStudents.IndexFromPoint(e.X, e.Y)
If lstStudents.SelectedIndex <> -1 Then
Dim RecSize As Size = SystemInformation.DragSize
DragRectangle = New Rectangle(New Point(e.X - CInt(RecSize.Width /
2), e.Y - CInt(RecSize.Height / 2)), RecSize)
Else
DragRectangle = Rectangle.Empty
End If
End Sub

Private Sub lstStudents_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstStudents.MouseUp
DragRectangle = Rectangle.Empty
End Sub

Private Sub lstStudents_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstStudents.MouseMove
If Not DragRectangle.IsEmpty And DragRectangle.Contains(e.X, e.Y) Then
lstStudents.DoDragDrop(lstStudents.Items(Index),
DragDropEffects.Move)
End If
End Sub
Nov 20 '05 #7
In article <OH**************@tk2msftngp13.phx.gbl>,
nk*********************@softhome.net says...
Oops... that last one got sent by mistake. I got the drag-drop to work, but
it still messes up the lstStudents.DoubleClick event. I want to be able to
double-click a student's name to select that student and close the dialog
box, equivalent to clicking the form's Select button. If I double click an
item in the list, then click the item again, the student is selected and the
form closes. If I double-click one student, then click another, that
student is selected and the form closes.


Nathan, sorry for the delay in responding. Haven't been able to visit
here recently. Did you get your issue resolved?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #8
No, I haven't been able to solve this one yet. Any ideas?
"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <OH**************@tk2msftngp13.phx.gbl>,
nk*********************@softhome.net says...
Oops... that last one got sent by mistake. I got the drag-drop to work, but it still messes up the lstStudents.DoubleClick event. I want to be able to double-click a student's name to select that student and close the dialog box, equivalent to clicking the form's Select button. If I double click an item in the list, then click the item again, the student is selected and the form closes. If I double-click one student, then click another, that
student is selected and the form closes.


Nathan, sorry for the delay in responding. Haven't been able to visit
here recently. Did you get your issue resolved?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 20 '05 #9
In article <uR**************@TK2MSFTNGP10.phx.gbl>,
nk*********************@softhome.net says...
No, I haven't been able to solve this one yet. Any ideas?


Let me make sure I understand the situation:

You have a dialog that pops up for selecting a student from a ListBox.
You want the user to be able to click on a student and then click on a
"Select" button which will close the dialog (and I assume keep track of
the student selected). Alternatively, you want the user to be able to
simply double-click on a student and get the same effect (close the
dialog and keep track of the student selected).

So the problem you have now is that when you double-click, nothing
happens. But you then single-click on another student and the dialog
closes with that student selected?

Can you post the code in your DoubleClick event?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #10
Patrick,

Thank you for your willingness to help me with this. Yes, clicking Select
selects the loads the selected student's info and closes the form. Here is
the code you asked for; don't know if you'll see anything in it. FYI,
btnSelect's DialogResult is OK.

\\\
Private Sub lstStudents_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lstStudents.DoubleClick
btnSelect.PerformClick()
End Sub

Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSelect.Click
'Set the current file to something like "C:\Program
Files\Math\Students\Group1\Bob.mpd"
CurrentFile = InstallDir & "Students\" & lstGroups.SelectedItem.ToString &
"\" & lstStudents.SelectedItem.ToString & ".mpd"
LoadStudentFile()
End Sub

Private Sub LoadStudentFile()
'This loads the selected student's file into memory for game play
'The game accesses simple text files for storing data

Student = lstStudents.SelectedItem.ToString
Dim StudentReader As New StreamReader(CurrentFile)

If StudentReader.Peek <> -1 Then
. . .
'Code for gathering information from the file - works fine when
btnSelect is clicked, but not with double-click event
. . .
End If

StudentReader.Close()

End Sub
\\\

While the drag-drop operation is imperative, the double-click comes in
second. I could live without it, but to me a program has a lot more
functionality when a lstbox item can be double-clicked in place of clicking
a separate button. As I mentioned before, if I get rid of the drag-drop
subs, the double-clicking works fine.
Nov 20 '05 #11
In article <O4**************@tk2msftngp13.phx.gbl>,
nk*********************@softhome.net says...
Thank you for your willingness to help me with this. Yes, clicking Select
selects the loads the selected student's info and closes the form.


My guess is that the mouse-up/mouse-down code is interfering with the
double-click operation. Not sure if I can see a clean solution for
that. It might be interesting to check the DragRectangle variable
inside the dbl-click and see if it's Empty or not. May give you some
clues as to what is messing it up.

The only other thing I could think of would be to ignore a mouse-down if
it's going to lead to a double-click. What you'd have to do is look at
the SystemInformation.DoubleClickTime property to see how many
milliseconds between mouse clicks will generate a double-click (along
with SystemInformation.DoubleClickSize to limit the amount of movement
to be considered a double-click). Quite a bit of work...

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #12
Thanks, Patrick. I'll have a look at those things.

"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <O4**************@tk2msftngp13.phx.gbl>,
nk*********************@softhome.net says...
Thank you for your willingness to help me with this. Yes, clicking Select selects the loads the selected student's info and closes the form.


My guess is that the mouse-up/mouse-down code is interfering with the
double-click operation. Not sure if I can see a clean solution for
that. It might be interesting to check the DragRectangle variable
inside the dbl-click and see if it's Empty or not. May give you some
clues as to what is messing it up.

The only other thing I could think of would be to ignore a mouse-down if
it's going to lead to a double-click. What you'd have to do is look at
the SystemInformation.DoubleClickTime property to see how many
milliseconds between mouse clicks will generate a double-click (along
with SystemInformation.DoubleClickSize to limit the amount of movement
to be considered a double-click). Quite a bit of work...

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele

Nov 20 '05 #13
While searching for mouse event info for another project, I discovered the
..Click event in the MouseEventArgs. So I thought I'd try it. Sure enough,
this allows me to do the drag-drop I need as well as double-click a name to
select it:

Private Sub lstStudents_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lstStudents.MouseDown
If e.Clicks = 2 Then
btnSelect.PerformClick()
Else
'Drag-drop code
End If
End Sub

"Patrick Steele [MVP]" <pa*****@mvps.org> wrote in message
news:MP************************@msnews.microsoft.c om...
In article <O4**************@tk2msftngp13.phx.gbl>,
nk*********************@softhome.net says...
Thank you for your willingness to help me with this. Yes, clicking Select selects the loads the selected student's info and closes the form.


My guess is that the mouse-up/mouse-down code is interfering with the
double-click operation. Not sure if I can see a clean solution for
that. It might be interesting to check the DragRectangle variable
inside the dbl-click and see if it's Empty or not. May give you some
clues as to what is messing it up.

The only other thing I could think of would be to ignore a mouse-down if
it's going to lead to a double-click. What you'd have to do is look at
the SystemInformation.DoubleClickTime property to see how many
milliseconds between mouse clicks will generate a double-click (along
with SystemInformation.DoubleClickSize to limit the amount of movement
to be considered a double-click). Quite a bit of work...

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele


Nov 20 '05 #14
In article <el**************@TK2MSFTNGP09.phx.gbl>,
nk*********************@softhome.net says...
While searching for mouse event info for another project, I discovered the
.Click event in the MouseEventArgs. So I thought I'd try it. Sure enough,
this allows me to do the drag-drop I need as well as double-click a name to
select it


Cool!!

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jon Cosby | last post: by
2 posts views Thread by vunet.us | last post: by
reply views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.