0

我有国际象棋桌,我的元素现在按照规则移动。但是当我拖出规则时,我的按钮消失了......我该如何解决?(红色按钮显示我可以去哪里我的元素)例如骑士现在正在按照规则移动(如果我没有越过红色按钮没有问题)但是当我越过红色的地方并且如果我掉到那里骑士消失了,红色的地方变回了原来的颜色(没有更多的红色地方表明我的骑士可以去哪里)。我试图进行调试,但由于我是 C# 的新手并且调试我还没有解决问题。如果你启发我的方式,我会很高兴。我该如何解决?谢谢

        void btn_DragEnter(object sender, DragEventArgs e)
        {
        Button button = (Button)sender;
        e.Effect = DragDropEffects.Move;
        for (int x = 0; x <= 7; x++)
        {
        for (int y = 0; y <= 7; y++)
        {
        btn[x, y].Image = null;
        if ((x + y) % 2 == 0)
        btn[x, y].BackColor = Color.Black;
        else
        btn[x, y].BackColor = Color.White;
        }
        }
        }

        void btn_DragDrop(object sender, DragEventArgs e)
        {
        Button button = (Button)sender;
        button.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);

        int[] dizi = (int[])button.Tag;
        int x = dizi[0];
        int y = dizi[1];

        for (int a = 0; a <= 7; a++)
        {
        for (int b = 0; b <= 7; b++)
        {
        btn[a, b].AllowDrop = false;
        }
        }

        if ((x + 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x + 1 <= 7))
        {
        btn[x + 1, y + 2].BackColor = Color.Red;
        btn[x + 1, y + 2].AllowDrop = true;
        }
        if ((x + 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x + 1 <= 7))
        {
        btn[x + 1, y - 2].BackColor = Color.Red;
        btn[x + 1, y - 2].AllowDrop = true;
        }
        if ((x - 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x - 1 <= 7))
        {
        btn[x - 1, y + 2].BackColor = Color.Red;
        btn[x - 1, y + 2].AllowDrop = true;
        }
        if ((x - 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x - 1 <= 7))
        {
        btn[x - 1, y - 2].BackColor = Color.Red;
        btn[x - 1, y - 2].AllowDrop = true;
        }
        if ((x + 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x + 2 <= 7))
        {
        btn[x + 2, y + 1].BackColor = Color.Red;
        btn[x + 2, y + 1].AllowDrop = true;
        }
        if ((x + 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x + 2 <= 7))
        {
        btn[x + 2, y - 1].BackColor = Color.Red;
        btn[x + 2, y - 1].AllowDrop = true;
        }
        if ((x - 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x - 2 <= 7))
        {
        btn[x - 2, y + 1].BackColor = Color.Red;
        btn[x - 2, y + 1].AllowDrop = true;
        }
        if ((x - 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x - 2 <= 7))
        {
        btn[x - 2, y - 1].BackColor = Color.Red;
        btn[x - 2, y - 1].AllowDrop = true;
        }
        }
4

1 回答 1

0

DoDragDrop is called only when actually dropping the piece. Your logic for determining where the piece can be dropped should be run when starting the drag, in MouseDown or MouseMove, before the call to DoDragDrop. It also seems you are clearing the red buttons in the DragEnter function. That also should be done in the MouseDown/MouseMove function, after the call to DoDragDrop.

于 2012-07-03T14:41:01.017 回答