An array of controls Not particularly convenient in VB Examples: array of pictureboxes Array of textboxes Notes on Concentration game (a possible final.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Lists, Loops, Validation, and More
Practical Programming COMP153-08S Lecture: Repetition Continued.
Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Control structures Part 2 iteration control To enable repetition of a statement block.
Odds and Ends Component Tray Menu and contextmenu Splash Screen.
Arrays & Strings part 2 More sophisticated algorithms, including sorting.
Mouse draw Using the mouse and a palette to make a picture.
More on lists, exceptions, loops and validation. You can use the exception to indicate the error that occurred Private Sub btnCheck_Click(ByVal sender.
Additional loop presentation
Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales.
5.05 Apply Looping Structures
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Graphics and Multimedia. Outline Introduction to Multimedia Loading, Displaying and Scaling Images Windows Media Player Adding a Flash Movie Microsoft.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
CS0004: Introduction to Programming Variables – Numbers.
1 Insert, Update and Delete Queries. 2 Return to you Address Book database. Insert a record.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
InvEasy (Project1) Please use speaker notes for additional information!
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
Visual Basic Games: Prepare for Hangman
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
Visit to download CodeAwesomeness.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Array - adding to array at run time Please see speaker notes for additional information!
Chapter 6 Sub Procedures
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
Some graphics. Projects included A slideshow a dark moon moving phases of the moon billiards Your own icons and bitmaps Pie chart.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Tutorial 6 The Repetition Structure
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
1 ball, 2 ball, red ball, blue ball By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
1 Advanced Computer Programming Lab Calculator Project.
Two Forms Please use speaker notes for additional information!
VCE IT Theory Slideshows By Mark Kelly Vceit.com Arrays.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
6b – Sub Procedure With Parameters Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
List Boxes and Combo Boxes Provides a list of items to select from Various styles — choose based on Space available Need to select from an existing list.
Lab 10 Slides.
Practical Programming COMP153-08S Lecture: Repetition.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Computer Science Up Down Controls, Decisions and Random Numbers.
Directory and File. Access Files in a Directory Name space: System.IO The Directory and File classes contain only shared methods that set or return information.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Visual Basic Fundamental Concepts
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Single Dimensional Arrays
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Visual Basic..
Visual Basic: Week 5 Review User defined functions
Please use speaker notes for additional information!
To understand what arrays are and how to use them
CIS 338: Images on Forms Dr. Ralph D. Westfall May, 2009.
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
GUI Programming in Visual Studio .NET
Presentation transcript:

An array of controls Not particularly convenient in VB Examples: array of pictureboxes Array of textboxes Notes 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 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

When text is changed in any textbox, the 4 th 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 fields Dim cntr(3) As Integer Dim names As String() = {"c:\icecream.jpg", "c:\gfruit.jpg", "c:\fatlady.jpg"} ‘the pictures I’ll 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

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 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 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 2 nd 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

shuffling pos1 = 0 pos2 = 0 picnum = 0 Do 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 more Loop

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 2 nd picturebox is clicked, click count again. If click count is 2, Get the picnum of the 2 nd 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 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

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 design Dim 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)