Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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
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;
            }
        }
Posted
Updated 4-Jul-12 3:17am
v5
Comments
Tim Corey 3-Jul-12 9:41am    
No clue. We cannot see your code or look at what you are doing. You will need to share your code with us and give us a much more detailed explanation before we can do more than guess. For instance, what are you dragging? What is the button that is disappearing? Are you calculating the position of an item relative to the board in such a way as to confuse the application when the item isn't above the board?

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!
 
Share this answer
 
Comments
y.baris 3-Jul-12 9:57am    
thanks I edited question and half of problem is ok now
but how can I move in table without missing ?
y.baris 4-Jul-12 9:18am    
Thanks for trying to help OriginalGriff
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 :

C#
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 :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900