0

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();
        }
2
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by Azurit
0

Hm. I thought that would call the pictureBox_paint method.

0

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);
        }
0

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?

0

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.

0

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

0

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; }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.