vendredi 17 avril 2015

Custom Control Unhandled Exception in the Designer

I'm using code for a custom tab control that I found online and modified to suit my needs.



protected override void OnPaint(PaintEventArgs e) {
this.PaintTransparentBackground(e.Graphics, this.ClientRectangle);
this.PaintAllTabs(e);

if (this.TabCount > 0 && ShowUnderline)
e.Graphics.DrawLine(new Pen(Colors.TAB_SELECTED, 1), UnderlineStartOffset, 23, this.ClientRectangle.Width - UnderlineEndOffset, 23);
}

protected void PaintAllTabs(PaintEventArgs e) {
if (this.TabCount > 0)
{
CursorSet = false;

for (int index = this.TabCount - 1; index > -1; index--)
{
this.PaintTab(e, index);
this.PaintTabText(e, index);
this.TabPages[index].BackColor = Colors.CUSTOM_BLACK_2;
}
}
}


I also added some properties to be editable in the designer (not sure if this is how it is meant to be done):



[Browsable(true)]
[Category("Customizations")]
[DisplayName("Tab X Offset")]
public int XOffset { get; set; }

[Browsable(true)]
[Category("Customizations")]
[DisplayName("Tab Y Offset")]
public int YOffset { get; set; }

[Browsable(true)]
[Category("Customizations")]
[DisplayName("Tab Width Offset")]
public int WOffset { get; set; }

[Browsable(true)]
[Category("Customizations")]
[DisplayName("Show Tab Underline")]
public bool ShowUnderline { get; set; }

[Browsable(true)]
[Category("Customizations")]
[DisplayName("Underline Start Offset")]
public int UnderlineStartOffset { get; set; }

[Browsable(true)]
[Category("Customizations")]
[DisplayName("Underline End Offset")]
public int UnderlineEndOffset { get; set; }

[Browsable(true)]
[Category("Customizations")]
[DisplayName("Enable Tab Closing")]
public bool EnableTabClosing { get; set; }


The result is it looks great in the designer and at run-time, providing the Designer is not selected when running. If it is, I get the following error:



The control App.Custom_Controls.CustomTabControl has thrown an unhandled exception in the designer and has been disabled.


Exception: The file ..\View_Main.cs cannot be modifed in the designer while building or debugging.



If I surround the OnPaint with


if(!this.DesignMode) { }


The error is resolved but I lose the painting of my tab control in the designer.


Aucun commentaire:

Enregistrer un commentaire