-1

I have a problem.

I need to add a MouseDown event to my own class (Unidad.cs) but I don't know how to do it. I have a class that extends from pictureBox and I need a MouseDown and MouseMove event to move the pictureBox, how can I do it? And... It goes always to the exact pictureBox? I mean, I will have 20 (for example) "Unidad" objects and I want to move only the Unidad object that is clicked with the mouse.

My class:

public class Unidad : PictureBox
{
    //Constructor
    public Unidad(string nombre, string tipo, int movimiento, int ha, int hp, int fuerza, int resistencia, int heridas, int iniciativa, int ataques, int liderazgo, int coste, string rutaImagen)
    {
        tipoUnidad = tipo;
        movimientoUnidad = movimiento;
        nombreUnidad = nombre;
        costeUnidad = coste;
        haUnidad = ha;
        hpUnidad = hp;
        fuerzaUnidad = fuerza;
        resistenciaUnidad = resistencia;
        iniciativaUnidad = iniciativa;
        ataquesUnidad = ataques;
        liderazgoUnidad = liderazgo;

        object O = Resources.ResourceManager.GetObject(rutaImagen); 
        dibujoUnidad = new PictureBox();
        dibujoUnidad.SizeMode = PictureBoxSizeMode.StretchImage;
        dibujoUnidad.ClientSize = new Size(145, 67);
        dibujoUnidad.Image = (Image)O;
    }

    //Propiedades
    public string nombreUnidad { get; set; }
    public string tipoUnidad { get; set; }
    public int movimientoUnidad { get; set; }
    public int costeUnidad { get; set; }
    public int haUnidad { get; set; }
    public int hpUnidad { get; set; }
    public int fuerzaUnidad { get; set; }
    public int resistenciaUnidad { get; set; }
    public int heridasUnidad { get; set; }
    public int iniciativaUnidad { get; set; }
    public int ataquesUnidad { get; set; }
    public int liderazgoUnidad { get; set; }
    public PictureBox dibujoUnidad { get; set; }

    //Método para dibujar unidad
    public void Colocar(Point p, Control control, Image imagen)
    {
        using (Graphics g = control.CreateGraphics())
        {
            using (Pen pen = new Pen(Color.Red, 2))
            {
                g.DrawImage(imagen, p);
            }
        }
    }
}

Thanks!

EDIT 1:

I can do it with that at the end of my "Unidad.cs":

public void Colocar(Control control, Unidad unidad, Point p)//(Point p, Control control, Image imagen)
    {
                control.Controls.Add(unidad);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        //Do Stuff here
        base.OnMouseDown(e);
        MessageBox.Show("HOLA");
    }

The problem that I have now is that I don't have the image, I saw a black rectangle, or grey rectangle if I don't use the BackColor command, that when I click on it, it shows me a MessageBox, but I can't put the correct image or the correct location. Anyone can help me?

Imrik
  • 674
  • 2
  • 14
  • 32

3 Answers3

0

First of all, you need to create a method which will be the event handler. It should be like this:

dibujoUnidad.MouseDown.MouseDown += new MouseEventHandler(mouseDownEvent_method);

Hope it helps!

mindOfAi
  • 4,412
  • 2
  • 16
  • 27
0

Try this:

public class Unidad {
    public event EventHandler MouseDown;

    public void someMethode()
    {
        // Raise the event
        OnMouseDown(EventArgs.Empty);
    }

    protected void OnMouseDown(EventArgs e)
    {
        if (MouseDown != null)
            MouseDown(this, e);
    }
}

You have to do the same with MouseUp

You can also use your own EventArgs Object. Do take that, use this line: public event EventHandler<MyEventArgs> MouseDown

Make sure your MyEventArgs class is derived from EventArgs.

TheBrain
  • 685
  • 6
  • 26
0

You are inheriting from Picturebox so you can override the Mouse events

 public class Unidad : PictureBox
    {
        protected override void OnMouseDown(MouseEventArgs e)
        {
            //Do Stuff here
            base.OnMouseDown(e);
        }
    }

I added a Panel to my form, "Form1" called "picContainer"

public Form1()
{
      InitializeComponent();  

      picContainer.Controls.Add(new MyPictureClass()
      {
            Image = imageList1.Images[0],
      }
     );
}

I set the image from the image container.

Wheels73
  • 2,850
  • 1
  • 11
  • 20
  • Your right, I didn't see the derivation from PictureBox. – TheBrain Mar 03 '17 at 09:35
  • I try but it doesn't work. I copy your code on my class and I put a MessageBox.Show("OK"); after you base.OnMouseDown but it doesn't show the message box. Can you help me a little bit more, please? Thanks! – Imrik Mar 03 '17 at 11:54
  • Are you able to see the boundaries of the picture box? I couldn't see my message box at first so had to click around on my form until I actually clicked in the boundaries itself . It works locally for me. – Wheels73 Mar 03 '17 at 11:59
  • No, it doesn't show the messagebox. Also I have a new problem that could concern our problem. I minimize my form and when I maximize, the picturebox disappears. – Imrik Mar 03 '17 at 12:05
  • When you instance Unidad. try setting the back colour to black so you can see it. new Unidad(){BackColor = Color.Black}); If you can't see the box then thats your first issue to sort. – Wheels73 Mar 03 '17 at 12:12
  • I use that; Unidad unidadLancerosImp = new Unidad("Lanceros", "I", 10, 4, 1, 4, 5, 1, 6, 1, 7, 12, "infanteriaImp") { BackColor = Color.Black }; and it doesn't show anything black, why not? Can you help me? – Imrik Mar 03 '17 at 12:16
  • I've edited the answer... but it could be a container issue... i.e where you are adding your controls too... maybe start off with a test form and do what I did. – Wheels73 Mar 03 '17 at 12:23
  • it works now! it shows the messageBox!!! The new problem I have is that I can't give him a location or see the image, but I see a grey rectangle and when I click on it it shows the message box, I put my code on the question, can you see it, please? – Imrik Mar 03 '17 at 13:20
  • I add the code on my question now, the picturebox is black, not grey. It changes to grey if I don't use the "BackColor". – Imrik Mar 03 '17 at 13:24
  • Add an image list to your form. Import all the images you need in there and then add them to your picture box. Answer edited. – Wheels73 Mar 03 '17 at 13:41
  • Morning... that's good news...pls feel free to upvote the answer... :) – Wheels73 Mar 06 '17 at 09:38