Hi all,
I'm new to C# and I'm facing this very annoying problem.. I can't seem to update the text of a label, and when I change the label to a textbox, it only updates it once. I'm now using a textbox, and when I click once, it changes the text. But clicking more than once won't change it anymore. This is my current code:
using System.Windows.Forms;
using System.Drawing;
using System;
class ReversiForm : Form
{
private TextBox label_zet;
public int [,] playground = new int[6,6];
int x = 0;
int y = 0;
int turn = 1;
public ReversiForm()
{
this.Text = "Reversi";
this.BackColor = Color.White;
this.Size = new Size(600, 600);
this.Paint += InitializeComponent;
this.MouseClick += this.mouseClick;
}
private void InitializeComponent(object o, PaintEventArgs pea)
{
for (int i = 0; i < 7; i++)
{
pea.Graphics.DrawLine(Pens.Black, 20 + 50 * i, 200, 20 + 50 * i, 500);
pea.Graphics.DrawLine(Pens.Black, 20, 200 + 50 * i, 320, 200 + 50 * i);
}
this.label_zet = new TextBox();
this.label_zet.Location = new Point(20, 20);
this.label_zet.Name = "label";
this.label_zet.Size = new Size(120, 20);
this.label_zet.Text = "Player " + turn + " can go now";
this.Controls.Add(this.label_zet);
// Stones
pea.Graphics.FillEllipse(Brushes.Red, 100, 80, 50, 50);
pea.Graphics.FillEllipse(Brushes.Blue, 100, 140, 50, 50);
// Draw stones
for (int i = 0; i < 6; i++)
{
for (int t = 0; t < 6; t++)
{
if (playground[i, t] == 1)
{
pea.Graphics.FillEllipse(Brushes.Red, 20 + 50 * i, 200 + 50 * t, 50, 50);
}
if (playground[i, t] == 2)
{
pea.Graphics.FillEllipse(Brushes.Blue, 20 + 50 * i, 200 + 50 * t, 50, 50);
}
}
}
}
public void mouseClick(object o, MouseEventArgs mea)
{
x = mea.X;
y = mea.Y;
if (x > 20 && x < 320 && y > 200 && y < 500)
{
for (int i = 0; i < 6; i++)
{
if (x > 20 + 50 * i && x < 20 + 50 * (i + 1))
{
for (int t = 0; t < 6; t++)
{
if (y > 200 + 50 * t && y < 200 + 50 * (t + 1))
{
playground[i, t] = turn;
if (turn == 1) turn = 2;
else turn = 1;
this.label_zet.Text = "Player " + turn + " can go now";
Invalidate();
}
}
}
}
}
}
}
class Reversi
{
static void Main()
{
ReversiForm scherm;
scherm = new ReversiForm();
Application.Run(scherm);
}
}
Changing the textbox into a label will cause the label to not be changed no matter what, not even once. I tried creating a new blank windows forms program with only a label change on the mouseclick event, but this didn't work either. How the hell do you change a label's text? And why does the above code not work properly??
Thanks in advance!
