Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
In my program I have a List of picturebox and during the program when I push a button a new picturebox will be created. But I want to draw 2 lines between two selected pictureboxes by MouseDown and MouseUp but no line will be drawn.
Here is my code:
public partial class Form1 : Form
        int x1, y1, x2, y2, x3, y3, x4, y4;
        List<PictureBox> pics = new List<PictureBox>();
        PictureBox thisPB;
        int i = 0;
        int j;
        void drawlines(int x1, int y1, int x2, int y2)
            Graphics g = this.CreateGraphics();
            Pen p = new Pen(Color.Black,2);
            g.DrawLine(p, x1, y1, x1, y2);
            g.DrawLine(p, x1, y2, x2, y2);
        private void button1_Click(object sender, EventArgs e)
            pics.Add(new PictureBox());
            pics[i].Name = "pic" + i.ToString();
            pics[i].Left = 200 * i;
            pics[i].Top = 100 * i;
            pics[i].Height = 100;
            pics[i].Width = 100;
            pics[i].SizeMode = PictureBoxSizeMode.StretchImage;
            pics[i].Image = (Image)Properties.Resources.Tulips;
            //pics[i].MouseMove += new MouseEventHandler(pic_MouseMove);
            pics[i].MouseDown += new MouseEventHandler(pic_MouseDown);
            pics[i].MouseUp+=new MouseEventHandler(pic_MouseUp);

        void pic_MouseMove(object sender, MouseEventArgs e)
            if (e.Button == MouseButtons.Left)
                PictureBox thisPB = (PictureBox)sender;
                thisPB.Left = e.X;
                thisPB.Top = e.Y;

        void pic_MouseDown(object sender, MouseEventArgs e) 
            x3 = e.X;
            y3 = e.Y;
            //PictureBox thisPB = pics.Find(p => p.Name == pics[pics.IndexOf(p)].Name);
            thisPB = (PictureBox)sender;
            x1 = (thisPB.Right - thisPB.Left) / 2;
            y1 = (thisPB.Top - thisPB.Bottom) / 2;

        void pic_MouseUp(object sender, MouseEventArgs e)
            x4 = e.X;
            y4 = e.Y;
            thisPB = (PictureBox)sender;
            //PictureBox thisPB = pics.Find(p => p.Name == pics[pics.IndexOf(p)].Name);
            //if (x4 < thisPB.Right)
            x2 = (thisPB.Right - thisPB.Left) / 2;
            y2 = (thisPB.Top - thisPB.Bottom) / 2;
            drawlines(x1, y1, x4, y4);

How can I choose a picture to be selected for drawing line?

It's wonderful how many different people come to the same idea again and again. I'm pretty much sure: this is possible, but this is a really a dead-end approach, making no practical sense. Picture boxes won't help you at all.

Please see my past answer: Drawing Lines between mdi child forms[^].

Doesn't it make sense?

Please see also these answers:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my application?[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

And, for more remote alternative, also consider WPF:
Connect different controls[^] (again, the inquirer came up with the idea to connect controls),
Vector graphics software like CorelDraw or Inkscape in C# or VB.NET[^],
Architecture diagram tips[^].

Good luck,
Share this answer
[no name] 13-Jun-13 12:46pm    
Well... I did test his code and it works. After providing the proper coordinates, two lines are drawn without any problem.
Sergey Alexandrovich Kryukov 13-Jun-13 12:57pm    
Of course, in this particular sample. I admitted in the very beginning that this is possible. So what? This is not yet a product. For a whole project, supportable and more or less complicated, this is a dead end. And, if you consider more reasonable alternatives, you would probably see it does not make practical sense.
[no name] 13-Jun-13 13:01pm    
Sergey, I have to accept your expert opinion. I don't know too much about the matter and haven't analysed alternatives. A priori, I would rely on draw events rather than on other thing. But as far as apparently he wants it done in this way, I thought I should defend his position.
Sergey Alexandrovich Kryukov 13-Jun-13 13:04pm    
It's just about pro and contra, not about theoretical technical possibility of doing this or that. It's just implication for development process and qualities of the project. Please see my comments below.
[no name] 13-Jun-13 13:10pm    
OK, OK. As said, I am a poor ignorant (on this specific matter) and just wanted to help a bit to correct a simple problem, without looking at the big picture. Perhaps I should have let this question for more experienced developers on this who might see beyond the small errors and give a solid and really-helpful feedback. I will intend to not make the same error again. I am new in all this thing of participating in online programmers' networks :)
Your code seems to be working fine except for one small detail: the coordinates of the picturebox are wrongly calculated. There is a simple solution for that:
x1 or x2 = pic.Left;
y1 or y2 = pic.Top;
Share this answer
bahman01 13-Jun-13 12:52pm    
My problem is how to select picturebox by mousedown and mouseup not to calculate its left and top. If you use this code and create 3 pictures you will understand what is wrong with my code.

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