Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Stephens' Visual Basic Programming 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' Visual Basic Programming 24-Hour Trainer by Rod Stephens; ISBN: 978-0-470-94335-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' Visual Basic Programming 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 23rd, 2011, 12:15 AM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: Tennessee
Posts: 46
Thanks: 27
Thanked 0 Times in 0 Posts
Thumbs down Ex 2-1 TicTacToe

I finished the first example and then added a button to clear all the large labels. This isn't working because of my 'clear' line. Any suggestions or will I learn all about this later?? It also didn't work without the extra " marks.

code
Private Sub btnNewGame_Click(sender As System.Object, e As System.EventArgs) Handles btnNewGame.Click
' clear all lblTaken
For x = 0 To 2
For y = 0 To 2
"lblTaken"xy".text" = ""
Next y
Next x

End Sub
/code
  #2 (permalink)  
Old May 23rd, 2011, 09:02 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Unfortunately you can't use a string to name a control in this way.

Code:
"lblTaken"xy".text" = ""
There are several ways you can approach this. The easiest but least satisfying is to just clear them individually:

Code:
lblTaken00.Text = ""
lblTaken01.Text = ""
lblTaken02.Text = ""
...
Probably if you are going to turn this into a more realistic program, you would want to make an array holding the labels. Then you can loop through the array to clear them out. Arrays are described in Lesson 16, although I don't think that lesson revisits the Tic-Tac-Toe program.

The program would declare the array outside of any method:

Code:
Private Labels As Label(,)
When it starts, the program would initialize the array something like this:

Code:
Labels = New Label(,) _
{ _
    {lblTaken00, lblTaken01, lblTaken02}, _
    {lblTaken10, lblTaken11, lblTaken12}, _
    {lblTaken20, lblTaken21, lblTaken22} _
}
Later you can loop through the array like this:

Code:
For r As Integer = 0 To 2
    For c As Integer = 0 To 2
        Labels(r, c).Text = ""
    Next c
Next r
This is all getting pretty far ahead of the Tic-Tac-Toe program in Lesson 2 but perhaps later you can revisit this and see if you can make it work.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
zavodney (May 23rd, 2011)
  #3 (permalink)  
Old May 23rd, 2011, 02:57 PM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: Tennessee
Posts: 46
Thanks: 27
Thanked 0 Times in 0 Posts
Thumbs up Thank you.

OK, thank you. I can wait until later. Clearing each label individually was an option, but it wasn't a satisfying way to do it. Thank you.

Keith






Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.