Custom Control - OnPaint - not drawing

A

Ashutosh

Hi,
I am trying to create a custom control in which I have to display my custom
data. The custom control is placed on a form and docked completely. When I
have the following code for the custom control, it doesn't re-paint all the
data when I re-size the form vertically at run time. If I switch to other
window and use ALT-TAB to switch back to my form, everything works fine.


The code for my custom control is

public partial class CustomControl1 : Control
{
Dictionary<int, string> data = new Dictionary<int, string>();
public CustomControl1()
{
InitializeComponent();
for (int i = 1; i <= 40; i++)
{
data[i * 20] = "This is a sample text for line number " +
i.ToString();
}
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (int y in data.Keys)
{
if (e.ClipRectangle.Y <= y && (e.ClipRectangle.Y +
e.ClipRectangle.Height) >= y)
{
e.Graphics.DrawString(data[y], SystemFonts.DefaultFont,
SystemBrushes.ControlText, new PointF(20, y));
}
}
}
}

I am actually confused with the the usage of ClipRectangle. Please advice.

Thanks & Regards,
Ashu
 
L

Linda Liu[MSFT]

Hi Ashu,

I performed a test on your sample code and did see the problem on my side.

If you print out the value of the PaintEventArgs.ClipRectangle property
from within the OnPaint method, you will see why the custom control doesn't
re-paint all the data when the form is resized vertically at tun time.

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// add this line of code to print the value of the
ClipRectangle property to the Output window
Console.WriteLine(e.ClipRectangle.ToString());
foreach (int y in data.Keys)
{
if (e.ClipRectangle.Y <= y && (e.ClipRectangle.Y +
e.ClipRectangle.Height) >= y)
{
e.Graphics.DrawString(data[y], SystemFonts.DefaultFont,
SystemBrushes.ControlText, new PointF(20, y));
}
}
}

Press F5 to run the application and resize the form vertically. The values
printed in the Output window are as follows:

{X=0,Y=0,Width=419,Height=413}
{X=0,Y=413,Width=419,Height=3}
{X=0,Y=416,Width=419,Height=3}
{X=0,Y=419,Width=419,Height=8}
{X=0,Y=427,Width=419,Height=3}
{X=0,Y=430,Width=419,Height=3}
{X=0,Y=433,Width=419,Height=1}
{X=0,Y=434,Width=419,Height=2}
{X=0,Y=436,Width=419,Height=4}
{X=0,Y=440,Width=419,Height=5}
{X=0,Y=445,Width=419,Height=3}
{X=0,Y=448,Width=419,Height=9}
{X=0,Y=457,Width=419,Height=2}
...

As you can see, the Paint event of the custom control is raised continously
when I resize the form. Each time when the Paint event is raised, the
PaintEventArgs.ClipRectangle is a small rectangle that doesn't meet the
condition you set in the OnPaint method. Thus, the new strings are not
drawn in the new rectangle on the custom control that needs painting.

Use the ClientRectangle property of the custom control instead of the
PaintEventArgs.ClipRectangle and this should solve your problem. For
example:

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (int y in data.Keys)
{
if (this.ClientRectangle.Y <= y && (this.ClientRectangle.Y
+ this.ClientRectangle.Height) >= y)
{
e.Graphics.DrawString(data[y], SystemFonts.DefaultFont,
SystemBrushes.ControlText, new PointF(20, y));
}
}
}

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Ashutosh

Hi,
Please ignore the post! The issue is resolved.

I didn't consider the various cases of overlapping of clipping region and he
drawing text.

Thanks & Regards,
Ashu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top