简体   繁体   中英

My MessageBox.Show Code In C# Isn't Working. There Are No Errors But Something Is Wrong

So I have recently been trying to learn C#, so I thought I'd try I simple project such as Tic-Tac-Toe. I am currently trying to add click functionality so to make sure it is working I put in a MessageBox.Show to make sure it new which area I was clicking. However, when I ran it no errors appeared and yet when I clicked on a box nothing happened. Does anyone know what is wrong with my code? Is it a problem with the MessageBox.Show code or with something else? Here is my code:

In a Board.cs file I have:

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;
        }
    }
}

Then in my Form1.cs file I have:

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);
        }
    }
}

Based on the name of your event handler (Form1_Click), I suspect you've got the click event handler hooked up to your form 's click event instead of panel1 's click event. Note that if a user clicks a panel inside your form then only the panel's click event will fire, not the form's.

You may not have the event registered. Try registering it in the constructor:

    public Form1()
    {
        InitializeComponent();

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

The only explanation that makes any sense is that Form1_Click is not running. If it was executed then detectHit would definitely run. And MessageBox.Show would definitely be called. There are no execution branches that avoid MessageBox.Show being shown. The only possible way for MessageBox.Show not to be called in the event of Form1_Click executing is for an exception to be raised. In which case you would have noticed that.

Either the event handler is not hooked up at all. Or it is hooked up to the form's Click event but you are clicking on the panel rather than the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM