1

I have a Windows Forms application and need to be able to select objects on the form, in the same way that you select files on the Desktop by left-clicking and dragging over the files, like below:

enter image description here

I have the following code which I've written by myself, but it's terrible. It's not right. I'm not too worried about the "selection" part of it right now, I'm mainly concerned with how do I draw like this?

private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (mouseDown)
    {
        // TODO: Draw Rectangle (so you can select elements on the canvas).


        Graphics graphics = Graphics.FromHwnd(splitContainer1.Panel1.Handle);
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        Pen pen = new Pen(Color.SlateBlue, 0.5f);

        graphics.PageUnit = GraphicsUnit.Pixel;

        graphics.DrawRectangle(pen, e.X, e.Y, (e.Location.X + e.X) - lastCursorLocation.X, (e.Location.Y + e.Y) - lastCursorLocation.Y);
    }
}

Update

private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e) { // TODO: Draw Rectangle (so you can select elements on the canvas).

        Graphics graphics = Graphics.FromHwnd(splitContainer1.Panel1.Handle);
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

        Pen pen = new Pen(Color.SlateBlue, 0.5f);

        graphics.PageUnit = GraphicsUnit.Pixel;

        graphics.DrawRectangle(pen, 1, 1, 1, 1);

        Invalidate();
    }

After placing the code in the Paint event, and calling Invalidate(), nothing draws on the form at all. I'm obviously doing something wrong, but what?

0550
  • 132
  • 1
  • 12
  • Don't do that. Instead, handle the `Paint` event and redraw everything. – SLaks Feb 09 '14 at 13:56
  • @SLaks, Thank you, but I don't understand. If I handle the Paint event of the Form or Panel, how will it know when I want to drag, if there is no MouseDown/MouseMove events? – 0550 Feb 09 '14 at 13:58
  • Call `Invalidate()` to force a repaint. – SLaks Feb 09 '14 at 13:58
  • @SLaks also, How will I draw a Rectangle and control how it will move with the mouse when you do no have access to e.X or e.Location from within the panel1_Pain() method? – 0550 Feb 09 '14 at 14:01
  • Store the information you need in fields in the form. – SLaks Feb 09 '14 at 14:03
  • I've updated my question with code, I think that's what you were getting at, but nothing is drawing at all now. – 0550 Feb 09 '14 at 14:04
  • You need to do all of the drawing in the `Paint` event only. You should never create your own `Graphics`. (otherwise, everything you draw will vanish when it's repainted) – SLaks Feb 09 '14 at 14:06
  • 1
    Your screenshot is of a control you already have in the toolbox. It is a ListView, no effort is required at all to get it to select items like this, it is automatic. If you are not actually using a ListView then you need to be explicit about what an "item" on the panel looks like. It gets to be a lot harder if they are actually controls, that requires an overlay. Another window that displays the rectangle on top of the existing controls. I wrote one [here](http://social.msdn.microsoft.com/Forums/windows/en-US/3087655c-bd50-4408-9c55-dd179e442675/creating-mouse-drag-box?forum=winforms). – Hans Passant Feb 09 '14 at 14:07

0 Answers0