dimanche 29 mars 2015

Programming a module to handle multiple button clicks vb

I'm programming a vb form to allow the user to play tic-tac-toe using a form. I've set up a table (just to make it look like an actual game) and have put buttons in this table. I'm new to programming so excuse me if this is a really easy fix but I wanted to produce a solution in the simplest way possible. Therefore, I used a module which I want to handle each button click instead of writing out code for each button.


Code:



Private Sub Btn_Box6_Click(sender As Object, e As EventArgs) Handles Btn_Box6.Click
ChangeTxt()
End Sub


(Example of one of the button click conditions )



Sub ChangeTxt()
If Form1.Active_PlayerTxt.Text = "X" Then
PlayerOne = True
ElseIf Form1.Active_PlayerTxt.Text = "O" Then
PlayerTwo = True
Else
MsgBox("Please enter X or O to decide the player!")
End If
If PlayerOne = True Then
Form1.Btn_Box1.Text = "X"
ElseIf PlayerTwo = True Then
Form1.Btn_Box1.Text = "O"
Else
MsgBox("Please enter X or O to decide the player!")
End If
Form1.Active_PlayerTxt.Text = ""
PlayerOne = False
PlayerTwo = False
End Sub


(That is the sub routine ) So basically as you can see, this currently only changes btn_box1, but I want it to change each separate button (btn_box2) when they are clicked instead of just changing btn_box1. Is there a easy way to do this?


Aucun commentaire:

Enregistrer un commentaire