Click here to Skip to main content
15,897,518 members
Home / Discussions / C#
   

C#

 
AnswerRe: Processes (up to 4) accessing to "serial.log" file handling (with pre-tags) Pin
Blubbo24-Aug-09 9:42
Blubbo24-Aug-09 9:42 
GeneralRe: Processes (up to 4) accessing to "serial.log" file handling (with pre-tags) Pin
Mycroft Holmes24-Aug-09 14:17
professionalMycroft Holmes24-Aug-09 14:17 
QuestionSetup & dependency error for officectrl.ocx Pin
NarVish24-Aug-09 6:53
NarVish24-Aug-09 6:53 
AnswerRe: Setup & dependency error for officectrl.ocx Pin
Saksida Bojan24-Aug-09 7:19
Saksida Bojan24-Aug-09 7:19 
GeneralRe: Setup & dependency error for officectrl.ocx Pin
NarVish24-Aug-09 7:24
NarVish24-Aug-09 7:24 
GeneralRe: Setup & dependency error for officectrl.ocx Pin
Saksida Bojan24-Aug-09 9:15
Saksida Bojan24-Aug-09 9:15 
QuestionCalling Form Buttonclick event from another file Pin
RS.Ratheesh24-Aug-09 6:41
RS.Ratheesh24-Aug-09 6:41 
AnswerRe: Calling Form Buttonclick event from another file Pin
Luc Pattyn24-Aug-09 6:52
sitebuilderLuc Pattyn24-Aug-09 6:52 
ratheeshnair123 wrote:
getting error


That is zero information. Be specific if you want specific help.


ratheeshnair123 wrote:
Graphics mg = CreateGraphics();


Unless you are doing highly advanced stuff, this is bound to be wrong, unnecessary and a big waste.

Here is one of my standard texts, I suggest you read it carefully:


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 [Forum Guidelines] [My Articles]

The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.

QuestionVirtual and Override problem Pin
bonzaiholding24-Aug-09 5:17
bonzaiholding24-Aug-09 5:17 
GeneralRe: Virtual and Override problem Pin
harold aptroot24-Aug-09 5:22
harold aptroot24-Aug-09 5:22 
AnswerRe: Virtual and Override problem Pin
Luc Pattyn24-Aug-09 5:47
sitebuilderLuc Pattyn24-Aug-09 5:47 
Questionint x = new int() where this will be stored ?? Pin
jpk42024-Aug-09 4:28
jpk42024-Aug-09 4:28 
AnswerRe: int x = new int() where this will be stored ?? PinPopular
Luc Pattyn24-Aug-09 4:58
sitebuilderLuc Pattyn24-Aug-09 4:58 
GeneralRe: int x = new int() where this will be stored ?? Pin
Richard MacCutchan25-Aug-09 9:41
mveRichard MacCutchan25-Aug-09 9:41 
GeneralRe: int x = new int() where this will be stored ?? Pin
Luc Pattyn25-Aug-09 15:30
sitebuilderLuc Pattyn25-Aug-09 15:30 
GeneralRe: int x = new int() where this will be stored ?? Pin
Richard MacCutchan26-Aug-09 3:19
mveRichard MacCutchan26-Aug-09 3:19 
QuestionHow to embaded rss feeds in to html page which will be displayed in mozilla as well as IE browser Pin
biswa4724-Aug-09 3:52
biswa4724-Aug-09 3:52 
AnswerRe: How to embaded rss feeds in to html page which will be displayed in mozilla as well as IE browser Pin
Nagy Vilmos24-Aug-09 4:35
professionalNagy Vilmos24-Aug-09 4:35 
Questionemgucv face detection Count Pin
ChangSiJie24-Aug-09 3:28
ChangSiJie24-Aug-09 3:28 
AnswerRe: emgucv face detection Count PinPopular
Henry Minute24-Aug-09 3:40
Henry Minute24-Aug-09 3:40 
AnswerRe: emgucv face detection Count Pin
sheahpar8-Feb-10 6:17
sheahpar8-Feb-10 6:17 
QuestionConvert SQL data to XML data using nHibernate Pin
DJ24524-Aug-09 2:56
DJ24524-Aug-09 2:56 
AnswerRe: Convert SQL data to XML data using nHibernate Pin
Henry Minute24-Aug-09 3:25
Henry Minute24-Aug-09 3:25 
QuestionExcel + C# Pin
GrgBalden24-Aug-09 2:16
GrgBalden24-Aug-09 2:16 
QuestionWorkbookEvents_SheetChangeEventHandler excel delegate Pin
NarVish24-Aug-09 2:16
NarVish24-Aug-09 2:16 

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.