Control Authoring OnPaint not working as expected
-
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
-
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
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
-
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