Im making a TickTackToe game in Visual Studio 2010, C# / .NET -
Has anyone got an idea as to what I can write to make the form resize based on the selected grid size... button size is 50px / space between buttons is 10px... so form needs to resize 60px for every button created .Left and .Top
The form loads at 400 x 400, and I can get it to resize but it either goes smaller or massive, taking over my 2 screens and can't seem to be able to tweek it just right! Also, Ive put the code inside the button's 'for' loop, thinking I want it to grow with the buttons..!! although thinking about it.. its running that code every time it goes through the loop humm, is this right? or should it be outside the 'for' loop?
Another problem is that when I get it to resize, the groupBox doen't resize, so hiding the buttons anyway!
The other thing was: Where I am selecting a button Image when ButtonClicked, it is not showing the second image on the second clickedButton, but when using clickedButton.Text ("X" "O") it worked fine! The buttons start with no image so is the 'null' value ive used correct? previously it was clickedButton.Text == " ";
Here's the code, problem areas marked as: ?????? :-S
Thank u
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace TickTackToe
{
public partial class frmTickTackToe : Form
{
public frmTickTackToe()
{
InitializeComponent();
}
private void frmTickTackToe_Load(object sender, EventArgs e)
{
cboGridWidth.SelectedIndex = 0;
}
private void btnStart_Click(object sender, EventArgs e)
{
int grid1 = int.Parse(cboGridWidth.Text);
//int grid2 = int.Parse(cboGridHeight.Text);
Button[,] tickTackButton = new Button[grid1, grid1];
for (int i = 0; i < grid1; i++)
{
for (int x = 0; x < grid1; x++)
{
tickTackButton[i, x] = new Button();
tickTackButton[i, x].Name = "tickTackButton" + i + ", " + x;
tickTackButton[i, x].Width = 50;
tickTackButton[i, x].Height = 50;
tickTackButton[i, x].Left = tickTackButton[i, x].Left + tickTackButton[i, x].Width + (i * 60);
tickTackButton[i, x].Top = tickTackButton[i, x].Top + tickTackButton[i, x].Top + 50 + (x * 60);
tickTackButton[i, x].Text = " ";
gbxButtons.Controls.Add(tickTackButton[i, x]);
tickTackButton[i, x].Click += new EventHandler(Button_Click);
frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(400, 400);
frmTickTackToe.ActiveForm.Width = frmTickTackToe.ActiveForm.Width; // ????? :-S
frmTickTackToe.ActiveForm.Height = frmTickTackToe.ActiveForm.Height; /// ????? :-S
}
}
}
private void Button_Click(object sender, EventArgs e)
{
SoundPlayer[] sounds = new SoundPlayer[3]; // created soundplayer array of 3 .wav sounds.
Random random = new Random(); // creates a new random to play sounds randomly.
sounds[0] = new SoundPlayer(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\1.wav");
sounds[1] = new SoundPlayer(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\2.wav");
sounds[2] = new SoundPlayer(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\3.wav");
sounds[random.Next(0, 3)].Play(); //
Button clickedButton = (Button)sender;
string index = clickedButton.Name.Substring("tickTackButton".Length);
string targetTextBox = "tickTackButton" + index;
int myIndex = gbxButtons.Controls.IndexOfKey(targetTextBox);
Image[] playerImage = new Image[2];
playerImage[0] = Image.FromFile(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\A.png");
playerImage[1] = Image.FromFile(@"C:\Users\ROB\Desktop\OO Programing\TickTackToe\TickTackToe\bin\Debug\sounds\B.png");
if (myIndex != -1)
{
//MessageBox.Show(index);
if (clickedButton.Image == null) // ?????? :-S
{
clickedButton.Image = playerImage[0];
}
if (clickedButton.Image == playerImage[0])
{
clickedButton.Image = playerImage[1];
}
if (clickedButton.Image == playerImage[1])
{
clickedButton.Image = playerImage[0];
}
}
}
private void btnNewGame_Click(object sender, EventArgs e)
{
gbxButtons.Controls.Clear();
frmTickTackToe.ActiveForm.Size = new System.Drawing.Size(400, 400);
}
}
}