an array of controls not particularly convenient in vb examples: array of pictureboxes array of...

Post on 15-Jan-2016

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

An array of controls

Not particularly convenient in VBExamples: array of pictureboxes

Array of textboxesNotes on Concentration game (a possible final project)

Recall the microwave oven? (Proj12)

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

Simplifying 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“ won’t work anymore.

• Use:Dim a as String= sender.tostring()strtime &= a.substring(a.length-1,1)

An array of textboxes

The entire code… so farPublic 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 SubEnd Class

When text is changed in any textbox, the 4th textbox gets the new text

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

About 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 won’t fit/display properly, so choose small files.

• You’ll also need to make sure your form is “big enough” to hold a bunch of pictureboxes…

• The example shown here has just 3.

Three pictureboxes (backs)

Clicking a picturebox “flips it over”

And clicking again shows the back

Field declarations and formload• fieldsDim cntr(3) As IntegerDim names As String() = {"c:\icecream.jpg", "c:\gfruit.jpg", "c:\

fatlady.jpg"} ‘the pictures I’ll useDim 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 picturesDim i As IntegerFor i = 0 To names.Length - 1 cntr(i) = 0 ’zero counters

pics(i).Image = Image.FromFile(names(i)) ‘put on a picture Next

Clicking 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

PictureBox1_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:\

jraff.jpg") Else ‘if odd show front pics(0).Image = Image.FromFile(names(0)) End If End Sub

Cutting down on the necessary code- V1.2 of textbox array example

The entire code for this examplePublic 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

End Class

shuffling

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

Shuffling: 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

shufflingpos1 = 0 pos2 = 0 picnum = 0Do While picnum < 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) <> -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 moreLoop

Using a listbox to display successful shuffling

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

Concentration 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 doesn’t do anything.

• You’ll need to re-enable pictureboxes, set backs to back-of-card image, and do all the reshuffling code for the play again button.

Concentration 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 you’ll 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.

Checking to see if I can flip over cards

Flipping over cards when clickedPrivate 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

Beginning 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)

formload

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 designDim i As Integer For i = 0 To 5 pics(i).Enabled = False Next End Sub

More remarks

• You may want a boolean named match to keep track of whether there was a match

• You’ll 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)

top related