Hello,

Ok. How to describe my problem :-/

I'm making a 2D drawing program. Currently working on lines.
So I got a class line which holds the draw method.
And I've created a list of lines + a line object.
In the form I have a listbox where every line is added.
And when I doubleclick on an item in the listbox I want the program to draw the clicked line on pictureBox1.

What happends now is that I am able to draw lines, and each line is added to the lines list and listBox1. But nothing happends when I doubleclick an item in listBox1.

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDownPoint = new Point(e.X, e.Y);

            listBox1.SelectedIndex = -1;
        }

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel3.Text = "X: " + e.X + " Y: " + e.Y;

            if (e.Button == MouseButtons.Left)
            {
                if (drawLineBtn.Checked)
                {
                    line.P1 = mouseDownPoint;
                    line.P2 = new Point(e.X, e.Y);
                    pictureBox1.Invalidate();
                }
            }
        }

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (drawLineBtn.Checked)
            {
                listBox1.Items.Add(line);
                lines.Add(line);
            }
        }

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                line.Draw(e.Graphics);

            else if (listBox1.SelectedIndex != -1)
                lines[listBox1.SelectedIndex].Draw(e.Graphics);
        }

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            pictureBox1.Invalidate();
        }

What do you expect to happen, except an Invalidate of a pictureBox?

Hm. I thought that would call the pictureBox_paint method.

Tried modifying a little bit. Still nothing.

Graphics graphics = pictureBox1.CreateGraphics();
    
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            graphics.Clear(Color.White);
            lines[listBox1.SelectedIndex].Draw(graphics);
        }

Invalidate will call Paint. Could you specify what you mean by "Nothing happens" (after doubleclick that is)
Did you set a breakpoint to see if you get in the painteventhandler?

Back to before I modified it, Yes I did get in the painteventhandler. Put breakpoint at line 38.
What I expect (or want) to happen is that the selected object in the listbox will be redrawn on the picturebox.

So it must be something in the draw method of your line class, I think.
Could you show some code?

Sure, here is some of the code from the Line class.
Thanks for taking your time to looking into this btw :)

class Line
    {
        private Point p1 = Point.Empty;
        private Point p2 = Point.Empty;

        private Pen linePen = new Pen(Color.Black);

        public void Draw(System.Drawing.Graphics g)
        {
            g.DrawLine(linePen, p1, p2);
        }

        public Point P1
        {
            get { return p1; }
            set { p1 = value; }
        }

        public Point P2
        {
            get { return p2; }
            set { p2 = value; }
        }

Should you not do a new line() in your mouseDown?

Hehey!! Thank you very much! :D Now it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.