|
Thats a good answer, thanks! Now all I have to do is learn how to do it.
|
|
|
|
|
I'm writing a solitaire game and trying to drag and drop card images from an array of images to on top of another array of images on a form. I'm stuck on trying to get the images dropped and MsgBoxes to work...I have them for test purposes. Here is some of my code. Note: I tried to use .net, but since I'm such a newbie, I thought I'd stay with VB6 for this. Not a school assignment. A birthday present to someone. sorry for bad format; just testing right now.
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
Dim HeartsIndex As Integer
Dim NewIndex As Integer
Dim Button As Integer
HeartsIndex = 0
Index = 0
For HeartsIndex = 1 To 12 - 1
For Index = 1 To 12 - 1
Hearts(HeartsIndex).Picture = Image1(Index).Picture
If Button = vbLeftButton Then
If TypeOf Source Is Image Then
Hearts(HeartsIndex).Picture = Source.Picture
End If
End If
Next
Next
NewIndex = 0
For NewIndex = 0 To 12 - 1
If Hearts(NewIndex).Picture <> Image1(NewIndex).Picture Then
Image1(NewIndex).Drag vbCancel
MsgBox ("This card does not belong here")
Exit Sub
Else
Image1(Index).Drag vbEndDrag
MsgBox ("You're right!")
Exit Sub
End If
Next
End Sub
|
|
|
|
|
Yep 4 hours later and still no response - get the hint, VB6 is dead and no longer supported, the people who usually answer forum questions are all using .net and have been for years, any help from them will be based on them dragging old info out of the deep past and is of little interest to them!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Some comments on the code.
1 The For NewIndex = 0 To 12 - 1 loop will only be done with NewIndex = 0. It must take either the If or Else branch, both of which exit the sub.
2 Why have 12-1 as end value for loops? Why not just 11?
3 In the For NewIndex = 0 To 12 - 1 Else branch you have Image1(Index).Drag vbEndDrag . Should this be Image1(NewIndex).Drag vbEndDrag ?
4 You don't need to initialise the index variables to 0 since they are set by the For statements.
5 Why is Index a parameter? Whatever value is sent in is ignored since you use it to control the inner loop. So it gets set to 1 and ends up at 12 irrespective of what was supplied. So when the sub returns, whatever variable was supplied will have value 12 (IIRC VB6 defaults to pass ByRef so it changes values in calling code). Is this what you want to do?
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thank you David for your suggestions and expertise. You are really thoughtful, even if I'm using VB6. I tried to use .NET, but it actually takes much more expertise and code than I have, at this time...I'm learning it though....so that is why I'm using VB6 for this.
Anyhow, Index was used as a parameter because that is what I saw in several tutorials on drag/drop operations. I see your point though. I'll try again and see what I come up with. Thanks again!
|
|
|
|
|
|
|
I did some work on this, but what happens at run time is:
The message displayed is "This card does not belong here" irregardless of whatever image1 card I place drag and drop over the hearts card. I can drag the matching card, or another one to the very same slot and that message is displayed. It's like the code never gets past the <> line.
At run time, after I drop the image1 card over the hearts card, then all the cards in the image1(0) slot automatically drop on the same position hearts cards. Any ideas on how I can wrestle with this would be appreciated.
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
Dim HeartsIndex As Integer
Dim NewIndex As Integer
Dim Button As Integer
For HeartsIndex = 0 To 12
For Index = 0 To 12
Hearts(HeartsIndex).Picture = Image1(Index).Picture
If Button = vbLeftButton Then
If TypeOf Source Is Image Then
Hearts(HeartsIndex).Picture = Source.Picture
End If
End If
Next
Next
For NewIndex = 0 To 12
If Source.Picture <> Image1(NewIndex).Picture Then
Image1(NewIndex).Drag vbCancel
MsgBox ("This card does not belong here")
Exit Sub
Else
Image1(NewIndex).Drag vbEndDrag
MsgBox ("You're right!")
Exit Sub
End If
Next
|
|
|
|
|
Not sure what is supposed to be happening here so it's difficult to advise what to do. It depends on what the screen looks like e.g. what is the Hearts control, what is the Source control?
However, one obvious point is that If Button = vbLeftButton Then will never be true. And I don't think you need it. The DragDrop event is triggered when the source object is released over the target (i.e. the Hearts control). You have no way of knowing which mouse button is involved.
Another thought is that you might want to move the Source (assuming it can be dropped on target i.e. is a heart) rather than setting Hearts(x).Picture = Source.Picture.
I just fired up my old VB6 box to check help on drag-and-drop. Not exactly clear is it?
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks David! I tried again but I'm thinking I'm getting more confused though....
I read through a tutorial that the target card, the one that's supposed to be dropped on, is the source card, thus I set the hearts to the source card, just like they coded.
Then image1 array of cards is supposed to equal the hearts array...if the card that is used to drop, image1, once chosen, does not equal the exact heart card to be dropped on, then the message would be "Your card does not belong here." Else, it does belong there.
I hope I'm not being even more confused when writing this, but here is what I've tried. What is happening at run time is like I find the ace card, image1(12) and try to drop it on hearts(12), it says "Your card does not belong here" then all the #12 position card in the location of the 12th card on the image1 card side, all take the spaces of hearts(1 through 12). It's weird. If you send me your email addy, I can send you a screen shot. Pardon the formatting.
Here's what I have....
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
Dim HeartsIndex As Integer
Dim NewIndex As Integer
For HeartsIndex = 0 To 12
For Index = 0 To 12
If TypeOf Source Is Image Then
Hearts(HeartsIndex).Picture = Source.Picture
Hearts(HeartsIndex).Picture = Image1(Index).Picture
End If
Next
Next
For NewIndex = 0 To 12
If Image1(NewIndex).Picture <> Hearts(NewIndex).Picture Then
Image1(NewIndex).Drag vbCancel
MsgBox ("This card does not belong here")
Exit Sub
Else
Image1(NewIndex).Drag vbEndDrag
MsgBox ("You're right!")
Exit Sub
End If
Next
End Sub
|
|
|
|
|
Sounds a bit of a strange tutorial, how can source and target be the same?
As I said I'm not sure what the GUI is like so here's my take on what you might need to do.
Assuming you have four piles (one each for Hearts, Spades, Clubs and Diamonds) each being a control array of images. The aim being to complete each pile by placing cards on it in order.
You also have a deck of cards and possibly one or more heaps of cards (it depends on the version of solitaire you want to play).
Simplifying, the rules are that the top card of the deck is turned face up. It can then be placed on one of the piles or one of the heaps. A card can be moved from a heap to the appropriate pile provided it is same suit and 1 higher in value than top of pile (if the pile is empty it must be the Ace).
In terms of code the source will be the selected card on a heap, or the top of deck; the target will be one of the piles.
So the piles must react to the DragDrop event (as in your Hearts_DragDrop).
The question is what must it do in this event? The answer depends on the rules of the game and your GUI design. However, it should only be dropping one card onto the top of the pile. Your first loops are trying to drop all the image1().Pictures onto all the Hearts().Pictures. Looking at the code it has the same effect as this bit of code (not what I think was intended):
For HeartsIndex = 0 To 12
If TypeOf Source Is Image Then
Hearts(HeartsIndex).Picture = Image1(12).Picture
End If
Next
I.e. the effect is to set all the Hearts().Pictures to image1(12).Picture.
It might be better to continue this off-forum, if so, you can mail me as riced with domain david-rice dot demon dot co dot uk . (obviously in proper email address format!)
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Hi,
I've been working more on this and came up with the following. wondering if State variable makes any difference. Anyhow, when I run at runtime, the card from image1 pile that I drag from the left to the right over the same card, actually copies all of those cards in the entire suit of the hearts cards. Hope this makes sense. Here is the code.
Private Sub Image1_DragOver(Index As Integer, Source As Control, _
X As Single, Y As Single, State As Integer)
Dim HeartsIndex As Integer
For Index = 0 To 12
For HeartsIndex = 0 To 12
If State = 0 Then
Hearts(HeartsIndex).Picture = Image1(Index).Picture
End If
Next
Next
End Sub
Private Sub Hearts_DragOver(Index As Integer, Source As Control, _
X As Single, Y As Single, State As Integer)
Dim HeartsIndex As Integer
If State = 2 Then
If Hearts(HeartsIndex).Picture <> Image1(Index).Picture Then
Image1(Index).Drag vbEndDrag
MsgBox ("You're Right")
Exit Sub
Else
Image1(Index).Drag vbCancel
MsgBox ("You're Wrong")
End If
End If
End Sub
Private Sub Image1_DragDrop(Index As Integer, Source As Control, _
X As Single, Y As Single)
If Source.Name <> Image1(Index).Picture Then Exit Sub
Image1(Index).Picture = Source.Picture
End Sub
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
For Index = 0 To 12
Hearts(Index).Picture = Source.Picture
Next
End Sub
|
|
|
|
|
I don't think you want to be doing this.
When you drag a card from the left (i.e. image1(x)) to right, when the dragged card enters the Hearts() area then Hearts_DragOver is triggered. At this point you should only be doing something like changing the Hearts background to give visual indication to the user; and restoring original background if state = 1. You should not be changing the image to that of the card you have dragged - that should be done in Hearts_DragDrop.
The state variable only tells you if you are entering (0), leaving (1) or moving within (2) the target area. So when state=0 and the user releases the mouse button, a Hearts_DragDrop event is triggered - it's in that event that you would want to change the image.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thank you; so this code wouldn't work then? I thought I had it close, but get a runtime error of 340. So, I'll take heed of your suggestion though; back to the drawing board. Thanks in advance.
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
Dim NewIndex As Integer
'For Index = 0 To 12
'If TypeOf Source Is Image Then
' Hearts(Index).Picture = Source.Picture
'End If
'Next
Index = 0
NewIndex = 0
For Index = 0 To 12
For NewIndex = 0 To 12
Image1(Index).Picture = Hearts(NewIndex).Picture
Index = Index + 1
NewIndex = NewIndex + 1
Next
Next
For Index = 0 To 12
For NewIndex = 0 To 12
If Image1(Index).Picture = Hearts(NewIndex).Picture Then
Image1(Index).Drag vbEndDrag
MsgBox ("You're right!")
Exit Sub
Else
Image1(Index).Drag vbCancel
MsgBox ("This card does not belong here")
Exit Sub
End If
Next
Next
End Sub
|
|
|
|
|
I'm a bit still confused; you think I should use dragover? dragdrop seems to allow me to drag and drop the cards just fine. Problem is I don't know how to write the code for this....what you are stating....I know State is for dragover, but if I don't use that, I use source for dragdrop. Now I don't even know the right settings for the cards on the left and on the right for in design mode, dragmode, oledragdrop, and oledrag...something, can't remember off the top of my head. But now I have all those confused and that's what might be causing my code not to work properly. Can you pleeeeeese help? You're my only hope.
|
|
|
|
|
ymilan wrote: you think I should use dragover?
Not for dropping the image. As I said you should only be changing something like the background or border of the target to give user feedback. If you don't want to give feedback do nothing.
To see why imagine you have a form with four images.
On the left is an image called theShape; on the right are three images called BoxA, BoxB and BoxC.
Imagine that the user selects theShape image and drags it to the right of the form (i.e.they are holding the mouse button down).
Suppose they drag the shape over the BoxA image. This will trigger the BoxA_DragOver event with state = 0. What should this event do? One option is nothing, so you don't write any code for the event. Another option is to change the border or color of the BoxA image so the user knows that theShape is over BoxA. What you should not do is set BoxA image to theShape image. The user has not released the mouse button so they don't want theShape to be in BoxA.
If the user does release the mouse button then the BoxA_DragDrop event is triggered. That is when you might want to set BoxA image to theShape so that the code to do so should be in the BoxA_DragDrop event.
Suppose the user drags theShape over the BoxA image (without releasing the mouse button) and then drags it over the BoxB image. Two things happen. First the BoxA_DragOver event is triggered with state = 1; then the BoxB_DragOver event is triggered with state = 0. In this case the BoxA_DragDrop could set the border or color back to their original values and the BoxB_DragDrop could change the border or color of BoxB image. Again, BoxB_DragOver should not set the BoxB image to theShape image. The user might continue the drag so it goes over BoxC.
If you imagine that the images for the boxes are much larger than theShape image, then as the user drags theShape around in a box image the DragOver event is triggered continuously with state = 2. In most cases that does not matter so the code does nothing.
This all means that the typical code for a DragOver event will be something like:
If State = 0 Then
'Do something like change border or color of the box
'You simply want to make user aware of where theShape has been dragged too
Else
If State = 1 Then
'Set the border/color of the box back to original value
Else 'State = 2
'Do nothing - they are moving around within the box
End If
End If
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Awesome! I understand dragover now! I will write that and see how I do. However, please assist on dragdrop; what do I put in that code? I tried hearts(index).Picture = Source, like every tutorial I read said to do. Then do I do vbenddrag and vbcancel like I did before, or is this in error?
|
|
|
|
|
Ok, this is the code I have so far, but ALL the cards from every suit are allowed to drop on the target. In design view, I need some assistance on whether to put Manual, Autmatic, or None or DragDrop, OLEDragOver, OLEDragDrop.....I think that may be messing this whole thing up. Anyhow, my code is below; any errors?
Private Sub Hearts_DragOver(Index As Integer, Source As Control, _
X As Single, Y As Single, State As Integer)
For Index = 0 To 12
If State = 0 Then
CommonDialog1.Flags = &H80FFFF
CommonDialog1.ShowColor
Hearts(Index).BackColor = CommonDialog1.Color
Else
If State = 1 Then
CommonDialog1.Flags = &HFFFFFF
CommonDialog1.ShowColor
Hearts(Index).BackColor = CommonDialog1.Color
Else
If State = 2 Then
End If
End If
End If
Next Index
End Sub
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
For Index = 0 To 12
Hearts(Index).Picture = Source.Picture
Source.Picture = Image1(Index).Picture
Next Index
For Index = 0 To 12
If Image1(Index).Picture = Hearts(Index).Picture Then
Image1(Index).DragMode Manual
Image1(Index).Drag vbEndDrag
MsgBox ("You're Right")
Else
Image1(Index).Drag vbCancel
MsgBox ("You're Wrong")
End If
Next Index
|
|
|
|
|
Firstly, I don't think you should be messing with OLEDrag* at all, so would set OLEDragMode property to 0. OLE drag features are for dragging and dropping data rather than images. E.g. drag data between DataGrid controls.
Secondly, I don't think you should have loops in the Hearts_DragOver or Hearts_DragDrop events. You probably should not be changing the value of Index (as the loops are doing) and I doubt that you should be changing the Source.Picture.
Imagine the following. There is a form with two control arrays called theShapes and theBoxes. Both have say 5 items (so indexes go from 0 to 4) with theShapes on left of form; theBoxes on right.
The user clicks and drags e.g. theShapes(3) image over to the right.
Suppose it is dragged over theBoxes(1) image. Then the theBoxes_DragOver event is triggered with Index = 1 and State = 0. So in the code the only image that should change color is theBoxes(1). All the other theBoxes() images should remain as they were. So no loop required. Also, the Source is theShapes(3) and presumably you don't want its picture to change. So you never want to be doing something like Source.Picture = SomeOtherControl.Picture .
Suppose that the object is to place the correct shapes in the correct boxes. So at the start theBoxes() all have some blank image; theShapes() all have some appropriate image. E.g. theShapes(3) could be a triangle, theShapes(4) could be a circle, etc. If the user drags a shape and drops it into a box, the box should only accept it if it is the correct shape. So e.g. if theShape(3) (i.e. a triangle) is dragged and dropped into theBoxes(1), the drop should only succeed if theBoxes(1) accepts triangles. So how do we tell if this is the case? We cannot compare pictures because theBoxes() are all a blank image so will never be the same as a shape image.
One way of doing this is to set the Tag property for each of the controls and compare the Tags before allowing the drop to succeed or fail.
E.g. when setting up the control arrays you would have something like
theShapes(3).Tag = "Triangle"
theBoxes(0).Tag = "Square"
theBoxes(1).Tag = "Triangle"
theBoxes(2).Tag = "Circle"
In the theBoxes_DragDrop event you would then have something like
If theBoxes(Index).Tag = Source.Tag Then
'Allow the drop
Else
'Cancel the drop
End If
I think this is how to do it. But to do so requires that the DragMode for theShapes() to be set to 0 (i.e. vbManual) and that the Drag method is implemented. I might be wrong about this, it may be possible to cancel the drop when mode is set to vbAutomatic but I can't think how it would work.
The DragMode should be set when the controls are created, I can't think of a good reason to set the mode in the DragDrop event of another control.
Finally, the mode for theBoxes controls should be set to vbManual and no Drag method implemented. This stops the user from dragging theBoxes controls around.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Wow! Thanks for the expert explanation. Does this mean I would put:
Image1(0).Tag = Hearts(0).Tag
Somewhere in the first part of the code? Where so? This is all my code so far; problem is which ever card is in the first slot (as in Image1(0)), but can be any card, when dropped over Hearts(0), it says, you're right and allows drop. If the real Image1(0).Picture, King Hearts is dropped over Hearts(0), it says you're wrong.
I changed all my settings in design mode to put images on the left as Automatic and none for both OLE modes. I set images on the right to be Manual for drag and none for both Ole modes. Any problems on this? Here is all of my code so far: I appeciate your help....
Option Explicit
Public Sub Main()
Start_Game.Show
End Sub
Private Sub Exit_Click()
MsgBox ("Thank you for playing. Good Bye.")
End
End Sub
Private Sub Start_Click()
Dim arrDeck(52)
Dim Cardcount As Integer
Dim Counter As Integer
Do While Cardcount <= 51
arrDeck(Cardcount) = Cardcount
Cardcount = Cardcount + 1
Loop
Dim swap As Integer
Dim posn1 As Integer
Dim posn2 As Integer
Randomize
For swap = 0 To 100
posn1 = Int(52 * Rnd)
posn2 = Int(52 * Rnd)
If (posn1 <> posn2) Then
KingH(0).Picture = Image1(posn1).Picture
Image1(posn1).Picture = Image1(posn2).Picture
Image1(posn2).Picture = KingH(0).Picture
End If
Next swap
Counter = 0
For Counter = 0 To 51
Image1(Counter).Visible = True
Next Counter
End Sub
Private Sub Hearts_DragOver(Index As Integer, Source As Control, _
X As Single, Y As Single, State As Integer)
If Index <= 12 Then
If State = 0 Then
Hearts(Index).Picture = LoadPicture("C:\Program1\redbacking.gif")
Else
If State = 1 Then
Hearts(Index).Picture = Hearts(Index).Picture
Else
If State = 2 Then
End If
End If
End If
End If
End Sub
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
If Hearts(Index).Tag = Source.Tag Then
Image1(Index).Drag vbEndDrag
MsgBox ("You're Right")
Else
Image1(Index).Drag vbCancel
MsgBox ("You're Wrong")
Exit Sub
End If
End Sub
|
|
|
|
|
ymilan wrote: put images on the left as Automatic and
Then you cannot stop the drop - you can only do that if the mode is Manual and you have a Drag method which allows you to Cancel the drop.
ymilan wrote: If State = 0 Then
Hearts(Index).Picture = LoadPicture("C:\Program1\redbacking.gif")
Else
If State = 1 Then
Hearts(Index).Picture = Hearts(Index).Picture
Else
This won't work - when you DragDrop is triggered with State = 0 (entering) the Hearts(Index).Picture is changed to the gif. When it is triggered with state = 1 (leaving) it set back to itself i.e. to the gif it was set to when entering.
ymilan wrote: Does this mean I would put:
Image1(0).Tag = Hearts(0).Tag
Somewhere in the first part of the code?
Have you set Hearts(x).Tags in Designer for all controls? If so and you want the Image(x).Tags to be the same as the Hearts(x) tags, you need to do so in the Form_Load event. But I'm not sure that's what you want.
Is this the complete code? There seems to be something missing e.g. this is not right.
ymilan wrote: Private Sub Exit_Click()
MsgBox ("Thank you for playing. Good Bye.")
End
End Sub
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Can I have your email address again to take this offline? I have some gifs to show you.
Thanks.
YMilan
|
|
|
|
|
When I put the images on the left in design view as automatic, when they are in a random place, i.e. image1(0), King of Hearts, they do not move at all. When I put them in manual, they move.
For some reason; and you said; but I don't understand the reason; when in State 0, I move the images, i.e. King of Hearts to the top of the hearts(0) image (a white backing gif, not King of Hearts. I did this so I can show a white backed card, so the player can see a white card instead of a black space. I can place the redbacking card no problem when the card enters the white backed card on the right. It works. However, if it is right, it doesn't drop the Image1(Index).Picture at all. It leaves it a redbacking gif.
Now, when my images on the left are randomized, and I want to pic out the King of Hearts on the left, somewhere in the shuffle, it says you're wrong when I place it over the hearts(0) position on the right. It shows a red backing but, does not say You're right or drop the card. I have that code in my hearts dragdrop; you'll see above in my last code reply. However, when I place a random card from the Image1(0) POSITION on the left, it works and says you'r right, but that is the wrong card....???? Don't understand. It leaves a redbacked gif as well, when it is the wrong card, but say's your're right.
You're helping me and I'm glad; Thank you.
|
|
|
|
|
Oh in addition, what I need to know is what setting, manual, automatic, or none do I set in design view for the one dragmode, and two oledrag properties?
|
|
|
|
|
This is what I've tried as well....still in design mode I have each image1 card set to Manual for dragmode, Automatic for oledragmode and automatic for oledropmode. Not sure what to set the hearts cards to though...tried many different modes and all seems confusing...here is my code so far: Wondering if I should have the question of whether image1(1 through 12) = Hearts(1 through 12) in the dragdrop not dragover....I tried that too, but no avail.
Public Sub Image1_Click(Index As Integer)
For Index = 0 To 12
Hearts(Index).Picture = Image1(Index).Picture
Next Index
For Index = 13 To 25
Clubs(Index).Picture = Image1(Index).Picture
Next Index
For Index = 26 To 38
Diamonds(Index).Picture = Image1(Index).Picture
Next Index
For Index = 39 To 51
Spades(Index).Picture = Image1(Index).Picture
Next Index
End Sub
Private Sub Hearts_DragOver(Index As Integer, Source As Control, _
X As Single, Y As Single, State As Integer)
For Index = 0 To 12
If State = vbEnter Then
If Source.Picture = Image1(Index).Picture Then
If Image1(Index).Picture = Hearts(Index).Picture Then
Image1(Index).Drag vbEndDrag
MsgBox ("You're Right")
Exit Sub
Else
Image1(Index).Drag vbCancel
MsgBox ("You're Wrong")
Exit Sub
End If
End If
End If
Next Index
End Sub
Private Sub Hearts_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
For Index = 0 To 12
Hearts(Index).Picture = Source.Picture
Next Index
End Sub
|
|
|
|
|