Reputation: 941
I created a UserControl
called Switch
.
I built my project and this UC successfully shown in my toolbox.
So I tried to add it to my frmMain
by dragging it to the form. But at this moment, Visual Studio always shows an error message:
A new guard page for the stack cannot be created.
After clicking OK, devenv.exe
crashes.
I have to mention, that I have another user control in the same namespace and folder as Switch
. This UC works fine.
This is the code of my Switch
user control:
public partial class Switch : UserControl
{
private Rectangle switchRectangle;
private int xOn = 0; // switchRectangle x position, when switch is on
private int xOff = 0; // switchRectangle x position, when switch is off
private Color SwitchColor = Color.Black;
private Color OuterRectangleColor = Color.DarkGray;
private Color InnerRectangleColor
{
get { return this.On ? Color.DodgerBlue : InnerRectangleColor; }
}
public bool On { get; set; }
public Switch()
{
InitializeComponent();
ReloadSwitchRectangle();
xOn = this.Width - switchRectangle.Width;
}
private void Switch_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(this.BackColor);
// Create inner rectangle, on every side by 2px smaller than ClientRectangle
const int amountToDecrease = 2;
Rectangle innerRectangle = new Rectangle(this.ClientRectangle.X + amountToDecrease, this.ClientRectangle.Y + amountToDecrease,
this.ClientRectangle.Width - amountToDecrease * 2, this.ClientRectangle.Height - amountToDecrease * 2);
ReloadSwitchRectangle();
e.Graphics.DrawRectangle(new Pen(OuterRectangleColor), this.ClientRectangle); // Draw outer rectangle
e.Graphics.FillRectangle(new SolidBrush(InnerRectangleColor), innerRectangle); // Fill inner rectangle
e.Graphics.FillRectangle(new SolidBrush(SwitchColor), switchRectangle);
}
private void ReloadSwitchRectangle()
{
int x = this.On ? xOn : xOff;
switchRectangle = new Rectangle(x, 0, this.Width / 5, this.Height);
}
}
Upvotes: 0
Views: 770
Reputation: 941
Thanks to Sriram Sakthivel, I solved my problem like this:
private Color InnerRectangleColor
{
get { return this.On ? Color.DodgerBlue : OuterRectangleColor; }
}
Upvotes: 0
Reputation: 73502
This is the problem:
private Color InnerRectangleColor
{
get { return this.On ? Color.DodgerBlue : InnerRectangleColor; }
}
You have infinite recursion here when this.On
is set to false.
Upvotes: 2