Title: An array of controls
1An array of controls
- Not particularly convenient in VB
- Examples array of pictureboxes
- Array of textboxes
- Notes on Concentration game (a possible final
project)
2Recall the microwave oven? (Proj12)
3Digit-button handlers
- We built a subroutine for every button which
looked like this - Private Sub Btn1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Btn1.Click - Beep()
- strtime "1"
- displayTime()
- End Sub
4Simplifying the code
- Make one sub handle all the digit buttons
- Private Sub Digit_Button(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Btn1.Click,Btn2.Click, - Delete the old subs and edit the code inside this
one strtime "1 wont work anymore. - Use
- Dim a as String sender.tostring()
- strtime a.substring(a.length-1,1)
5An array of textboxes
6The entire code so far
- Public Class Form1
- Dim txt As TextBox() field value at top so
txt(i) can be recognized - Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load - txt New TextBox() TextBox1, TextBox2,
TextBox3 - assign actual names to elements of txtbox array
- Dim i As Integer
- For i 0 To txt.Length - 1
- txt(i).Text "I am txtbox" I
display something - Next
- End Sub
- End Class
7When text is changed in any textbox, the 4th
textbox gets the new text
8Code for one of the textchanged events
- Private Sub TextBox1_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles TextBox1.TextChanged - TextBox4.Text txt(0).Text
- End Sub
- This code has an event handler sub for each
control in the array. A lot of unnecessary
code!!!! - It is possible to get one event handler (sub) to
handle all the textbox changed events, and thus
to save a lot of code. See third example in this
slideshow.
9About the next example
- This short ppt shows how to declare an array of
controls (in this case pictureboxes) - And then to coordinate action on those array
elements based on the actual pictureboxes placed
on the form during design. - You will need to be concerned somewhat with the
size of the image file too big wont
fit/display properly, so choose small files. - Youll also need to make sure your form is big
enough to hold a bunch of pictureboxes - The example shown here has just 3.
10Three pictureboxes (backs)
11Clicking a picturebox flips it over
12And clicking again shows the back
13Field declarations and formload
- fields
- Dim cntr(3) As Integer
- Dim names As String() "c\icecream.jpg",
"c\gfruit.jpg", "c\fatlady.jpg" the pictures
Ill use - Dim pics As PictureBox() must go at top so
different subs can access - Formload method
- Need to assign array elements to actual allocated
controls - pics New PictureBox() PictureBox1,
PictureBox2, PictureBox3 - these are the actual pic box names I used in
design - Load initial pictures
- Dim i As Integer
- For i 0 To names.Length - 1
- cntr(i) 0 zero counters
- pics(i).Image Image.FromFile(names(i)) put
on a picture - Next
14Clicking a picturebox
- Clicking a picturebox toggles the displayed
image - we keep track by counting mod 2
- Increment the counter and mod by 2 each time
picturebox is clicked - Set the image based on odd/even count value
15PictureBox1_Click code (similar for others)
- Private Sub PictureBox1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles PictureBox1.Click - cntr(0) (cntr(0) 1) Mod 2 count mod
2 - If cntr(0) 0 Then if even show back
- pics(0).image Image.FromFile("c\jra
ff.jpg") - Else if odd show front
- pics(0).Image Image.FromFile(names(0
)) - End If
- End Sub
16Cutting down on the necessary code- V1.2 of
textbox array example
17The entire code for this example
- Public Class Form1
- Dim txt As TextBox()
- Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load - txt New TextBox() TextBox1, TextBox2,
TextBox3 - Dim i As Integer
- For i 0 To txt.Length - 1
- txt(i).Text "I am txtbox" i
- Next
- End Sub
- a single sub is called if any textbox is
changed - Private Sub TextisChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles TextBox1.TextChanged, TextBox2.TextChanged
, TextBox3.TextChanged - Dim i As Integer
- For i 0 To txt.Length - 1
- If sender.Equals(txt(i)) Then see
which one was changed - TextBox4.Text txt(i).Text
then change txtbox4 appropriately - End If
- Next
- End Sub
18shuffling
- Need to mark all cards as not shuffled.
- I used an array of ints set to -1 to mean not
shuffled - Need to pick 2 random values.
- I picked one random value a position in the
array that was still not shuffled and then
picked the 2nd one by adding an amount mod
picnums to the first. - Mark these positions with their picnum
- Keep going until all positions have been
shuffled. - For ten cards, you need 5 pictures, and you need
to go around successfully picking pairs 5 times. - Not shown after shuffling, display the pictures
using the numbers stored in the array.
19Shuffling set all cards to not shuffled
- Dim i As Integer
- For i 0 To 5
- cntr(i) -1 'empty counters
- pics(i).Image Image.FromFile("c\jraff.
jpg") - 'put on back picture
- Next
-
20shuffling
- pos1 0
- pos2 0
- picnum 0
- Do While picnum lt 3 I used 6 pictures so I need
3 pairs - pos1 r.Next(6) I used 6 pictures
so get a random 0..5 - Do While cntr(pos1) ltgt -1 while a
shuffled card picked - pos1 r.Next(6) pick another
- Loop
- pos2 (pos1 5) Mod 6 arbitrarily
pick his pair card - cntr(pos1) picnum set these two
cards with their picnum - cntr(pos2) picnum
- picnum picnum 1 success so count
one more - Loop
21Using a listbox to display successful shuffling
22Remarks on prev slide
- I put a listbox on just to see what pairs I got.
- Next step after testing shuffling would be to
begin coding the actual concentration game.
23Concentration game
- Form load should shuffle integers and display
card backs. - You might want a button called play again.
- Not shown in this ppt you might set background
color of pictureboxes to black to indicated a
pair has been chosen. - You can disable pictureboxes when they are
successfully picked so that clicking doesnt do
anything. - Youll need to re-enable pictureboxes, set backs
to back-of-card image, and do all the reshuffling
code for the play again button.
24Concentration game..how to play
- Not shown in ppt. When a game starts, you need
to set a click counter to zero. You also need a
pair counter set to 0 to count the number of
matched pairs. - When a picturebox is clicked, increment your
click count, flip it over (the integer in my cntr
array tells which picture to draw on it.) Maybe
youll want to save his picnum somewhere. - When a 2nd picturebox is clicked, click count
again. If click count is 2, Get the picnum of the
2nd picture. Check the picnumbers on the clicked
pictures. If they are the same, disable these
pictureboxes, put backgrounds to black, count the
pair match, set click counter back to zero.
25Checking to see if I can flip over cards
26Flipping over cards when clicked
- Private Sub PicClick(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles PictureBox1.Click, PictureBox2.Click,
PictureBox3.Click, PictureBox4.Click,
PictureBox5.Click, PictureBox6.Click - Dim i As Integer
- For i 0 To 5
- If sender.Equals(pics(i)) Then
- 'see which one was changed
- pics(i).Image
Image.FromFile(names(cntr(i))) 'then change
picbox appropriately - End If
- Next
- End Sub
27Beginning the game
- In formload, disable all the pictureboxes. (next
slide) - Add a start button and put the code to shuffle
the cards and enable the pictureboxes in the
event sub for that button. (not shown)
28formload
- Private Sub Form1_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load - pics New PictureBox() PictureBox1,
PictureBox2, PictureBox3, PictureBox4,
PictureBox5, PictureBox6 - 'these are the pic box names I used in
design - Dim i As Integer
- For i 0 To 5
- pics(i).Enabled False
- Next
- End Sub
29More remarks
- You may want a boolean named match to keep track
of whether there was a match - Youll need another image to put on pictures
which have been matched (plus disable them) - Make sure that the player clicks two different
pictures (not the same one twice)