pictureBox1_Paint() doesn't draw

Hi, I am making a scrolling text with transparent background.

However, thepictureBox1_Paint() seem to draw nothing!

Any idea?


public partial class Form1 : Form
{
private String drawString = "Sample Text";
private Font drawFont = new Font("Arial", 31);
private SolidBrush drawBrush = new SolidBrush(Color.White);
private int textX = 0;
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
timer1.Enabled = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString(drawString, drawFont, drawBrush, new PointF(textX, 0));
}

private void timer1_Tick(object sender, EventArgs e)
{
textX++;
Invalidate();
}
}


[1192 byte] By [ddlam] at [2007-12-23]
# 1
You may override the OnPaint function to do the painting. Hope this helps.
gqlu at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2

I tested the code above, and it worked fine. Of course, when textX is large enough, the string will be out of the client size.

To debug, try to add MessageBox.Show("pictureBox1_Paint called"); in the pictureBox1_paint function, and have a check.

gqlu at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3

Thanks gqlu, the "pictureBox1_Paint called" is printed out. The sample text is printed too.

but I suppose it to draw every minisecond

as I have written
e.Graphics.DrawString(drawString, drawFont, drawBrush, new PointF(textX++, 0));

However, it draw once only.

ddlam at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

Hi,

Make sure that the timer is enabled. To demonstrate that the timer is on, add a label in the form, and add the statement "this.label1.Text = this.textX.ToString();" in the pictureBox1_Paint function. Then you can clearly see whether the timer is on.

gqlu at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5

Thanks gqlu. The timer is also enabled.

I see the problem is that the "sample text" is not drawing in the From1.pictureBox1.

Instead, the text is drawn on the From1 which is covered by the pictureBox1.

Can I draw the text at the toppest component/layer?

or can I make it draw on the pictureBox1?

ddlam at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
You're invalidating the form, you need to invalidate the picturebox:

private void timer1_Tick(...) {
textX++;
pictureBox1.Invalidate();
}

nobugz at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

You also need to add the handler to PictureBox's Paint event, not form's:

this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

Andrej

AndrejTozon at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...