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
' 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

Open in new window

RobertoFreemanoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't have VS2003 on my systems anymore so I can't test it out...

Try this:
Public Class Form1

    Private R As New Random
    Private Score As Integer = 0
    Private Counter As Integer = 0
    Private PBs As New ArrayList
    Private LBLs As New ArrayList

    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
        For i As Integer = 1 To 6
            pb = GetControlByName("PictureBox" & i, Me)
            If Not (pb Is Nothing) Then
                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

            lbl = GetControlByName("Label" & i, Me)
            If Not (lbl Is Nothing) Then
                lbl.Text = data(i - 1, 1)
                LBLs.Add(lbl)
                AddHandler lbl.MouseMove, AddressOf lbl_MouseMove
            End If
        Next
        btnReset.PerformClick()
    End Sub

    Private Function GetControlByName(ByVal ctlName As String, ByVal container As Control) As Control
        Dim c As Control
        For Each c In container.Controls
            If c.Name.ToLower.Equals(ctlName.ToLower) Then
                Return c
            ElseIf c.Controls.Count > 0 Then
                Return GetControlByName(ctlName, c)
            End If
        Next
    End Function

    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

Open in new window

0
RobertoFreemanoAuthor Commented:
Hi Idle_Mind,

I get the following errors:

1. 'Zoom' is not a member of the 'System.Windows.Forms.PictureBoxSizeMode'... "pb = GetControlByName("PictureBox" & 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
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

RobertoFreemanoAuthor Commented:

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

Open in new window

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
RobertoFreemanoAuthor Commented:
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.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
List out the errors for me and we'll see if we can fix 'em...  =)
0
RobertoFreemanoAuthor Commented:
I'm uploading my screen shot(s) x1/3
cd-001.JPG
0
RobertoFreemanoAuthor Commented:
I'm uploading my screen shot(s) x1/2
cd-002.JPG
0
RobertoFreemanoAuthor Commented:
I'm uploading my screen shot(s) x3/3
cd-003.JPG
0
RobertoFreemanoAuthor Commented:
As you can see, Syntax errors in pic 2 & 3 :(
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't see the error in #2.

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

Open in new window

0
RobertoFreemanoAuthor Commented:
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:
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

Open in new window

0
RobertoFreemanoAuthor Commented:
oop- that code actually moved the items into a textbox - sorry about that
0
RobertoFreemanoAuthor Commented:
of course, I will gladly increase the points ;)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
RobertoFreemanoAuthor Commented:
Hi
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Open in new window

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
RobertoFreemanoAuthor Commented:
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.
0
RobertoFreemanoAuthor Commented:
in PictureBoxes = yes
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here's an example of loading up the PictureBoxes from an ImageList and then a routine to shuffle the Images in the PictureBoxes:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim pb As PictureBox
        Dim i As Integer
        For i = 0 To ImageList1.Images.Count - 1
            pb = GetControlByName(Me, "PictureBox" & (i + 1))
            If Not (pb Is Nothing) Then
                pb.Image = ImageList1.Images(i)
            End If
        Next
        ShuffleImages()
    End Sub

    Private Function GetControlByName(ByVal cont As Control, ByVal name As String) As Control
        Dim ctl As Control
        Dim foundCtl As Control
        For Each ctl In cont.Controls
            If ctl.Name.ToLower = name.ToLower Then
                Return ctl
            ElseIf ctl.HasChildren Then
                foundCtl = GetControlByName(ctl, name)
                If Not (foundCtl Is Nothing) Then
                    Return foundCtl
                End If
            End If
        Next
        Return Nothing
    End Function

    Private Sub ShuffleImages()
        Dim i As Integer
        Dim index As Integer
        Dim tmpImage As Image
        Dim pb As PictureBox
        Dim pb2 As PictureBox
        Static R As New Random
        For i = 1 To 4
            pb = GetControlByName(Me, "PictureBox" & i)
            If Not (pb Is Nothing) Then
                tmpImage = pb.Image

                index = R.Next(1, 5) ' 1 to 4 inclusive
                pb2 = GetControlByName(Me, "PictureBox" & index)
                If Not (pb2 Is Nothing) Then
                    pb.Image = pb2.Image
                    pb2.Image = tmpImage
                End If
            End If
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShuffleImages()
    End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobertoFreemanoAuthor Commented:
Idle_Mind,

I think I love you ;)

Thanks mate.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Shhhhh!   My wife might see that I'm spending more time on code...than on her!   ;)
0
RobertoFreemanoAuthor Commented:
LOL,

I have another question if you're happy to accept... I have just posted it... FAO: Idle_Mind.

Thanks again m8.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.