title: Custom control rendering weird in Tapcontrol
body: <p>Here is an odd one and it will be a bit long to describe so bear with me on this one. </p>
  
  <p>I have a control made for c# 4 client profile called <code>Toggleswitch</code> (tsw). This toggleswitch is design to act like the metro switch, and that is does splendidly ... until it is added to a Tap control/Tap page then :</p>
  
  <ul>
  <li><p>the rendering acts all weird not drawing the background properly and the on/off label don’t show up. This affects other controls as well, it like the Whole form won't draw. </p></li>
  <li><p>also both VS 2010 and 2013 design editors properties fields freeze up so I can't access the other controls </p></li>
  <li><p>and if left in normal the program won't build ... basically crashed VS..</p></li>
  </ul>
  
  <p>BUT, if I change the tap control from normal to either flatbutton or button then it stops and runs smoothly as always and that is fine, but then I started wondering why it don't render in the normal </p>
  
  <p>So here is what I have tried </p>
  
  <ul>
  <li><p>the tsw extends <code>Buttonbase</code> for clickability. So I changed that to control then the <code>OnPaint</code> gets called in a unlimited loop so that don’t help, also the click don’t Work but that is less important right now </p></li>
  <li><p>I extended of button, no help there still weird</p></li>
  <li><p>override every single method from both button and buttonbase to see if that helped still nothing. </p></li>
  <li><p>After looking at the forms designer I found that the only property the gets changed is the <code>UseVisualStyleBackColor</code> when false it works when true it don't, so I made an override that forces it to be false, still doesn't work :/ </p></li>
  <li><p>I even tried to remove the partial keyword.</p></li>
  <li><p>I spend half a day reading about the defriend methods of both tap and button controls </p></li>
  <li><p>even found a custom control test the extended of button that worked fine, but there was no reason for it because the only different from what I could see was an if statement in the <code>OnPaint</code> about some rendering. </p></li>
  </ul>
  
  <p>.... Nothing works ....</p>
  
  <p>Here is the togglewitch in its entirety:</p>
  
  <pre><code>public partial class ToggleSwith : ButtonBase
  {
          object _Lock = new object();
          private Color _OffColor = SystemColors.ControlDarkDark;
          private Color _OnColor = Color.LimeGreen;
          private bool _ColorText = false;
          // private bool _BeenDrawn = false;
          private bool _Checked = false;
  
          [Browsable(false)]
          public override string Text { get { return null; } }
          [Browsable(false)]
          public override bool AutoSize{get{return false;}}
          [Browsable(false)]
          public override ContentAlignment TextAlign { get { return ContentAlignment.MiddleLeft; } }
          [Category("Appearance")]
          public virtual Color OffColor { get { return _OffColor; } set { _OffColor = value; Invalidate(); } }
          [Category("Appearance")]
          public virtual Color OnColor { get { return _OnColor; } set { _OnColor = value; Invalidate(); } }
          [Category("Appearance")]
          public virtual bool ColorText { get { return _ColorText; } set { _ColorText = value; Invalidate(); } }
          [Category("Appearance")]
          public virtual bool Checked { get { return _Checked; } set { _Checked = value; Invalidate(); } }
          new public bool UseVisualStyleBackColor { get { return false; } set{} }
  
          protected override Size DefaultSize { get { return new Size(80, 18); } }
          protected override Size DefaultMinimumSize { get { return new Size(40, 13); } }
  
          public event EventHandler ToggleChanged;
  
          public ToggleSwith()
          {
              this.Size = new Size(80, 18);
              InitializeComponent();
              this.Click += ToggleSwith_Click;
          }
  
          void ToggleSwith_Click(object sender, EventArgs e)
          {
              lock (_Lock)
              {
                  if (_Checked) _Checked = false;
                  else _Checked = true;
              }
              OnToggleChanged();            
          }
  
          private void OnToggleChanged()
          {
              if (ToggleChanged != null)
              {
                  EventArgs ea = new EventArgs();
                  ToggleChanged(this, ea);
              }
          }
  
          protected override void OnPaint(PaintEventArgs e)
          {
              this.Controls.Clear();
              int toggleblocsize=(int)this.Size.Width/8;
              int indent = 23;
              int labelx = 2;
              // Declare and instantiate a new pen.
              SolidBrush OffPen = new SolidBrush(this.OffColor);
              SolidBrush OnPen = new SolidBrush(this.OnColor);
              SolidBrush BackgroundPen = new SolidBrush(this.BackColor);
              SolidBrush BlackPen = new SolidBrush(SystemColors.ControlText);
              Pen ControlDarkPen = new Pen(SystemColors.ControlDark);
              Pen ControlPen = new Pen(SystemColors.Control);
              e.Graphics.FillRectangle(BackgroundPen,0, 0, this.Size.Width,this.Size.Height);
              e.Graphics.DrawRectangle(ControlDarkPen, indent, -0, this.Size.Width - (indent+1), this.Size.Height-1);
              Label l = new Label();
              l.Font = this.Font;
              l.Size = new Size((indent-labelx),this.Size.Height);
  
              // if(this.Size.Height &lt;13)l.Location= new Point(labelx,-1);
              //  else l.Location = new Point(labelx, 0);
  
              l.TextAlign = TextAlign;
              l.ForeColor = this.ForeColor;
  
              if (this._Checked)
              {
                  //ligth
                  e.Graphics.FillRectangle(OnPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
                  //Toggle
                  e.Graphics.DrawRectangle(ControlPen, this.Size.Width - (toggleblocsize+1), -0, this.Size.Width - (indent + 1), this.Size.Height - 1);
                  e.Graphics.FillRectangle(BlackPen, this.Size.Width - (toggleblocsize), -1, this.Size.Width, this.Size.Height + 1);
                  if (ColorText) l.ForeColor = OnColor;
                  l.Text = "On";
              }
              else
              {
                  //ligth
                  e.Graphics.FillRectangle(OffPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
                  //Toggle
                  e.Graphics.DrawRectangle(ControlPen, indent , -0, (toggleblocsize + 1), this.Size.Height - 1);
                  e.Graphics.FillRectangle(BlackPen, (indent+1) , -1,(toggleblocsize), this.Size.Height + 1);
                  if (ColorText) l.ForeColor = this.ForeColor;
                  l.Text = "Off";
              }
              this.Controls.Add(l);
          }
  
          public override Size GetPreferredSize(Size proposedSize)
          {
              return new Size(80, 18);
          }
      }
  </code></pre>
  
  <p>It is not that I can't live with a flat tapcontrol it is just the why it doesn't work that bugs me ... </p>
  
  <p>Any help would be greatly appreciated :)</p>
  
  <p>Lastly some visual documentation of the problems:</p>
  
  <p>Here is how it should work:</p>
  
  <p><img src="https://i.stack.imgur.com/t2IY8.png" alt="here is how it should Work"></p>
  
  <p>This is with a tap on normal</p>
  
  <p><img src="https://i.stack.imgur.com/lYM2j.png" alt="this is with a tap on normal."></p>