Member Avatar for Annieken

Hello

I want to draw every second a new Rectangle, just beside the other one.
I've got a timer which tick's every second. In this method, I want to draw a Rectangle. But I've already have a function to draw a Rectangle. So I want just to call the function in the Tick.

public void timer1_Tick(object sender, System.EventArgs e)
        {          
            lbTime.Visible = true;
            TimeSpan span = DateTime.Now.Subtract(da);
            this.lbTime.Text = span.Hours.ToString() + " : " + span.Minutes.ToString() + " : " + span.Seconds.ToString();
            lblTimerStap.Visible = true;
            TimeSpan spant = DateTime.Now.Subtract(da);               
            this.lblTimerStap.Text = spant.Hours.ToString() + " : " + spant.Minutes.ToString() + " : " + spant.Seconds.ToString();
            pictureBox1.Invalidate();
            if (cont == true)
            {
                    pictureBox1.Paint();
             }

PictureBox1.Paint() gives a fault --> The event 'System.Windows.Forms.Control.Paint' can only appear on the left hand side of += or -=

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        { 
            Graphics g = e.Graphics;
            String algoritme = cmbSchedule.SelectedItem.ToString();
            int burstlengte = Controller.GetInstance(ProcessList).KeuzeVanAlgoritme(algoritme);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Rectangle rect = new Rectangle(20, 20, burstlengte * 2, 100);
            Pen redPen = new Pen(Color.Red, 3);
            g.FillRectangle(Brushes.Red, 25, 20, burstlengte * 2, 100);
}

Does anyone knows how I got to call the PictureBox1.paint into the Tick?

call picturebox1.invalidate()

Hi,
pictureBox1.Paint is an Event, not a Function. It is similar to Button.Click, Form.Load ... etc.. So pictureBox1.Paint(); is wrong.

You can simply call pictureBox1.Invalidate() as said by LizR. This will implicitly call Paint () Event.

Or else You can use pictureBox1.CreateGraphics() function to Create the Graphics object in the Timer Tick Handler

public void timer1_Tick(object sender, System.EventArgs e)
{          
             //Your Usual Codes
            if (cont == true)
            {   

                   //To draw
                    Graphics g = pictureBox1.CreateGraphics();
                    //Now use g to draw anything
                    ....
             }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.