(VS 2008 - C# - Compact Framework) problem with e.Graphics.DrawString
-
Hello to all, I'm writing an application using C# in VisualStudio 2008 for Windows Embedded Compact 7 operating system, that is a SmartDevice application. I have a Form and a UserControl. In the Form load event I create a UserControl instance and I put it in the form:
private void Form1\_Load(object sender, EventArgs e) { UserControl1 uc1 = new UserControl1(); uc1.Parent = this; uc1.Location = new Point(607, 350); }
This works. I want also to write a string in the UserControl from Form1. I tried writing the following function in the UserControl:
public void SetText(String testo)
{
Graphics gr;
PaintEventArgs e;gr = CreateGraphics(); Font f = new Font("Tahoma", 14, FontStyle.Bold); e = new PaintEventArgs(gr, ClientRectangle); SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; Rectangle centered = e.ClipRectangle; centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(testo, f).Height) / 2); e.Graphics.DrawString(testo, f, br, centered, sf); }
and I called it from Form1:
private void Form1\_Load(object sender, EventArgs e) { UserControl1 uc1 = new UserControl1(); uc1.Parent = this; uc1.Location = new Point(607, 350); uc1.SetText("1"); }
but it doesn't work, it writes nothing without errors. So I modified the function in the UserControl in this way:
public void SetText(String testo, PaintEventArgs e) { Graphics gr; //PaintEventArgs e; gr = CreateGraphics(); Font f = new Font("Tahoma", 14, FontStyle.Bold); //e = new PaintEventArgs(gr, ClientRectangle); SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; Rectangle centered = e.ClipRectangle; centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(testo, f).Height) / 2); e.Graphics.DrawString(testo, f, br, centered, sf); }
and I called it in this way:
private void Form1\_Load(object sender, EventArgs e) { UserControl1 uc1 = new UserControl1(); uc1.Parent = this; uc1.Location = new Point(607, 350); PaintEventArgs e
-
Hello to all, I'm writing an application using C# in VisualStudio 2008 for Windows Embedded Compact 7 operating system, that is a SmartDevice application. I have a Form and a UserControl. In the Form load event I create a UserControl instance and I put it in the form:
private void Form1\_Load(object sender, EventArgs e) { UserControl1 uc1 = new UserControl1(); uc1.Parent = this; uc1.Location = new Point(607, 350); }
This works. I want also to write a string in the UserControl from Form1. I tried writing the following function in the UserControl:
public void SetText(String testo)
{
Graphics gr;
PaintEventArgs e;gr = CreateGraphics(); Font f = new Font("Tahoma", 14, FontStyle.Bold); e = new PaintEventArgs(gr, ClientRectangle); SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; Rectangle centered = e.ClipRectangle; centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(testo, f).Height) / 2); e.Graphics.DrawString(testo, f, br, centered, sf); }
and I called it from Form1:
private void Form1\_Load(object sender, EventArgs e) { UserControl1 uc1 = new UserControl1(); uc1.Parent = this; uc1.Location = new Point(607, 350); uc1.SetText("1"); }
but it doesn't work, it writes nothing without errors. So I modified the function in the UserControl in this way:
public void SetText(String testo, PaintEventArgs e) { Graphics gr; //PaintEventArgs e; gr = CreateGraphics(); Font f = new Font("Tahoma", 14, FontStyle.Bold); //e = new PaintEventArgs(gr, ClientRectangle); SolidBrush br = new SolidBrush(System.Drawing.SystemColors.ControlText); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; Rectangle centered = e.ClipRectangle; centered.Offset(0, (int)(e.ClipRectangle.Height - e.Graphics.MeasureString(testo, f).Height) / 2); e.Graphics.DrawString(testo, f, br, centered, sf); }
and I called it in this way:
private void Form1\_Load(object sender, EventArgs e) { UserControl1 uc1 = new UserControl1(); uc1.Parent = this; uc1.Location = new Point(607, 350); PaintEventArgs e
You do NOT put the Graphics and Drawing code in SetText. That goes in the Paint event of your UserControl. SetText should only set a private field that contains the text to paint and then call Invalidate on itself to get the control to repaint itself. Your code also leaks resources badly. You're not calling Dispose on the GDI objects (Graphics, Brush, Pen, ...) that you're creating. You'll eventually run Windows out of handles and it'll start acting weird and crash.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
You do NOT put the Graphics and Drawing code in SetText. That goes in the Paint event of your UserControl. SetText should only set a private field that contains the text to paint and then call Invalidate on itself to get the control to repaint itself. Your code also leaks resources badly. You're not calling Dispose on the GDI objects (Graphics, Brush, Pen, ...) that you're creating. You'll eventually run Windows out of handles and it'll start acting weird and crash.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakThanks very much Dave, this is the help I needed. I solved the problem. ...and yes, you're right, I miss some Dispose, but this was just a short and fast sample to show my problem. Anyhow I recognize that my code is not good code... Thanks