Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hey,

I need to make a C# project wich allows you to play OXO using Drag and Drop. Understanding code shows what i have till now. i have 2 problems.

1. The array of pictureboxes keeps getting in front of the dragged object.
2. When I stop clicking the mousebutton (MouseUp eventhandler) it won't put my dragged object into a picturebox.

Someone knows an answer?

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            //PictureBox MyX = new PictureBox();
            //PictureBox MyY = new PictureBox();
            InitializeComponent();
                
                MyX.MouseDown +=new MouseEventHandler(MyX_MouseDown);
                MyX.MouseMove +=new MouseEventHandler(MyX_MouseMove);    

                Point pX = new Point(230, 316);
                Point pY = new Point(20, 316);
                Bitmap X = new Bitmap("X.png");
                Bitmap Y = new Bitmap("O.jpg");
                MyX.BackgroundImage = X;
                MyX.BackgroundImageLayout = ImageLayout.Stretch;
                MyY.BackgroundImage = Y;
                MyY.BackgroundImageLayout = ImageLayout.Stretch;
                
                //
                int kolommen = 4;
                int rijen = 4;
                int aantalElementen = (kolommen * rijen) + 1;
                PictureBox[] Boxes = new PictureBox[aantalElementen];
                int tellerIndex = 1;
                for (int tellerRij = 1; tellerRij <= rijen; tellerRij++)
                {
                    for (int tellerKol = 1; tellerKol <= kolommen; tellerKol++)
                    {
                        Boxes[tellerIndex] = new PictureBox();
                        Boxes[tellerIndex].Name = "Picturebox" + tellerIndex.ToString();
                        Point positie = new Point((-50 + (tellerKol * 70)), (-50 + (tellerRij * 70)));
                        Boxes[tellerIndex].Width = 60;
                        Boxes[tellerIndex].BorderStyle = BorderStyle.FixedSingle;
                        Boxes[tellerIndex].Height = 60;
                        Boxes[tellerIndex].Location = positie;
                        Boxes[tellerIndex].AllowDrop = true;
                        tellerIndex++;
                    }
                }
                
                MyX.BorderStyle = BorderStyle.Fixed3D;
                MyX.Height = 60;
                MyX.Width = 60;
                MyX.Image = X;
                MyX.SizeMode = PictureBoxSizeMode.StretchImage;
                MyY.BorderStyle = BorderStyle.Fixed3D;
                MyY.Height = 60;
                MyY.Width = 60;
                MyY.Image = Y;
                MyY.SizeMode = PictureBoxSizeMode.StretchImage;
                MyX.Location = pX;
                MyY.Location = pY;


                this.Controls.AddRange(Boxes);
                this.Controls.Add(MyX);
                this.Controls.Add(MyY);
        }
        bool isDragging = false;
        int CurrentX, CurrentY;

        private void MyX_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging == true)
            {
                MyX.Top = MyX.Top + (e.Y - CurrentY);
                MyX.Left = MyX.Left + (e.X - CurrentX);
            }
            
        }

        private void MyX_MouseDown(object sender, MouseEventArgs e)
        {
            isDragging = true;
            CurrentX = e.X;
            CurrentY = e.Y;
            
        }
        
    }
}

[edit]Code block inserted to preserve formatting, minor spelling - OriginalGriff[/edit]
Posted
Updated 11-Nov-10 4:52am
v2

a couple of notes...

you need to implement a MouseUp event as well. Right now, there's nothing to tell the app that dragging has stopped. That means that once you push the mouse button down the first time, isDragging will always be true.

You also probably want to consider not moving MyX or MyY (though why you named it MyY instead of MyO makes no sense). (Also as an FYI, with c, the naming conventions are that variables start with a lower case while types or classes start with uppercase.) But the way you're doing it, again, you need a MouseUp event to reset the position of the PictureBox that you are dragging.

If you want to make sure that MyX is being drawn on top of the other controls, try calling the method BringToFront as in MyX.BringToFront(); when you start the dragging.

You should really google "c# drag and drop". The way that you're doing it is fine, but the creators of c# have implemented built-in functions in the controls to handle the drag drop.
 
Share this answer
 
For the dragging you will need to change the Z order for the dragged item...

You need to check the "mouseDoneMoving" position against your controls array using Contains to get the index(control) back and do the modification manually to that controls properties.

Hope that was helpfull...:confused:
 
Share this answer
 
Comments
William Winner 6-Dec-10 15:18pm    
where in the world did you come up with a "mouseDoneMoving" position?
ely_bob 6-Dec-10 15:27pm    
.. the location where mouse up occours... basically ::=> Containter.Contains("mouseDoneMoving").AddImage(currentMovingImage)
William Winner 6-Dec-10 18:17pm    
fyi, it wasn't me that down-voted you...you were right...it just seemed like you were implying an inherent property that doesn't exist.
ely_bob 6-Dec-10 23:07pm    
Naw it's o.k., William, With some of these posts, it is a mere google-ing away and when thats the case i tend to be a little more vague with the intent of attempting to "educate" ....

... but that approach is lost on some i guess...

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