473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 2148
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: kiran | last post by:
I am trying to implement a rubber band/image selection script. For that I need to remove the default drag behaviour on an image. I am able to do this in IE but not Netscape. Does any one have a...
1
by: kiran | last post by:
I am trying to implement a rubber band/image selection script. For that I need to remove the default drag behaviour on an image. I am able to do this in IE but not Netscape. Does any one have a...
3
by: NewSun | last post by:
How can i drag a datarow from a datagrid to a other control? e.g. Drag a datarow from a datagrid to a TextBox,Then display one colnumn' value of the datarow . Or Drag a cell's value to a controll...
1
by: Jon Cosby | last post by:
I need an event handler for dragging the cursor on a PictureBox. The existing events only include handlers for dragging and dropping objects over the controls. I'm trying to use the MouseDown and...
0
by: Samuel R. Neff | last post by:
I have a typical TreeView/ListView combo and can drag from the ListView to the TreeView. I'd like to select the TreeView node that is the target of the drag operation as the ListView items are...
0
by: david | last post by:
Yesterday I got a suggestion about the web page layout from Stas. Thanks, Stas. However, I have a problem to drag the existing radio buttons into the table cell. Case: I have a lot of radion...
2
by: vunet.us | last post by:
Hello JavaScript experts, I have a floating div which I drag all over the page. If the page has scrollbars and users drag the floating div to the very top, page scrolls up too. The problem occurs...
0
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
Being you can drag webparts from zone to zone, is there a way to control drag speed while dragging/scrolling up the page? I have a list of webparts that may go beyond the page. If a user wants...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.