Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

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

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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

Avatar of RobertoFreemano

ASKER

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

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

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
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.
List out the errors for me and we'll see if we can fix 'em...  =)
I'm uploading my screen shot(s) x1/3
cd-001.JPG
I'm uploading my screen shot(s) x1/2
cd-002.JPG
I'm uploading my screen shot(s) x3/3
cd-003.JPG
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?
    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

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

oop- that code actually moved the items into a textbox - sorry about that
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...
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

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?
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.
in PictureBoxes = yes
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Idle_Mind,

I think I love you ;)

Thanks mate.
Shhhhh!   My wife might see that I'm spending more time on code...than on her!   ;)
LOL,

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

Thanks again m8.