CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2008
    Posts
    36

    Question timer and drawing

    I have a timer that will be used to draw an image
    The interval is 3 seconds, In the OnPaint() I will change the coordinates of the rectangle I draw
    when I run the program it just doesn't work.

    Code:
    Timer timer=null;
    int x;
    int y;
    private void Func()
    {
       timer=new Timer();
       x=1;
       y=2;
       timer.Tick+=new EventHandle(timer_Tick)
    }
    
    protected void timer_Tick(EventArg a)
    {
    }
    
    protected overide void Onpain(PaintEvetArg e)
    {
       Graphics g=e.grahics;
       g.drawRectanfgle(brush,x,y,12,12)
       x++;
       y++;
    }
    Thanks

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: timer and drawing

    You should awlays state what you have tried and what specifically is not working. You are not doing anything in your Timer.Tick handler, so what would you expect? You are simply incrementing x and y in OnPaint, the timer is useless. You have to call Start() on your timer and then change the x and y coordinates in the Tick handler. You will also need to set the interval on the timer. You will want to call Invalidate() on your control after changing the x and y in the Tick handler.

  3. #3
    Join Date
    May 2008
    Posts
    36

    Re: timer and drawing

    Thanks a lot,
    I will stick with only the narrow sense at most I could figure out your explanation.
    I am sorry for my bad English used by the way.

  4. #4
    Join Date
    Nov 2007
    Posts
    74

    Re: timer and drawing

    Quote Originally Posted by BigEd781 View Post
    .... You are not doing anything in your Timer.Tick handler, so what would you expect? You are simply incrementing x and y in OnPaint, the timer is useless...
    It might not be related to how a timer will work, if you already have the control.
    Sometimes it is just a flip of true and false to completely disable properties.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: timer and drawing

    Quote Originally Posted by Thu View Post
    It might not be related to how a timer will work, if you already have the control.
    Sometimes it is just a flip of true and false to completely disable properties.
    I don't understand that at all...
    He wants to update the rectangle location using the Timer, but the Timer is not doing anything. It is not even started.

  6. #6
    Join Date
    Aug 2008
    Posts
    112

    Re: timer and drawing

    Quote Originally Posted by BigEd781 View Post
    I don't understand that at all...
    He wants to update the rectangle location using the Timer, but the Timer is not doing anything. It is not even started.
    Most students in my class has it as spring exercise alreay over, I need him to return only the lost papers, which doesn't cost him much. No more further discussion of solution is kindly requested
    Thanks and Regards
    hi,,,

  7. #7
    Join Date
    Dec 2006
    Posts
    38

    Re: timer and drawing

    Quote Originally Posted by Khiem View Post
    Most students in my class has it as spring exercise alreay over, I need him to return only the lost papers, which doesn't cost him much. No more further discussion of solution is kindly requested
    Thanks and Regards
    That, I believe, is the most bizarre statement I have ever read on this board!

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: timer and drawing

    Is it guaranteed that the thread the timer uses to handle the tick is the thread that created the graphics? If not, might there be a windows GUI cross threading problem (in addition to the fact that the timer is inactive, and the handler code does nothing) ?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured