Saturday, November 22, 2008

Guess Number Game in VB.net




frmGame Codes
Option Explicit On
Option Strict On

Public Class frmGuess

Dim RandomClass As New Random()
Dim tries As Integer = 0
Dim number As Integer
Dim max As Integer = 100
Dim min As Integer = 0

Private Sub frmGuess_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
number = RandomClass.Next(1, 100)
lbldisplay.Text = "I've a number between 1 and 100" & vbCrLf & _
"Can You Guess My Number" & vbCrLf & "Please Enter the First Guess"
End Sub


Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnCalculate.Click

If (CInt(txtEntry.Text)) = number Then
Me.BackColor = Color.Green
tries += 1
frmBegin.lblWinDisplay.Text = "You Win"
frmBegin.lblTries.Text = "Number of Tries= " & tries
btnCalculate.Text = ("&Try Again")
frmBegin.BackColor = Color.GreenYellow
Me.Close()
frmBegin.Show()

ElseIf (CInt(txtEntry.Text)) > (number) Then
Me.BackColor = Color.Red
tries += 1
lbldisplay.Text = "High" & vbCrLf & "Try Again"
max = CInt(txtEntry.Text)

ElseIf (CInt(txtEntry.Text)) < (number) Then Me.BackColor = Color.Blue tries += 1 lbldisplay.Text = "Low" & vbCrLf & "Try Again" min = CInt(txtEntry.Text) End If End Sub Private Sub txtEntry_Validated(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles txtEntry.Validated If CInt(txtEntry.Text) > 100 Then
MessageBox.Show("Enter a Number Below 100", "NOT ALLOWED")
End If

If CInt(txtEntry.Text) > max Then
MessageBox.Show("Number Higher than " & max & " is not allowed", _
"NUMBER IS HIGH")
End If

If CInt(txtEntry.Text) <>






No comments: