そこで最近C#を勉強しようと思っていたので、Tic-Tac-Toeなどの簡単なプロジェクトに挑戦してみようと思いました。現在、クリック機能を追加しようとしているので、それが機能していることを確認するために、MessageBox.Show に入れて、クリックした領域が新しいことを確認します。ただし、実行してもエラーは表示されませんでしたが、ボックスをクリックしても何も起こりませんでした。私のコードの何が問題なのか誰か知っていますか? MessageBox.Show コードの問題ですか、それとも何か他の問題ですか? これが私のコードです:
Board.cs ファイルには次のものがあります。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class Board
{
private Rectangle[,] slots = new Rectangle[3, 3];
private Holder[,] holders = new Holder[3, 3];
public const int X = 0;
public const int O = 1;
public const int B = 2;
public void initBoard()
{
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
slots[x, y] = new Rectangle(x * 167, y * 167, 167, 167);
holders[x, y] = new Holder();
holders[x, y].setValue(B);
holders[x, y].setLocation(new Point(x, y));
}
}
}
public void detectHit(Point loc)
{
int x = 0;
int y = 0;
if (loc.X < 167)
{
x = 0;
}
else if (loc.X > 167 && loc.X < 334)
{
x = 1;
}
else if (loc.X > 334)
{
x = 2;
}
if (loc.Y < 167)
{
y = 0;
}
else if (loc.Y > 167 && loc.Y < 334)
{
y = 1;
}
else if (loc.Y > 334 && loc.Y < 500)
{
y = 2;
}
MessageBox.Show(x.ToString() + ", " + y.ToString() + "/n/n" + loc.ToString());
}
}
class Holder
{
private Point location;
private int value = Board.B;
public void setLocation(Point p)
{
location = p;
}
public Point getLocation()
{
return location;
}
public void setValue(int i)
{
value = i;
}
public int getValue()
{
return value;
}
}
}
次に、 Form1.cs ファイルに次のものがあります。
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
GFX engine;
Board theBoard;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics toPass = panel1.CreateGraphics();
engine = new GFX(toPass);
theBoard = new Board();
theBoard.initBoard();
}
private void Form1_Click(object sender, EventArgs e)
{
Point mouse = Cursor.Position;
mouse = panel1.PointToClient(mouse);
theBoard.detectHit(mouse);
}
}
}