Draw text on a Panel

Bor picture Bor · Nov 4, 2012 · Viewed 13.4k times · Source

OK, I fix everything, now is exactly what I want. I have a textBox1, panel1, and drawTexta (a button).

When I click the button and choose a point in the panel, I want to draw the string from the textBox1.

private void panel1_Paint(object sender, PaintEventArgs e)
{
    using (SolidBrush br = new SolidBrush(Color.Red))
    {
        StringFormat sf = new StringFormat();
        sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
        e.Graphics.DrawString(textBox1.Text, this.Font, br, point1, sf);
    }
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    point1 = new Point(e.X, e.Y);
} 

bool flag = false;
Point point1 = new Point();

private void drawTexta_Click(object sender, EventArgs e)
{ 
    flag = true;
    panel1.Refresh();
}

Answer

Ove picture Ove · Nov 4, 2012

The text isn't being drawn to panel1 because you need to refresh it.

Add this code to button1_Click, after you set drawText to true:

panel1.Refresh();

That will make the static text show up.