Reputation: 1
I'm making a simple control based on a button, but I can't change its background color inside the OnPaint event without generating an error in Visual Studio's design mode only.
I edited the OnPaint event to see the background color change in design mode, but I needed to check if this change is being made in design mode only, otherwise the Click event of the button wasnt working:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.DesignMode)
{
this.BackColor = System.Drawing.Color.Yellow;
}
}
I also needed to change the OnCreateControl event for the change to be applied at runtime:
protected override void OnCreateControl()
{
base.OnCreateControl();
this.BackColor = System.Drawing.Color.Yellow;
}
This makes the button work perfectly as expected, but an error (System.InvalidOperationException) is thrown when starting the debug with the design page in focus. It looks like the OnPaint event tries to change the design while debug is active. The error only happens in the design, the form continues to work correctly.
Design before debugging Design after debuggin whith the working form
This error is very annoying, because I need to reopen the design of the form I'm working on whenever I do a debug on it. Does anyone know how to fix this?
Upvotes: 0
Views: 58