help in manipulating pictureboxes positions

ljpv14

Active member
Joined
Nov 28, 2011
Messages
44
Programming Experience
3-5
hey guys, i need help in my logic formulation. i'm creating a game like text twist. that when i press letter a, i need to determine which picturebox contains letter a and display it to different location. that also goes to the proceeding letter a's. for example, i have a jumbled letters "aamba". in my code, it only gets the first letter a and removes it form the screen, it's nowhere to be found. and it won't recognize that i still have other letter a's i'm pretty sure about the location that i set it to be placed.

here is my code: (it's kind of brute force i think)
 ElseIf e.KeyChar = Chr(97) Then
            counter1 = 0
            If letterPicbox1.Tag Is "a" Then
                If counter1 = 0 Then
                    If currentWordLen = 5 Then
                        letterPicbox1.Location = New Point(145, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox1.Location = New Point(110, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox1.Location = New Point(80, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 1 Then
                    If currentWordLen = 5 Then
                        letterPicbox1.Location = New Point(225, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox1.Location = New Point(190, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox1.Location = New Point(160, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 2 Then
                    If currentWordLen = 5 Then
                        letterPicbox1.Location = New Point(305, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox1.Location = New Point(270, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox1.Location = New Point(240, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 3 Then
                    If currentWordLen = 5 Then
                        letterPicbox1.Location = New Point(385, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox1.Location = New Point(350, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox1.Location = New Point(320, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 4 Then
                    If currentWordLen = 5 Then
                        letterPicbox1.Location = New Point(465, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox1.Location = New Point(430, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox1.Location = New Point(400, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 5 Then
                    If currentWordLen = 6 Then
                        letterPicbox1.Location = New Point(510, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox1.Location = New Point(480, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 6 Then
                    If currentWordLen = 7 Then
                        letterPicbox1.Location = New Point(560, 100)
                        counter1 += 1
                    End If
                End If
            ElseIf letterPicbox2.Tag Is "a" Then
                If counter1 = 0 Then
                    If currentWordLen = 5 Then
                        letterPicbox2.Location = New Point(145, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox2.Location = New Point(110, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox2.Location = New Point(80, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 1 Then
                    If currentWordLen = 5 Then
                        letterPicbox2.Location = New Point(225, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox2.Location = New Point(190, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox2.Location = New Point(160, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 2 Then
                    If currentWordLen = 5 Then
                        letterPicbox2.Location = New Point(305, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox2.Location = New Point(270, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox2.Location = New Point(240, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 3 Then
                    If currentWordLen = 5 Then
                        letterPicbox2.Location = New Point(385, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox2.Location = New Point(350, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox2.Location = New Point(320, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 4 Then
                    If currentWordLen = 5 Then
                        letterPicbox2.Location = New Point(465, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox2.Location = New Point(430, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox2.Location = New Point(400, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 5 Then
                    If currentWordLen = 6 Then
                        letterPicbox2.Location = New Point(510, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox2.Location = New Point(480, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 6 Then
                    If currentWordLen = 7 Then
                        letterPicbox2.Location = New Point(560, 100)
                        counter1 += 1
                    End If
                End If
            ElseIf letterPicbox3.Tag Is "a" Then
                If counter1 = 0 Then
                    If currentWordLen = 5 Then
                        letterPicbox3.Location = New Point(145, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox3.Location = New Point(110, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox3.Location = New Point(80, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 1 Then
                    If currentWordLen = 5 Then
                        letterPicbox3.Location = New Point(225, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox3.Location = New Point(190, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox3.Location = New Point(160, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 2 Then
                    If currentWordLen = 5 Then
                        letterPicbox3.Location = New Point(305, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox3.Location = New Point(270, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox3.Location = New Point(240, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 3 Then
                    If currentWordLen = 5 Then
                        letterPicbox3.Location = New Point(385, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox3.Location = New Point(350, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox3.Location = New Point(320, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 4 Then
                    If currentWordLen = 5 Then
                        letterPicbox3.Location = New Point(465, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox3.Location = New Point(430, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox3.Location = New Point(400, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 5 Then
                    If currentWordLen = 6 Then
                        letterPicbox3.Location = New Point(510, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox3.Location = New Point(480, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 6 Then
                    If currentWordLen = 7 Then
                        letterPicbox3.Location = New Point(560, 100)
                        counter1 += 1
                    End If
                End If
            ElseIf letterPicbox4.Tag Is "a" Then
                If counter1 = 0 Then
                    If currentWordLen = 5 Then
                        letterPicbox4.Location = New Point(145, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox4.Location = New Point(110, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox4.Location = New Point(80, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 1 Then
                    If currentWordLen = 5 Then
                        letterPicbox4.Location = New Point(225, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox4.Location = New Point(190, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox4.Location = New Point(160, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 2 Then
                    If currentWordLen = 5 Then
                        letterPicbox4.Location = New Point(305, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox4.Location = New Point(270, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox4.Location = New Point(240, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 3 Then
                    If currentWordLen = 5 Then
                        letterPicbox4.Location = New Point(385, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox4.Location = New Point(350, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox4.Location = New Point(320, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 4 Then
                    If currentWordLen = 5 Then
                        letterPicbox4.Location = New Point(465, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox4.Location = New Point(430, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox4.Location = New Point(400, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 5 Then
                    If currentWordLen = 6 Then
                        letterPicbox4.Location = New Point(510, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox4.Location = New Point(480, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 6 Then
                    If currentWordLen = 7 Then
                        letterPicbox4.Location = New Point(560, 100)
                        counter1 += 1
                    End If
                End If
            ElseIf letterPicbox5.Tag Is "a" Then
                If counter1 = 0 Then
                    If currentWordLen = 5 Then
                        letterPicbox5.Location = New Point(145, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox5.Location = New Point(110, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox5.Location = New Point(80, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 1 Then
                    If currentWordLen = 5 Then
                        letterPicbox5.Location = New Point(225, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox5.Location = New Point(190, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox5.Location = New Point(160, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 2 Then
                    If currentWordLen = 5 Then
                        letterPicbox5.Location = New Point(305, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox5.Location = New Point(270, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox5.Location = New Point(240, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 3 Then
                    If currentWordLen = 5 Then
                        letterPicbox5.Location = New Point(385, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox5.Location = New Point(350, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox5.Location = New Point(320, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 4 Then
                    If currentWordLen = 5 Then
                        letterPicbox5.Location = New Point(465, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 6 Then
                        letterPicbox5.Location = New Point(430, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox5.Location = New Point(400, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 5 Then
                    If currentWordLen = 6 Then
                        letterPicbox5.Location = New Point(510, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox5.Location = New Point(480, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 6 Then
                    If currentWordLen = 7 Then
                        letterPicbox5.Location = New Point(560, 100)
                        counter1 += 1
                    End If
                End If
            ElseIf letterPicbox6.Tag Is "a" Then
                If counter1 = 0 Then
                    If currentWordLen = 6 Then
                        letterPicbox6.Location = New Point(110, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox6.Location = New Point(80, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 1 Then
                    If currentWordLen = 6 Then
                        letterPicbox6.Location = New Point(190, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox6.Location = New Point(160, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 2 Then
                    If currentWordLen = 6 Then
                        letterPicbox6.Location = New Point(270, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox6.Location = New Point(240, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 3 Then
                    If currentWordLen = 6 Then
                        letterPicbox6.Location = New Point(350, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox6.Location = New Point(320, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 4 Then
                    If currentWordLen = 6 Then
                        letterPicbox6.Location = New Point(430, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox6.Location = New Point(400, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 5 Then
                    If currentWordLen = 6 Then
                        letterPicbox6.Location = New Point(510, 100)
                        counter1 += 1
                    ElseIf currentWordLen = 7 Then
                        letterPicbox6.Location = New Point(480, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 6 Then
                    If currentWordLen = 7 Then
                        letterPicbox6.Location = New Point(560, 100)
                        counter1 += 1
                    End If
                End If
            ElseIf letterPicbox7.Tag Is "a" Then
                If counter1 = 0 Then
                    If currentWordLen = 7 Then
                        letterPicbox7.Location = New Point(80, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 1 Then
                    If currentWordLen = 7 Then
                        letterPicbox7.Location = New Point(160, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 2 Then
                    If currentWordLen = 7 Then
                        letterPicbox7.Location = New Point(240, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 3 Then
                    If currentWordLen = 7 Then
                        letterPicbox7.Location = New Point(320, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 4 Then
                    If currentWordLen = 7 Then
                        letterPicbox7.Location = New Point(400, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 5 Then
                    If currentWordLen = 7 Then
                        letterPicbox7.Location = New Point(480, 100)
                        counter1 += 1
                    End If
                ElseIf counter1 = 6 Then
                    If currentWordLen = 7 Then
                        letterPicbox7.Location = New Point(560, 100)
                        counter1 += 1
                    End If
                End If
            End If
        End If

I don't know if I just missed out some code since it's too long. Or my logic wasn't that good to perform what I want with my letters. If anyone can provide me a better logic or even a suggestion. I will be fully thankful! THANKS! :D
 
Last edited by a moderator:
First things first, why check whether e.KeyChar is "a" and then, if it is, test every PictureBox for "a"? Why not just test every PictureBox for e.KeyChar?

Second, why use the same code over and over each different PictureBox when you could simply find the PictureBox first, assign it to a variable and then use the code once with that variable?

Putting those together, you end up with this:
Dim pb As PictureBox = Nothing

Select Case e.KeyChar.ToString()
    Case CStr(letterPicbox1.Tag)
        pb = letterPicbox1
    Case CStr(letterPicbox2.Tag)
        pb = letterPicbox2
    Case CStr(letterPicbox3.Tag)
        pb = letterPicbox3
    Case CStr(letterPicbox4.Tag)
        pb = letterPicbox4
    Case CStr(letterPicbox5.Tag)
        pb = letterPicbox5
    Case CStr(letterPicbox6.Tag)
        pb = letterPicbox6
    Case CStr(letterPicbox7.Tag)
        pb = letterPicbox7
End Select

If pb IsNot Nothing Then
    '...
End If
If you want to throw in some LINQ then that can be simplified further. Assuming that they are the only PictureBoxes on the form you could use this:
Dim pb = Me.Controls.OfType(Of PictureBox).FirstOrDefault(Function(c) CStr(c.Tag) = e.KeyChar.ToString())
Also, you should not be looking for the same currentWordLen over and over. Given that the value of interest is based on the current PictureBox, put them into an array and use Array.IndexOf to get an index that you can then use to determine what the currentWordLen needs to be. You then just need one Select Case for the counter1 value.
 
i need to determine what the currentwordlen is since the position i will assign to each picturebox is based on the length of the word. so after determining if a certain picturebox has a tag "a" when I press "a" and assign it to a variable, I need to give it a position on the screen. then i need again to determine whether there are other "a" in the word. and again place it next to the previous "a". everytime i press "a" It should just process the first "a" not all "a"'s but what's happening in my code is only one letter "a" is processed then when i press "a" nothing happens to the rest. I believe that goes to other letters if I try to put a code for them.
 
I didn't say that you should not be looking for currentWordLen. I said that you shouldn't be looking for it over and over. Look at this for example:
               If counter1 = 0 Then
                   If currentWordLen = 7 Then
                       letterPicbox7.Location = New Point(80, 100)
                       counter1 += 1
                   End If
               ElseIf counter1 = 1 Then
                   If currentWordLen = 7 Then
                       letterPicbox7.Location = New Point(160, 100)
                       counter1 += 1
                   End If
               ElseIf counter1 = 2 Then
                   If currentWordLen = 7 Then
                       letterPicbox7.Location = New Point(240, 100)
                       counter1 += 1
                   End If
               ElseIf counter1 = 3 Then
                   If currentWordLen = 7 Then
                       letterPicbox7.Location = New Point(320, 100)
                       counter1 += 1
                   End If
               ElseIf counter1 = 4 Then
                   If currentWordLen = 7 Then
                       letterPicbox7.Location = New Point(400, 100)
                       counter1 += 1
                   End If
               ElseIf counter1 = 5 Then
                   If currentWordLen = 7 Then
                       letterPicbox7.Location = New Point(480, 100)
                       counter1 += 1
                   End If
               ElseIf counter1 = 6 Then
                   If currentWordLen = 7 Then
                       letterPicbox7.Location = New Point(560, 100)
                       counter1 += 1
                   End If
               End If
Why can't you just look for currentWordLen once?
               If currentWordLen = 7 Then
                   If counter1 = 0 Then
                       letterPicbox7.Location = New Point(80, 100)
                   ElseIf counter1 = 1 Then
                       letterPicbox7.Location = New Point(160, 100)
                   ElseIf counter1 = 2 Then
                       letterPicbox7.Location = New Point(240, 100)
                   ElseIf counter1 = 3 Then
                       letterPicbox7.Location = New Point(320, 100)
                   ElseIf counter1 = 4 Then
                       letterPicbox7.Location = New Point(400, 100)
                   ElseIf counter1 = 5 Then
                       letterPicbox7.Location = New Point(480, 100)
                   ElseIf counter1 = 6 Then
                       letterPicbox7.Location = New Point(560, 100)
                   End If

                   counter1 += 1
               End If
That can be simplified again because your Y coordinate is always 100 and your X coordinate is always (80 * (counter1 + 1)), so you can get rid of all the If...ElseIf stuff.
 
Sir, I altered your code and added some codes. Here is the result:

Dim pb As New PictureBox
Select Case (e.KeyChar)
Case CChar(letterPicbox1.Tag)
pb = letterPicbox1
Case CChar(letterPicbox2.Tag)
pb = letterPicbox2
Case CChar(letterPicbox3.Tag)
pb = letterPicbox3
Case CChar(letterPicbox4.Tag)
pb = letterPicbox4
Case CChar(letterPicbox5.Tag)
pb = letterPicbox5
Case CChar(letterPicbox6.Tag)
pb = letterPicbox6
Case CChar(letterPicbox7.Tag)
pb = letterPicbox7
End Select


Select Case (currentWordLen)
Case 5
If counter1 = 0 Then
pb.Location = New Point(145, 100)
ElseIf counter1 = 1 Then
pb.Location = New Point(225, 100)
ElseIf counter1 = 2 Then
pb.Location = New Point(305, 100)
ElseIf counter1 = 3 Then
pb.Location = New Point(385, 100)
ElseIf counter1 = 4 Then
pb.Location = New Point(465, 100)
End If
counter1 += 1
Case 6
If counter1 = 0 Then
pb.Location = New Point(110, 100)
ElseIf counter1 = 1 Then
pb.Location = New Point(190, 100)
ElseIf counter1 = 2 Then
pb.Location = New Point(270, 100)
ElseIf counter1 = 3 Then
pb.Location = New Point(350, 100)
ElseIf counter1 = 4 Then
pb.Location = New Point(430, 100)
ElseIf counter1 = 5 Then
pb.Location = New Point(510, 100)
End If
counter1 += 1
Case 7
If counter1 = 0 Then
pb.Location = New Point(80, 100)
ElseIf counter1 = 1 Then
pb.Location = New Point(160, 100)
ElseIf counter1 = 2 Then
pb.Location = New Point(240, 100)
ElseIf counter1 = 3 Then
pb.Location = New Point(320, 100)
ElseIf counter1 = 4 Then
pb.Location = New Point(400, 100)
ElseIf counter1 = 5 Then
pb.Location = New Point(480, 100)
ElseIf counter1 = 6 Then
pb.Location = New Point(560, 100)
End If
counter1 += 1
End Select

The code works for a single letter, but not for multiple letters. Whenever I have 2 letter "A" it only recognizes the first one but not the other "A". I've been tinkering this code for a while and I give up! I need expert's help! :)
 
I'm unsure of your design logic, but here's a tip about dynamically flowed control locations: the FlowLayoutPanel can manage this for you automatically, including changing these based on index.
Also, maybe using drag-drop will be a better interface for user to rearrange the controls/letters.
 
shot1.jpg

Here is the sample screen shot. When I press letter "a" it actually move to the first location. but when I press "A" again, the next letter "a" is not moving beside the first letter but rather the same first letter "a" is the one moving, that means only the first letter 'a' is being recognized.
 
Back
Top