Answered by:
Paint different pictureboxes

Question
-
I have 3 pictureboxes. pb1, pb2 & pb3. Pb2 is my primary picturebox and double the size of the other. On these picture boxes I will draw a line based on some values. If value is less than 0 pb1 should be drawn upon and if the value is greater than 124 pb3 should be used.
I have then made this code if(value <0)
{
pb1.paint += new PaintEventHandler(pb1_paint)
}
In the paint event I draw the line.
When running the project no line will be drawn on the picturebox, however, if I break the line will be drawn. What do I do wrong?- Moved by Rudedog2 Wednesday, March 10, 2010 2:50 PM winforms issue (From:Visual C# General)
Wednesday, March 10, 2010 10:10 AM
Answers
-
So I suggest following
Form1_Load() { pb1.Paint += new PaintEventHandler(pb1_Paint); pb2.Paint += new PaintEventHandler(pb2_Paint); } pb1_Paint { if(value<0) { pb2.Invalidate(); } else { g.DrawLine(Pens.Black, 0, pb1.ClientSize.Height / 2, 100, pb1.ClientSize.Height / 2); } } pb2_Paint { if(value>=0) { g.DrawLine(Pens.Black, 0, pb2.ClientSize.Height / 2, 100, pb2.ClientSize.Height / 2); } }
My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010Wednesday, March 10, 2010 6:42 PM
All replies
-
Because value>=0, I guess.
Your code will run only once. so when program reach that line, if value<0 then it will register event handler elsewhere it wont at all, even if you change the value to less than zero.
So I suggest following instead:yourForm_Load() { //ALWAYS REGISTER THE HANDLER pb1.paint += new PaintEventHandler(pb1_paint); } pb1_paint() { //CHECK IF TO DRAW THAT LINE HERE INSTEAD if(value<0) { //TO DO: DRAW THAT LINE } }
My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010Wednesday, March 10, 2010 11:19 AM -
Form1_Load() { pb1.Paint += new PaintEventHandler(pb1_Paint); } pb1_Paint { if(value<0) { pb2.paint += new PaintEventHandler(pb2_paint);
I will just write my code, as I have two paint event handlers like this.
If I just use g.drawline..... It will draw in the currenct picturebox.
} else { g.DrawLine(Pens.Black, 0, pb1.ClientSize.Height / 2, 100, pb1.ClientSize.Height / 2); }Wednesday, March 10, 2010 2:20 PM -
So I suggest following
Form1_Load() { pb1.Paint += new PaintEventHandler(pb1_Paint); pb2.Paint += new PaintEventHandler(pb2_Paint); } pb1_Paint { if(value<0) { pb2.Invalidate(); } else { g.DrawLine(Pens.Black, 0, pb1.ClientSize.Height / 2, 100, pb1.ClientSize.Height / 2); } } pb2_Paint { if(value>=0) { g.DrawLine(Pens.Black, 0, pb2.ClientSize.Height / 2, 100, pb2.ClientSize.Height / 2); } }
My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010Wednesday, March 10, 2010 6:42 PM -
Hello Scorgi,
I prefer Yasser’s solution. It is correct to tell PictureBox itself (pb2) draw the item instead of hand pb1.Paint to draw something on pb2. The recommended way is let each pictureBox draw element according to the value.
Sincerely,
Kira Qian
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!Thursday, March 11, 2010 6:31 AM -
Thank you very much!!! Worked perfectly!Thursday, March 11, 2010 8:18 AM
-
You're welcome.
Thank you for your happiness :)
Have a good time!
My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010Thursday, March 11, 2010 8:40 AM