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. C#
  4. Control Authoring OnPaint not working as expected

Control Authoring OnPaint not working as expected

Scheduled Pinned Locked Moved C#
graphicsquestionhelp
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.
  • T Offline
    T Offline
    TheFoZ
    wrote on last edited by
    #1

    Hi All I'm trying to author a control which displays text vertically. I've overridden the OnPaint method and Text property so that if the Text is Changed it calls the new OnPaint method. When the text is changed, the old text does not disappear and the new text is overlaid on the old. How do I stop this from happening? The OnPaint method looks like this

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
    
            if (!String.IsNullOrEmpty(Text))
            {
                System.Drawing.Graphics formGraphics = pe.Graphics;
    
                System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 10);
                System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                float x = 150f;
                float y = 50f;
                System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
                formGraphics.DrawString(Text, drawFont, drawBrush, x, y, drawFormat);
                drawFont.Dispose();
                drawBrush.Dispose();
                formGraphics.Dispose();
            }
    
        }
    

    And the Text property is

        public override string Text
        {
    
            get { return base.Text; }
            set
            {
                base.Text = value;
                this.OnPaint(CreateNewPaintEventArgs());
            }
    
        }
    
        private PaintEventArgs CreateNewPaintEventArgs()
        {
            return new PaintEventArgs(CreateGraphics(), new Rectangle(new Point(0, 0), Size));
        }
    

    Your help is appreciated.

    The FoZ

    OriginalGriffO 1 Reply Last reply
    0
    • T TheFoZ

      Hi All I'm trying to author a control which displays text vertically. I've overridden the OnPaint method and Text property so that if the Text is Changed it calls the new OnPaint method. When the text is changed, the old text does not disappear and the new text is overlaid on the old. How do I stop this from happening? The OnPaint method looks like this

          protected override void OnPaint(PaintEventArgs pe)
          {
              base.OnPaint(pe);
      
              if (!String.IsNullOrEmpty(Text))
              {
                  System.Drawing.Graphics formGraphics = pe.Graphics;
      
                  System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 10);
                  System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
                  float x = 150f;
                  float y = 50f;
                  System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
                  formGraphics.DrawString(Text, drawFont, drawBrush, x, y, drawFormat);
                  drawFont.Dispose();
                  drawBrush.Dispose();
                  formGraphics.Dispose();
              }
      
          }
      

      And the Text property is

          public override string Text
          {
      
              get { return base.Text; }
              set
              {
                  base.Text = value;
                  this.OnPaint(CreateNewPaintEventArgs());
              }
      
          }
      
          private PaintEventArgs CreateNewPaintEventArgs()
          {
              return new PaintEventArgs(CreateGraphics(), new Rectangle(new Point(0, 0), Size));
          }
      

      Your help is appreciated.

      The FoZ

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Don't call OnPaint! Never, ever, call OnPaint directly. Use Invalidate instead - it causes the framework to correctly call OnPaint with the appropriate graphics object etc.

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      T 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Don't call OnPaint! Never, ever, call OnPaint directly. Use Invalidate instead - it causes the framework to correctly call OnPaint with the appropriate graphics object etc.

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        T Offline
        T Offline
        TheFoZ
        wrote on last edited by
        #3

        Thanks OriginalGriff. I changed my Text property to

            public override string Text
            {
        
                get { return base.Text; }
                set
                {
                    base.Text = value;
                    this.Invalidate();
                }
        
            }
        

        and all is well.

        The FoZ

        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