Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Timer erases background in windows form? I clear the screen and add the background again, but then the timer disappears. How can I use the timer without it messing with my background image?
Posted
Comments
choudhary.sumit 7-Dec-12 6:17am    
post your code.
AnkitGoel.com 7-Dec-12 6:28am    
please explain more. some code may help to understand
Sergey Alexandrovich Kryukov 7-Dec-12 15:16pm    
Timers are evil, and using System.Windows.Forms.Timer is the worst. What is "timer disappears"?! It's not an image or something like that. Most likely, you are abusing drawing. This is not how it works. It you clearly and correctly describe your goals, I'll tell you exactly what to do.
--SA
Member 9665282 8-Dec-12 3:03am    
I have a form with a background picture, and i would like to add a timer. but everytime i clear the form (for the timer) the image is destroyed.
private void timer1_Tick(object sender, EventArgs e)
{
milisec++;
if (milisec == 10)
{
if (sec < 10)
sec1 = "0" + Convert.ToString(sec);
else
sec1 = Convert.ToString(sec);
sec++;
milisec = 0;
dc.Clear(Color.White);
//his.BackgroundImage = new Bitmap(@"C:\Users\Adi\Pictures\משחקון\winter.jpg");
//dc.DrawImage(new Bitmap(@"C:\Users\Adi\Pictures\משחקון\winter.jpg"), this.Width, this.Height);
dc.DrawString(min1 + " : " + sec1, b, a, 600, 40);
}
if (sec == 60)
{
min++;
if (min < 10)
min1 = "0" + Convert.ToString(min);
else
min1 = Convert.ToString(min);
sec = 0;
}
}
Sergey Alexandrovich Kryukov 8-Dec-12 23:32pm    
You did not answer my questions. "I would like to add a timer" is not a sufficient reason to add a timer. And this is not how graphics works. You did not explain your ultimate goals -- nothing to discuss.
--SA

Something like the following perhaps?

C#
public partial class Form1 : Form
{

  private Timer t;

  public Form1()
  {
    InitializeComponent();
    t = new Timer();
    t.Tick += new EventHandler(t_Tick);
    t.Interval = 3000;
    t.Enabled = true;

    this.BackgroundImage = new Bitmap(@"C:\@Download\Images\Aero\FaireyFirefly.bmp");

  }

  void t_Tick(object sender, EventArgs e)
  {

    // Pick font, size and colour for the timer legend.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    PointF drawPoint = new PointF(10.0F, 10.0F);
    Graphics g = this.CreateGraphics();

    String legend = DateTime.Now.ToLongTimeString();

    // Hard coded for simpliciity.
    // Use g.MeasureString to get the correct size of the required output area.
    // Clear the previous op.
    Rectangle r = new Rectangle(10,10,100,30);
    Invalidate(r);
    Update();

    // Paint the string we want.
    g.DrawString(legend, drawFont, drawBrush, drawPoint);
    g.Dispose();

  }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jan-13 13:31pm    
The whole idea of using this timer is wrong. It's accuracy make any animation non-smooth. And "CreateGraphics" is totally wrong here. You need to use the instance passed in the event args of OnPaint or Paint, in almost all cases.
—SA
Thank you so much brother for sharing your knowledge and information i used your code and working properly thanks alot
 
Share this answer
 
Comments
BillWoodruff 17-Nov-14 13:42pm    
I think you may have meant this post as a message of thanks to 'cigwork: please post this a comment on his solution, rather than a solution. And, if you want to thank him, why not vote him up, also.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900