国际象棋表中缺少的元素 [英] missing element in chess table

查看:79
本文介绍了国际象棋表中缺少的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有国际象棋桌,现在我的元素正在按照规则移动.但是当我拖出规则时,我的按钮消失了...我该如何解决呢????
(红色按钮显示我可以在哪里放置元素)
例如,骑士现在按照规则移动(如果我不能越过红色按钮,就没有问题),但是当我通过红色地方时,如果我掉入那里而不是规则,我的元素就会崩溃

I have chess table and my elements are now moving according to rules .But when I drag out of rules my button is disappearing...How I can solve it ???
(red buttons are showing where can I go my elements)
for example knight is moving as rules now (if I can''t pass over the red buttons there is no problem)but when I pass red places and if I drop there as not rules my elements are disapperaing

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

推荐答案

根据您告诉我们的内容很难确定,但是尝试将按钮的Anchor属性设置为Bottom和右-默认为上"和左".然后,当您拉伸窗口时,相对于窗体的右下角,按钮将停留在相同的位置.

如果那不是您的工作,请向我们提供有关您的工作的更多信息!
It''s difficult to be sure based on the little you have told us, but try setting the Anchor property of the button to Bottom and Right - the default it Top and Left. Then when you stretch your window, the button will stay in the same place relative to the bottom right corner of your form.

If that isn''t what you are doing, please give us more information on what you are doing!


在(button.DoDragDrop .... statement)之后的mousedown事件中,我检查了按钮的背景色为红色,然后将按钮图像分配为null,否则将骑士图像分配给按钮图像.
这是代码:

In mousedown event after (button.DoDragDrop....statement) I checked if the button backcolor is red then I assigned the button image to null else I assigned the knight image to the button image.
here is the codes :

private void btn_MouseDown(object sender, MouseEventArgs e)
        {
            Button button = sender as Button;
            if (e.Button == MouseButtons.Left && button.Image != null)
            {
                button.DoDragDrop(button.Image, DragDropEffects.Move);
                if (button.BackColor == Color.Red)
                    button.Image = null;
                else
                    button.Image = pictureBox1.Image;
            }
        }



现在一切都可以了:)



And eveything is ok now :)


这篇关于国际象棋表中缺少的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆