Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Custom Control question for experts
 

Custom Control question for experts

Hello,

I'm doing a Microsoft MCTS C# course using Visual Studio 2005 Prof.
In the manual and video it says that 'custom painting' of a Custom Control must be done in the 'OnPaint' event. In the course-video a Custom Control Button's BackColor/ForeColor are set in the OnPaint event. The instructor is surprised that when he adds a BlueButton from the Toolbox on a Form that 'the mouse behaves strange' (devenv.exe takes 99% of processortime in taskmanager) and that an exception occurs when building and that the BlueButton does not show a properties window. To me there's something wrong.

I moved the BackColor/ForeColor property settings from the OnPaint event into the Constructor and voila: a nice blue button appears in the Toolbox and lets itself nicely put on a Form with correct properties and even the click events work fine.

So my statement is: There's a fundamental error or some info missing in the Customer Control documentation that even the Microsoft MCTS instructor does not know of.

Just challenge me! But please first try it out yourselve before answering from theory only.
Henk


namespace myExperiments01 {
public partial class BlueButton:Button {
public BlueButton() {
InitializeComponent();
this.BackColor=Color.Blue; // <--- NEW POSITION
this.ForeColor=Color.White;
// <--- NEW POSITION
}
protected override void OnPaint(PaintEventArgs pe) {
// TODO: Add custom paint code here
//this.BackColor=Color.Blue; <--- OUTCOMMENTED
//this.ForeColor=Color.White;
<--- OUTCOMMENTED
// Calling the base class OnPaint
base.OnPaint(pe);
}
}
}
mshvw  Saturday, July 07, 2007 8:48 AM

There is indeed something strange going on here.

Setting the BackColor seems to be causing a problem, but it should not be as the OnBackColorChanged() method, which raises the Paint Event, is only called if the Color differs from the current set Color. For some reason though, the Control is recieving WM_PAINT messages in a continuous loop.

A simple fix would be to set the BackColor only if it differs from the desired Color:

if (!this.BackColor.Equals(Color.Blue))

this.BackColor = Color.Blue;

This problem does not occur in VS2003, so I would suspect that this is an issue caused by the CustomRenderers used to paint the button when FlatStyle is not set to System.

If the Custom Control Inherits from Control rather than a ButtonBase derivitivethen this is not an issue.

As a side note:

I don't have any sort of programming qualifications, but I would think it bad practice to change GUI properties during a Paint event, even if there were no such problems.

Mick Doherty  Saturday, July 07, 2007 1:15 PM

There is indeed something strange going on here.

Setting the BackColor seems to be causing a problem, but it should not be as the OnBackColorChanged() method, which raises the Paint Event, is only called if the Color differs from the current set Color. For some reason though, the Control is recieving WM_PAINT messages in a continuous loop.

A simple fix would be to set the BackColor only if it differs from the desired Color:

if (!this.BackColor.Equals(Color.Blue))

this.BackColor = Color.Blue;

This problem does not occur in VS2003, so I would suspect that this is an issue caused by the CustomRenderers used to paint the button when FlatStyle is not set to System.

If the Custom Control Inherits from Control rather than a ButtonBase derivitivethen this is not an issue.

As a side note:

I don't have any sort of programming qualifications, but I would think it bad practice to change GUI properties during a Paint event, even if there were no such problems.

Mick Doherty  Saturday, July 07, 2007 1:15 PM
You are right.

It's not the location of the code but it is the code itself that's causing the trouble.
The setting of the BackColor property is the evil one. Somehow it initiates it's own OnPaint event causing recursive calls. That's why the 'devenv.exe' task takes 99% of processortime in taskmanager.

Your workaround works fine.

Now I understand why the instructor seems to be confused. Maybe he was used to VS2003 and teaching in VS2005 this he was not expecting.

I allready send a report to the originators of the official Microsoft course but I will add your comment to an additional report.


Regards,

Henk


mshvw  Saturday, July 07, 2007 1:48 PM
Hello Mick Doherty,

Congratulations!

You workaround has now been implemented in on of the official Microsoft MCTS C# course video's!

Just to let you know,

Regards,

Henk


mshvw  Friday, August 03, 2007 11:27 AM

You can use google to search for other answers

Custom Search

More Threads

• TabControl question...
• User control problem with Visible and Enabled
• custom controls
• different form height shown in the screen of notebook and desktop?
• Create a new ControlCollection in a Form in c#
• Design-time functionality at run-time
• Changing font of ErrorProvider and DataGridViewRow.ErrorText
• MonthCalendar: HighLight Only Selected Range.
• Feature rich .NET ComboBox control released!
• .NET Magic TabControl Help