Click here to Skip to main content
15,920,110 members
Home / Discussions / Windows Forms
   

Windows Forms

 
GeneralRe: Adding Dropdown button to ToolStrip Pin
εїзεїзεїз17-Sep-09 13:10
εїзεїзεїз17-Sep-09 13:10 
GeneralRe: Adding Dropdown button to ToolStrip Pin
Henry Minute17-Sep-09 13:44
Henry Minute17-Sep-09 13:44 
GeneralRe: Adding Dropdown button to ToolStrip Pin
εїзεїзεїз17-Sep-09 23:40
εїзεїзεїз17-Sep-09 23:40 
QuestionUsing Reflection to access functionality from a web-hosted library. Pin
ccdsystems15-Sep-09 13:33
ccdsystems15-Sep-09 13:33 
AnswerRe: Using Reflection to access functionality from a web-hosted library. Pin
Dave Kreskowiak16-Sep-09 5:17
mveDave Kreskowiak16-Sep-09 5:17 
GeneralRe: Using Reflection to access functionality from a web-hosted library. Pin
ccdsystems16-Sep-09 7:44
ccdsystems16-Sep-09 7:44 
QuestionRetaining Drawn Graphics Pin
farzadmf15-Sep-09 12:23
farzadmf15-Sep-09 12:23 
AnswerRe: Retaining Drawn Graphics Pin
Luc Pattyn15-Sep-09 14:57
sitebuilderLuc Pattyn15-Sep-09 14:57 
Hi,

this is a common question, for which I keep a standard reply, here it goes:

there are several steps to correctly draw something; it does not matter how complex the paint job is: from a single line, to a complex drawing, or a real work of art.

To make sure it all becomes visible on the screen and gets repainted automatically when moving, resizing, minimizing/maximizing/restoring or uncovering your Form, one should follow these steps:

1.
decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. And I do not like PictureBoxes, they are pretty useless.

2.
create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. For a complex drawing, it could be a List of objects that derive of a common type, each having its own PaintMe() method.

3.
create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics object inside the PaintEventArgs, and your variables. Do not call CreateGraphics!

4.
if and when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation).

5.
If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer which ticks on the GUI thread, so you are allowed to call Invalidate() from there too.

BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them
alive in class members (hence create them only once); or create them inside the Paint
handler and don't forget to call Dispose() on them.

C# example:
private Panel panel;
private bool paintRectFlag=true;
private Rectangle rect=new Rectangle(20, 20, 300, 200);
private Pen rectPen=Pens.Black;

public Form1() {
    InitializeComponents();
    panel=new Panel();
    panel.Bounds=new Rectangle(…);
    panel.Paint+=panelPaintHandler;
    Controls.Add(panel);
}

protected void panelPaintHandler(object sender, PaintEventArgs e) {
    Graphics g=e.Graphics;
    if (paintRectFlag) g.DrawRectangle(rectPen, rect);
}

protected void buttonClickHandler(object sender, EventArgs e) {
    paintRectFlag=!paintRectFlag;   // toggle visible flag
    panel.Invalidate();
}


Smile | :)

Luc Pattyn

Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

Local announcement (Antwerp region): Lange Wapper? Neen!


QuestionRe: Retaining Drawn Graphics Pin
farzadmf16-Sep-09 0:23
farzadmf16-Sep-09 0:23 
AnswerRe: Retaining Drawn Graphics Pin
Luc Pattyn16-Sep-09 1:52
sitebuilderLuc Pattyn16-Sep-09 1:52 
GeneralRe: Retaining Drawn Graphics Pin
farzadmf16-Sep-09 12:50
farzadmf16-Sep-09 12:50 
Questionwindows service not working Pin
iiiii15-Sep-09 0:00
iiiii15-Sep-09 0:00 
AnswerRe: windows service not working Pin
Dave Kreskowiak16-Sep-09 5:10
mveDave Kreskowiak16-Sep-09 5:10 
GeneralRe: windows service not working Pin
iiiii16-Sep-09 18:47
iiiii16-Sep-09 18:47 
GeneralRe: windows service not working Pin
Dave Kreskowiak17-Sep-09 1:10
mveDave Kreskowiak17-Sep-09 1:10 
QuestionUrgent : How to improve performence ?? Pin
Tejaswini Prashant J14-Sep-09 20:11
Tejaswini Prashant J14-Sep-09 20:11 
AnswerRe: Urgent : How to improve performence ?? Pin
Luc Pattyn15-Sep-09 15:01
sitebuilderLuc Pattyn15-Sep-09 15:01 
AnswerRe: Urgent : How to improve performence ?? Pin
Eddy Vluggen16-Sep-09 4:34
professionalEddy Vluggen16-Sep-09 4:34 
QuestionUpdating PictureBox control every one minute in windows form Pin
Member 418166914-Sep-09 6:29
Member 418166914-Sep-09 6:29 
AnswerRe: Updating PictureBox control every one minute in windows form Pin
Richard MacCutchan14-Sep-09 8:56
mveRichard MacCutchan14-Sep-09 8:56 
GeneralRe: Updating PictureBox control every one minute in windows form Pin
Luc Pattyn15-Sep-09 15:02
sitebuilderLuc Pattyn15-Sep-09 15:02 
QuestionCan i change the shape of the button as its background image? Pin
CoderForEver13-Sep-09 1:10
CoderForEver13-Sep-09 1:10 
AnswerRe: Can i change the shape of the button as its background image? Pin
dan!sh 13-Sep-09 1:26
professional dan!sh 13-Sep-09 1:26 
GeneralRe: Can i change the shape of the button as its background image? Pin
CoderForEver14-Sep-09 22:06
CoderForEver14-Sep-09 22:06 
GeneralRe: Can i change the shape of the button as its background image? Pin
Henry Minute16-Sep-09 2:41
Henry Minute16-Sep-09 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.