Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 11th, 2012, 10:00 AM
helpme42 helpme42 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 15 helpme42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 23 m 8 sec
Reputation Power: 0
MsgBox

I'm writing a tic tac toe program and i have the board set up, and the game works for the majority. determining who wins. what i am trying to do right now is after a winner is determined, a message box pops up and says who ever wins. I would like the message box to contain two buttons. One button with the text, "Ok for new game" and the second button with the text, "Cancel to exit"

Code:


Public Class Form1

    Private turn As Integer = 1
    Private play() As String = {"O", "X"}
    Private board(2, 2) As String

    Private Structure arrayIndex
        Dim x As Integer
        Dim y As Integer
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For x As Integer = 1 To 9
            Dim b As New Button With { _
                .Width = 80, _
                .Height = 80, _
                .Text = "", _
                .Location = New Point(60 + (((x - 1) Mod 3) * 80), 60 + (((x - 1) \ 3) * 80)), _
                .Tag = New arrayIndex With {.x = (x - 1) Mod 3, .y = (x - 1) \ 3}}
            Me.Controls.Add(b)
            AddHandler b.Click, AddressOf buttons_click

        Next
        Me.SetClientSizeCore(360, 360)
    End Sub

    Private Sub buttons_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If DirectCast(sender, Button).Text <> "" Then Return
        DirectCast(sender, Button).Text = play(turn Mod 2)
        Dim index As arrayIndex = DirectCast(DirectCast(sender, Button).Tag, arrayIndex)
        board(index.x, index.y) = play(turn Mod 2)
        turn += 1
        winner()
    End Sub

    Private Sub winner()
        Dim rows(7) As String
        rows(0) = board(0, 0) & board(1, 0) & board(2, 0)
        rows(1) = board(0, 1) & board(1, 1) & board(2, 1)
        rows(2) = board(0, 2) & board(1, 2) & board(2, 2)
        rows(3) = board(0, 0) & board(0, 1) & board(0, 2)
        rows(4) = board(1, 0) & board(1, 1) & board(1, 2)
        rows(5) = board(2, 0) & board(2, 1) & board(2, 2)
        rows(6) = board(0, 0) & board(1, 1) & board(2, 2)
        rows(7) = board(2, 0) & board(1, 1) & board(0, 2)

        For x As Integer = 0 To 7
            If rows(x).Trim.Length = 3 AndAlso (rows(x)(0) = rows(x)(1) AndAlso rows(x)(0) = rows(x)(2)) Then
                MsgBox(rows(x)(0) & "'s win!")
                turn = 1
                ReDim board(2, 2)
                For Each ctrl As Control In Controls
                    ctrl.Text = ""
                Next
                Return
            End If
        Next

    End Sub
End Class

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Adding buttons with text

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap