Click here to Skip to main content
13,446,281 members (32,294 online)
Rate this:
Please Sign up or sign in to 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?
Posted 13-Jun-13 4:19am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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,
varocarbas 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.
varocarbas 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.
varocarbas 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 :)
Sergey Alexandrovich Kryukov 13-Jun-13 13:49pm
No, your contribution makes sense. It demonstrates the source of the particular problem.
varocarbas 13-Jun-13 13:58pm
OK, thanks.
bahman01 13-Jun-13 13:37pm
Sergey Alexandrovich Kryukov 13-Jun-13 13:47pm
Sure. So, will you accept the answer formally (green button)?
bahman01 13-Jun-13 12:53pm
my problem is to select pictureboxes by mousedown and mouseup not to draw a line.
Sergey Alexandrovich Kryukov 13-Jun-13 13:00pm
Understood, so what? Your question still mention drawing lines between that, so you idea is basically the same. Don't do it.

However, this is not my goal to convince you; decide by yourself. I saw a number of attempts, but no a single successful project based on this idea.

I just warned you. You probably feel that you saved some work on using picture boxes, but you actually did not. What do they give you? Only some extra hassles. This is not the first problem you are going to face.
bahman01 13-Jun-13 13:34pm
I want to mousedown on a picturebox and mouseup on second picturebox for calculating x and y.
Thank you for taking time for my project.
bahman01 13-Jun-13 13:35pm
my problem is how to control the handlers.
varocarbas 13-Jun-13 13:02pm
Your code works fine (I mean... for what you want). I did pass the pointer over the new picture being created and the lines were generated.
bahman01 13-Jun-13 13:39pm
my problem is how to control the handlers.
varocarbas 13-Jun-13 13:51pm
What you mean with how to control the handlers? All the code inside the pic_MouseMove (I would use MouseEnter, MouseHover and MouseLeave but yours works too... you can create as many handlers as you wish by doing the same that you did for pic_MouseMove) function is triggered when the mouse moves over the given pictureBox. You can use them just as triggers of what you want when you want; don't need to use the arguments (at least, for what you are doing): "PictureBox thisPB = (PictureBox)sender" is a waste of code you can access the pictureBox directly, either via pics or via pic.
I think that you have too many not-so-clear ideas; why not following the advice of Sergey, doing everything step by step and understanding each single bit?
bahman01 13-Jun-13 14:01pm
In this code all of pictureboxes use the same function for mousedown and mouseup. That is my problem. I can not access the pictureboxe directly because all of pictures are defined by a List and when you push button a new picturebox will be created. Although all of pictureboxes in this List use the same method.
try this code now:
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, x2, y2);
varocarbas 13-Jun-13 14:06pm
Actually, when I firstly saw your code I said to me "what on the hell will he be doing with this array of pictureboxes without handlers". The problem, bahman01 is that the taken approach is not right. Sergey's advice makes every second more sense: take it easy and start step by step. Firstly understand exactly the best way to accomplish the most difficult part here (line drawing) and then do some research to keep clarifying all your doubts (like creating objects with handlers at runtime).
bahman01 13-Jun-13 14:18pm
Thank you for taking time on my code! This is a question that my boss asked me to do it.
Thank you anyway! I must do some research about runtime.
varocarbas 13-Jun-13 14:33pm
No problem. Good luck.
bahman01 13-Jun-13 14:03pm
I want to select the picturebox by mouseup in the List.
Espen Harlinn 13-Jun-13 17:41pm
Nice set of links :-D
Sergey Alexandrovich Kryukov 13-Jun-13 18:31pm
Thank you, Espen.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180314.2 | Last Updated 13 Jun 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100