3

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();
}
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Bor
  • 775
  • 3
  • 19
  • 44
  • Wouldn't it be easier to use a textbox and only draw the text on the canvas afterwards? – GolezTrol Nov 04 '12 at 08:25
  • The only thing I know is that there is going to be a memory leak by `SolidBrush`. – Alvin Wong Nov 04 '12 at 08:26
  • @AlvinWong .net handles garbage collection. It's still a good practice to use using, however. – Yatrix Nov 04 '12 at 08:39
  • 1
    @Yatrix No, GDI+ resources **need** to be disposed properly (like putting it in a `using` block) because they are not GC-ed. http://dotnetfacts.blogspot.com/2008/03/things-you-must-dispose.html – Alvin Wong Nov 04 '12 at 09:28
  • @GolezTrol can you give me any further information about adding a textbox ? OnMouseDown creates a textbox or ? I added using as well – Bor Nov 04 '12 at 09:32
  • @AlvinWong then thank you for teaching me something new. – Yatrix Nov 04 '12 at 23:31

1 Answers1

3

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.

Ove
  • 6,227
  • 2
  • 39
  • 68
  • 1
    I just learnt that `Refresh` is a combination of `Invalidate` and `Update`. http://blogs.msdn.com/b/subhagpo/archive/2005/02/22/378098.aspx – Alvin Wong Nov 05 '12 at 02:05