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

C#

 
AnswerRe: need to understand once and for all.... Pin
Robert Rohde23-Sep-05 6:21
Robert Rohde23-Sep-05 6:21 
GeneralRe: need to understand once and for all.... Pin
...---...23-Sep-05 6:56
...---...23-Sep-05 6:56 
GeneralRe: need to understand once and for all.... Pin
Dan Neely23-Sep-05 7:05
Dan Neely23-Sep-05 7:05 
GeneralRe: need to understand once and for all.... Pin
...---...23-Sep-05 7:17
...---...23-Sep-05 7:17 
GeneralRe: need to understand once and for all.... Pin
Guffa23-Sep-05 8:06
Guffa23-Sep-05 8:06 
GeneralRe: need to understand once and for all.... Pin
...---...23-Sep-05 8:39
...---...23-Sep-05 8:39 
GeneralRe: need to understand once and for all.... Pin
Dave Kreskowiak23-Sep-05 8:48
mveDave Kreskowiak23-Sep-05 8:48 
AnswerRe: need to understand once and for all.... Pin
mikanu23-Sep-05 9:04
mikanu23-Sep-05 9:04 
this is a basic object oriented design issue. the whole purpose to having all the drawing in the overrided onPaint is because you want to not have to worry about calling/optimizing the drawing processs. That is, the object is responisble for drawing itself. Any code using your class won't have to know the specific rules for drawing your object, thus achieving loose coupling... well enugh said, in practice what you want to do is:

MyClass{
    // by default the fixed (x,y) coordinates are set to (0,0)
    private int _x = 0;
    private int _y = 0;

    // by default no line will be drawn
    private bool drawLineA = false;  
    private bool drawLineB = false;

   // should be called on each instance of the class to
   // set values for the fixed x and y parameters for the
   // draw line functions
   public void setInitialXY(int x,int  y)
   {
      _x = x;
      _y = y;
   }

   // helper function to demonstrate the concept
   private void  MyDrawLine(Graphics pg, int x,int y)
   {
        pg.DrawLine(_x,_y, x, y);
   }

   // the paint function examines the settings for each line
   // and draws the line if necessary. the values for x and y\
   // for each line have been picked arbitrary
   protected override void  OnPaint(PaintEventArgs pe)
   {
        if(drawLineA)
              MyDrawLine(pe.Graphics, 87, 67);
        if(drawLineB)
              MyDrawLine(pe.Graphics, 55, 94);
   }

    // the following two functions can be called on an instance of this class
    // to toggle the drawing of either of the functions
    //  a call to ToggleLineA/B can be made inside a button click event handler
   public void ToggleLineA()
   {
       drawLineA = !drawLineA;  // this toggles whether or not Line A is drawn
       this.Invalidate();       // after the change, make sure the object will be redrawn
   }

   public void ToggleLineB()   
   {
       drawLineB = !drawLineB;  // this toggles whether or not Line B is drawn
       this.Invalidate();       // after the change, make sure the object will be redrawn
   }
}


I hope this answers your question and explains concept...

-- modified at 15:11 Friday 23rd September, 2005
AnswerRe: need to understand once and for all.... Pin
Guffa23-Sep-05 11:36
Guffa23-Sep-05 11:36 
QuestionExcel Type or Namespace Pin
zaboboa23-Sep-05 5:19
zaboboa23-Sep-05 5:19 
AnswerRe: Excel Type or Namespace Pin
Radioactive Cat23-Sep-05 7:06
sussRadioactive Cat23-Sep-05 7:06 
Questiondeployment (removing previous versions automatically) Pin
BlackDice23-Sep-05 4:30
BlackDice23-Sep-05 4:30 
AnswerRe: deployment (removing previous versions automatically) Pin
Wjousts23-Sep-05 10:08
Wjousts23-Sep-05 10:08 
GeneralRe: deployment (removing previous versions automatically) Pin
BlackDice23-Sep-05 10:09
BlackDice23-Sep-05 10:09 
QuestionA question about the "file field" Pin
shanzy23-Sep-05 3:38
shanzy23-Sep-05 3:38 
AnswerRe: A question about the "file field" Pin
Dave Kreskowiak23-Sep-05 6:23
mveDave Kreskowiak23-Sep-05 6:23 
QuestionType of a calling instace from within a method Pin
Radioactive Cat23-Sep-05 3:37
sussRadioactive Cat23-Sep-05 3:37 
AnswerRe: Type of a calling instace from within a method Pin
Dave Kreskowiak23-Sep-05 6:08
mveDave Kreskowiak23-Sep-05 6:08 
GeneralRe: Type of a calling instace from within a method Pin
Anonymous23-Sep-05 7:01
Anonymous23-Sep-05 7:01 
AnswerRe: Type of a calling instace from within a method Pin
leppie23-Sep-05 7:16
leppie23-Sep-05 7:16 
GeneralRe: Type of a calling instace from within a method Pin
Dave Kreskowiak23-Sep-05 7:25
mveDave Kreskowiak23-Sep-05 7:25 
GeneralRe: Type of a calling instace from within a method Pin
Radioacitve Cat23-Sep-05 7:38
sussRadioacitve Cat23-Sep-05 7:38 
QuestionFormating Report in C# programatically Pin
zaboboa23-Sep-05 3:16
zaboboa23-Sep-05 3:16 
QuestionLosing Focus Pin
Sled Dog23-Sep-05 3:09
Sled Dog23-Sep-05 3:09 
QuestionCheck user rights in .NET Pin
Stefan_ Spenz23-Sep-05 2:27
Stefan_ Spenz23-Sep-05 2:27 

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.