我的对话框.c#中显示代码不工作.没有错误,但有些地方出了问题

本文关键字:问题 方出 有错误 对话框 显示 代码 工作 我的 | 更新日期: 2023-09-27 18:10:48

所以我最近一直在尝试学习c#,所以我想我会尝试我简单的项目,如井字棋。我目前正试图添加点击功能,以确保它的工作,我把一个消息框。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;
        }
    }
}

然后在我的form .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);
        }
    }
}

我的对话框.c#中显示代码不工作.没有错误,但有些地方出了问题

根据事件处理程序(Form1_Click)的名称,我怀疑您已经将单击事件处理程序连接到表单的单击事件而不是panel1的单击事件。注意,如果用户单击表单中的面板,那么只有触发面板的单击事件,而不是表单的。

您可能没有注册事件。尝试在构造函数中注册它:

    public Form1()
    {
        InitializeComponent();
        //Register click event with handler
        this.Click += new EventHandler(Form1_Click);
    }

唯一有意义的解释是Form1_Click没有运行。如果它被执行,那么detectHit肯定会运行。MessageBox.Show肯定会被称为。没有执行分支可以避免显示MessageBox.Show。在Form1_Click执行的情况下,MessageBox.Show不被调用的唯一可能的方法是引发异常。在这种情况下,你会注意到。

事件处理程序根本没有连接。或者它连接到窗体的Click事件,但是您点击的是面板而不是窗体。