Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. (VS 2008 - C# - Compact Framework) problem with e.Graphics.DrawString

(VS 2008 - C# - Compact Framework) problem with e.Graphics.DrawString

Scheduled Pinned Locked Moved .NET (Core and Framework)
graphicscsharpvisual-studiohardwarehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    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
    
    D 1 Reply Last reply
    0
    • S steve_9496613

      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
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • D 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 Kreskowiak

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        Thanks 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups