Click here to Skip to main content
16,020,974 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. I would like to know if there's a possibility to create instances of lines without using Paint Event Handler?

My problem is that I have a user control but when I'm using paint event, it did not show in my user control. Help me. Thanks! :)
Posted
Comments
Manfred Rudolf Bihy 29-Dec-10 21:32pm    
Please try to pose a question that stands a chance of getting an answer.
- What are you trying to accomplish?
- What have you tried sofar?
- What is the difficulty, error or malfunction your observing?
Please supply a more detailed question regarding your problem. Thank you!
yummy02 29-Dec-10 22:06pm    
I'm creating a user control. Inside user control is the panel where instances of picture boxes occurs. What I want is to create instances of lines that connects one picture box to another.

I'm done with instances of picture box and its location. I'm having a problem with paint event handler. It seems that it doesn't work with panel. I'm planning to draw a line in panel(connecting pbox), but it did not work. Now I wonder if paint does work on panel.

A Panel does indeed have a Paint event and I've used it several times in my projects. Have you tried putting a break point in the event to see if it is getting called at all?

How are you attempting to draw the lines? GDI? Other things to check are what coordinates you are using to draw lines to make sure they are relative to the correct parent control, etc.
 
Share this answer
 
Comments
yummy02 30-Dec-10 0:12am    
Thanks, I have figured out what's missing. Last time, I user "g = this.CreateGraphics();", now that I've tried "g = panel1.CreateGraphics();", it works. Now my problem is to create a line to corresponding pbox. I want to create a lines that will connect one pbox to another.
Dr.Walt Fair, PE 30-Dec-10 0:21am    
Which Paint event are you using? Normally you get the Panel's Graphic by using Graphics g = e.Graphics; where e is the PaintEvent parameter.
yummy02 30-Dec-10 0:45am    
my code:

System.Drawing.Graphics g;
g = panel1.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Red, 5);
g.DrawLine(myPen, pbox.Right, pbox.Bottom, 0, 0);

my pbox is the picturebox.
Sergey Alexandrovich Kryukov 30-Dec-10 1:33am    
Whoever vote this way, without motivation, this is dishonest, because the answer does not say anything wrong. What you expect to see in "perfect" answer does simply not count. If the answer in not informative enough, ask question. The level of the issues discussed is for newbies, it's very hard to explain everything...

I'll put 5 to counteract...
Dr.Walt Fair, PE 30-Dec-10 10:28am    
As mentioned, if your code is in the Paint method for your panel, then do not use the CreateGraphics, but use the Graphics method passed in the PaintEvent's 'e' parameter.

In addition, your DrawLine would draw a line from the bottom right corner of the pbox to the top left corner of the PANEL. Is that what you meant?
If you use panel paint event you do not need a picture box; you need to draw on the canvas of the panel. In this case, you should never use CreateGraphics. When you create a handler to the event, graphics is already created for you and passed in second parameter (event argument) to your handler. Walt Fair tried to explain this to you, but it looks you need more detail. Something like that:

C#
MyPanel.Paint += (sender, args) => {
    args.Graphics.DrawLine(...
    args.Graphics.DrawImage(...
};


Fair enough? ;)
 
Share this answer
 
Comments
Espen Harlinn 28-Feb-11 15:25pm    
Quite fair, my 5
Sergey Alexandrovich Kryukov 1-Mar-11 3:45am    
Thank you, Espen,
John also fights against picture box, not so successfully; some OP fall in cognitive dissonance :-)
--SA

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