RobertoFreemano
asked on
drag'n'drop (interactive form) to score VB.net 2003, FAO - Idle Mind
HI Idle_Mind,
I wonder if you would be so kind as to piece this together for me? I feel like a right dumb dumb :(
Thanks,
Roberto
I wonder if you would be so kind as to piece this together for me? I feel like a right dumb dumb :(
Thanks,
Roberto
' Your original 2008 solution
-------------------------------
Public Class Form1
Private R As New Random
Private Score As Integer = 0
Private Counter As Integer = 0
Private PBs As New List(Of PictureBox)
Private LBLs As New List(Of Label)
Private data(,) As String = {{"C:\Users\Mike\Pictures\Images\AmericanFlag.jpg", "Flag"}, _
{"C:\Users\Mike\Pictures\Images\Evil Rubber Ducky.jpg", "Duck"}, _
{"C:\Users\Mike\Pictures\Images\Geek.jpg", "Geek"}, _
{"C:\Users\Mike\Pictures\Images\Hunter2.jpg", "Hunter"}, _
{"C:\Users\Mike\Pictures\Images\Left4Dead Kids.jpg", "Kids"}, _
{"C:\Users\Mike\Pictures\Images\Lynched Thumb.jpg", "Thumb"}}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lbl As Label
Dim pb As PictureBox
Dim matches() As Control
For i As Integer = 1 To 6
matches = Me.Controls.Find("PictureBox" & i, True)
If matches.Length > 0 Then
pb = matches(0)
pb.SizeMode = PictureBoxSizeMode.Zoom
pb.Image = Image.FromFile(data(i - 1, 0))
pb.Tag = data(i - 1, 1)
pb.AllowDrop = True
PBs.Add(pb)
AddHandler pb.DragEnter, AddressOf pb_DragEnter
AddHandler pb.DragDrop, AddressOf pb_DragDrop
End If
matches = Me.Controls.Find("Label" & i, True)
If matches.Length > 0 Then
lbl = matches(0)
lbl.Text = data(i - 1, 1)
LBLs.Add(lbl)
AddHandler lbl.MouseMove, AddressOf lbl_MouseMove
End If
Next
btnReset.PerformClick()
End Sub
Private Sub lbl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim lbl As Label = CType(sender, Label)
lbl.DoDragDrop(lbl, DragDropEffects.Move)
End If
End Sub
Private Sub pb_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim pb As PictureBox = CType(sender, PictureBox)
e.Effect = IIf(e.Data.GetDataPresent(GetType(Label)), DragDropEffects.All, DragDropEffects.None)
End Sub
Private Sub pb_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
Dim pb As PictureBox = CType(sender, PictureBox)
pb.Visible = False
Dim lbl As Label = e.Data.GetData(GetType(Label))
lbl.Visible = False
Score = Score + IIf(lbl.Text = pb.Tag, 1, 0)
Counter = Counter + 1
If Counter = 6 Then
btnScore.Enabled = True
End If
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
Score = 0
Counter = 0
For Each pb As PictureBox In PBs
pb.Visible = True
Next
For Each lbl As Label In LBLs
lbl.Visible = True
Next
btnScore.Enabled = False
Shuffle()
End Sub
Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
MessageBox.Show("You got " & Score & " correct!")
btnReset.PerformClick()
End Sub
Private Sub Shuffle()
Dim index As Integer
Dim tmpLocation As Point
For Each pb As PictureBox In PBs
index = R.Next(0, PBs.Count)
tmpLocation = pb.Location
pb.Location = PBs(index).Location
PBs(index).Location = tmpLocation
Next
For Each lbl As Label In LBLs
index = R.Next(0, LBLs.Count)
tmpLocation = lbl.Location
lbl.Location = LBLs(index).Location
LBLs(index).Location = tmpLocation
Next
End Sub
End Class
----------------------------------------------------------------------------------
' Your recommendation
---------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tb As TextBox = Nothing
Dim ctl As Control = Nothing
For i As Integer = 1 to 10
ctl = GetControlByName("ProductCode" & i, Me)
If Not (ctl Is Nothing) Then
If TypeOf ctl Is TextBox Then
tb = CType(ctl, TextBox)
tb.Text = "Hello World!"
End If
End If
Next i
End Sub
ASKER
Hi Idle_Mind,
I get the following errors:
1. 'Zoom' is not a member of the 'System.Windows.Forms.Pict ureBoxSize Mode'... "pb = GetControlByName("PictureB ox" & i, Me)"
2. Syntax error in line "For i As Integer = 1 To 6"... As is underlined.
3. (i) is not declared... am I right to add = Dim i As Integer?
Cheers,
Roberto
I get the following errors:
1. 'Zoom' is not a member of the 'System.Windows.Forms.Pict
2. Syntax error in line "For i As Integer = 1 To 6"... As is underlined.
3. (i) is not declared... am I right to add = Dim i As Integer?
Cheers,
Roberto
For #1 you'd have to look at the available enumerations for SizeMode in 2003. "Zoom" was added in .Net 2.0. You can use "Stretch" in 2003 I think but this won't preserve the aspect ratio like it does with "Zoom" in the newer versions.
For #2, that should work as is. You can declare "i" above but remove the "As Integer" part in the "For" line itself.
For #2, that should work as is. You can declare "i" above but remove the "As Integer" part in the "For" line itself.
ASKER
Okay, I replaced Zoom with StretchImage = this seemed to resolve :)
I declared: DIM I but nothing really happens :(
Most of the As in the code appear with SYNTAX error :(
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
Score = 0
Counter = 0
For Each pb As PictureBox In PBs
pb.Visible = True
Next
For Each lbl As Label In LBLs
lbl.Visible = True
Next
btnScore.Enabled = False
Shuffle()
End Sub
--AND--------------------------------------
Private Sub Shuffle()
Dim index As Integer
Dim tmpLocation As Point
For Each pb As PictureBox In PBs
index = R.Next(0, PBs.Count)
tmpLocation = pb.Location
pb.Location = PBs(index).Location
PBs(index).Location = tmpLocation
Next
For Each lbl As Label In LBLs
index = R.Next(0, LBLs.Count)
tmpLocation = lbl.Location
lbl.Location = LBLs(index).Location
LBLs(index).Location = tmpLocation
Next
End Sub
Try moving all the declarations out:
Dim pb As PictureBox
For Each pb In PBs
And:
Dim lbl As Label
For Each lbl In LBLs
Dim pb As PictureBox
For Each pb In PBs
And:
Dim lbl As Label
For Each lbl In LBLs
ASKER
Hi Idle_Mind,
I removed them, but this just creates more problems. :(
The reason I've gone back to vb2003, is because I originally developed this project in vb 2008 Express Ed... when I tried tp run the complied exe, I've had nothing but trouble when tested on at least 3 different machines.
I removed them, but this just creates more problems. :(
The reason I've gone back to vb2003, is because I originally developed this project in vb 2008 Express Ed... when I tried tp run the complied exe, I've had nothing but trouble when tested on at least 3 different machines.
List out the errors for me and we'll see if we can fix 'em... =)
ASKER
I'm uploading my screen shot(s) x1/3
cd-001.JPG
cd-001.JPG
ASKER
I'm uploading my screen shot(s) x1/2
cd-002.JPG
cd-002.JPG
ASKER
I'm uploading my screen shot(s) x3/3
cd-003.JPG
cd-003.JPG
ASKER
As you can see, Syntax errors in pic 2 & 3 :(
I don't see the error in #2.
For #3, the earlier posted fix didn't help?
For #3, the earlier posted fix didn't help?
Private Sub Shuffle()
Dim index As Integer
Dim tmpLocation As Point
Dim pb As PictureBox
For Each pb In PBs
index = R.Next(0, PBs.Count)
tmpLocation = pb.Location
pb.Location = PBs(index).Location
PBs(index).Location = tmpLocation
Next
Dim lbl As Label
For Each lbl In LBLs
index = R.Next(0, LBLs.Count)
tmpLocation = lbl.Location
lbl.Location = LBLs(index).Location
LBLs(index).Location = tmpLocation
Next
End Sub
ASKER
HI Idle_Mind,
Apologies for my absence. I couldn't get this work in 2003; however, I would settle for 2 simple solutions to help me get passed this wall:
1. Randomize text in a listbox
2. Randomize picturebox images from file (like before)
1. I found a solution (by you actually), but it only randomized the selection & not the items in the list box:
Apologies for my absence. I couldn't get this work in 2003; however, I would settle for 2 simple solutions to help me get passed this wall:
1. Randomize text in a listbox
2. Randomize picturebox images from file (like before)
1. I found a solution (by you actually), but it only randomized the selection & not the items in the list box:
Private R As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListBox1.Items.Count > 0 Then
Dim index As Integer = R.Next(0, ListBox1.Items.Count)
TextBox1.Text = ListBox1.Items(index)
ListBox1.Items.RemoveAt(index)
End If
If ListBox1.Items.Count = 0 Then
Button1.Enabled = False
End If
End Sub
ASKER
oop- that code actually moved the items into a textbox - sorry about that
ASKER
of course, I will gladly increase the points ;)
Wow...this thread has been dormant for quite awhile! =)
I'll need to re-read the whole thing to figure out where we were. Try to post something tomorrow...
I'll need to re-read the whole thing to figure out where we were. Try to post something tomorrow...
ASKER
Hi
Here's a simple example of randomizing the string items in a ListBox:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim index As Integer
Dim tmp As String
Static R As New Random
For i = 0 To ListBox1.Items.Count - 1
index = R.Next(0, ListBox1.Items.Count)
tmp = ListBox1.Items(index)
ListBox1.Items.RemoveAt(index)
ListBox1.Items.Insert(i, tmp)
Next
End Sub
For #2:
"2. Randomize picturebox images from file (like before)"
When does this need to happen?
When the app first loads and we are loading the actual image files?
...or are the images already loaded and you just need to shuffle the PictureBoxes?
"2. Randomize picturebox images from file (like before)"
When does this need to happen?
When the app first loads and we are loading the actual image files?
...or are the images already loaded and you just need to shuffle the PictureBoxes?
ASKER
Hi Idle_Mind,
I would be happy to embed the pictures in an image list and have them randomly shuffle on form load. they only need to shuffle once.
I would be happy to embed the pictures in an image list and have them randomly shuffle on form load. they only need to shuffle once.
ASKER
in PictureBoxes = yes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Idle_Mind,
I think I love you ;)
Thanks mate.
I think I love you ;)
Thanks mate.
Shhhhh! My wife might see that I'm spending more time on code...than on her! ;)
ASKER
LOL,
I have another question if you're happy to accept... I have just posted it... FAO: Idle_Mind.
Thanks again m8.
I have another question if you're happy to accept... I have just posted it... FAO: Idle_Mind.
Thanks again m8.
Try this:
Open in new window