Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,138 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,251 people online right now. Registration is fast and FREE... Join Now!




Intro to VB

 
Reply to this topicStart new topic

Intro to VB, need to change colors in labels

cckmm2407
19 Jul, 2008 - 11:15 AM
Post #1

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 5

I've coded a program called Concentration Game from the source technologies website's chapter 7 entry. The textbook, VB2005 reloaded, assignment requires us to change the BackColor when 2 items are matched. I've got that figured out, and even put in an else statement turning the color red for a mismatch. I'd like to (as I think the assignment requests) change the BackColor to a different color for each match. I've tried an embeded IfThenElse, but the code is set up for two conditions...firstLabel and secondLabel.
I'm not sure where to go from here. I think it's got to be another IfThenElse, or even a Select, but how do I make it determine that the user is on another set of matches?

Entire Code is...

vb

Option Explicit On
Option Strict On

Public Class MainForm

Private firstLabel As Label
Private secondLabel As Label
Private selectionCounter As Integer

Private Sub ShuffleWords()
' shuffles the words in the list box

Dim randomGenerator As New Random
Dim index1 As Integer
Dim index2 As Integer
Dim temp As String

For counter As Integer = 1 To 16
' generate two random numbers
index1 = randomGenerator.Next(0, 16)
index2 = randomGenerator.Next(0, 16)
' swap the two words
temp = wordListBox.Items(index1).ToString
wordListBox.Items(index1) = wordListBox.Items(index2)
wordListBox.Items(index2) = temp
Next counter

If selectionCounter = 16 Then
MessageBox.Show("Game Over.", _
"Concentration Game", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
End Sub

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' fills the list box with 8 pairs of matching words,
' then calls a procedure to shuffle the words

wordListBox.Items.Add("Refrigerator")
wordListBox.Items.Add("Range")
wordListBox.Items.Add("Television")
wordListBox.Items.Add("Computer")
wordListBox.Items.Add("Washer/Dryer")
wordListBox.Items.Add("Dishwasher")
wordListBox.Items.Add("Car")
wordListBox.Items.Add("Trip")
wordListBox.Items.Add("Refrigerator")
wordListBox.Items.Add("Range")
wordListBox.Items.Add("Television")
wordListBox.Items.Add("Computer")
wordListBox.Items.Add("Washer/Dryer")
wordListBox.Items.Add("Dishwasher")
wordListBox.Items.Add("Car")
wordListBox.Items.Add("Trip")

Call ShuffleWords()

End Sub

Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub

Private Sub newButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles newButton.Click
' removes any words from the label controls, then
' enables the label controls, then resets the
' selection counter, and then calls a procedure
' to shuffle the words

Label1.Text = String.Empty
Label2.Text = String.Empty
Label3.Text = String.Empty
Label4.Text = String.Empty
Label5.Text = String.Empty
Label6.Text = String.Empty
Label7.Text = String.Empty
Label8.Text = String.Empty
Label9.Text = String.Empty
Label10.Text = String.Empty
Label11.Text = String.Empty
Label12.Text = String.Empty
Label13.Text = String.Empty
Label14.Text = String.Empty
Label15.Text = String.Empty
Label16.Text = String.Empty

Label1.Enabled = True
Label2.Enabled = True
Label3.Enabled = True
Label4.Enabled = True
Label5.Enabled = True
Label6.Enabled = True
Label7.Enabled = True
Label8.Enabled = True
Label9.Enabled = True
Label10.Enabled = True
Label11.Enabled = True
Label12.Enabled = True
Label13.Enabled = True
Label14.Enabled = True
Label15.Enabled = True
Label16.Enabled = True

Label1.BackColor = Color.Silver
Label2.BackColor = Color.Silver
Label3.BackColor = Color.Silver
Label4.BackColor = Color.Silver
Label5.BackColor = Color.Silver
Label6.BackColor = Color.Silver
Label7.BackColor = Color.Silver
Label8.BackColor = Color.Silver
Label9.BackColor = Color.Silver
Label10.BackColor = Color.Silver
Label11.BackColor = Color.Silver
Label12.BackColor = Color.Silver
Label13.BackColor = Color.Silver
Label14.BackColor = Color.Silver
Label15.BackColor = Color.Silver
Label16.BackColor = Color.Silver
selectionCounter = 0
Call ShuffleWords()

End Sub

Private Sub TestForMatch(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.Click, _
Label2.Click, Label3.Click, Label4.Click, Label5.Click, Label6.Click, Label7.Click, _
Label8.Click, Label9.Click, Label10.Click, Label11.Click, Label12.Click, Label13.Click, _
Label14.Click, Label15.Click, Label16.Click
' displays the appropriate words, and determines whether
' the user selected a matching pair of words
Dim labelNum As String
Dim index1 As Integer
Dim index2 As Integer

' update the selection counter

selectionCounter = selectionCounter + 1

' determine whether this is the first or second selection
If selectionCounter = 1 Then
' if this is the first label, extract the number from
' the label's name, then use the number to display the
' appropriate word from the list box
firstLabel = TryCast(sender, Label)
labelNum = firstLabel.Name.Substring(5)
index1 = Convert.ToInt32(labelNum) - 1
firstLabel.Text = wordListBox.Items(index1).ToString

Else
' this is the second label, so diable the game board,
' then extract the number from the labels name, then
' use the number to display the appropriate word from
' the list box
boardTableLayoutPanel.Enabled = False
secondLabel = TryCast(sender, Label)
labelNum = secondLabel.Name.Substring(5)
index2 = Convert.ToInt32(labelNum) - 1
secondLabel.Text = wordListBox.Items(index2).ToString

' if both words match, disable corresponding
' label controls, then turn on matchTimer;
' otherwise, turn on the matchTimer
If firstLabel.Text = secondLabel.Text Then
firstLabel.Enabled = False
secondLabel.Enabled = False
matchTimer.Enabled = True
Else
noMatchTimer.Enabled = True
End If
If firstLabel.Text = secondLabel.Text Then
firstLabel.BackColor = Color.LightGreen
secondLabel.BackColor = Color.LightGreen
Else : firstLabel.BackColor = Color.Red
secondLabel.BackColor = Color.Red
End If

' reset the selection counter
selectionCounter = 0
End If

' Label1.BackColor = Color.Blue

End Sub
Private Sub matchTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles matchTimer.Tick
' when the two words match, the game board is enabled
' and the timer is turned off
boardTableLayoutPanel.Enabled = True
matchTimer.Enabled = False
End Sub
Private Sub noMatchTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles noMatchTimer.Tick
' when the words do not match, the words are
' removed from the labels, the game board is enabled,
' and the timer is turned off
firstLabel.Text = String.Empty
secondLabel.Text = String.Empty
boardTableLayoutPanel.Enabled = True
noMatchTimer.Enabled = False
End Sub
End Class


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Intro To VB
19 Jul, 2008 - 11:41 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:02AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month