Problem with my custom control crashing the Windows Forms Designer

If i add my control with this.controls.add(Mycontrol) in form_load i have no problems but if i add it from the toolbox, the designer crashes.

Here is the code for my control:

enumScrollDirection { Up, Down };

publicpartialclassScrollingText :Control

{

privateint _LineSpacing;

privateTimer _timer;

Bitmap b;

int yPos;

ScrollDirection _direction;

int _scrollAmount;

bool _scrolling;

public ScrollingText()

{

InitializeComponent();

_LineSpacing = 2;

this.Text ="Testing Line #1";

for (int i = 0; i < 20; i++)

this.Text +="\nTesting Line #" + (i + 2);

this.Size =newSize(50, 50);

_timer =newTimer();

_timer.Interval = 100;

_timer.Tick +=newEventHandler(_timer_Tick);

_timer.Enabled =true;

_direction =ScrollDirection.Up;

_scrollAmount = 2;

_scrolling =true;

this.SetStyle(ControlStyles.DoubleBuffer,true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);

this.SetStyle(ControlStyles.UserPaint,true);

}

void SwitchColours()

{

Color c =this.BackColor;

this.BackColor =this.ForeColor;

this.ForeColor = c;

}

void _timer_Tick(object sender,EventArgs e)

{

if (_scrolling)

{

if (_direction ==ScrollDirection.Up)

{

yPos -= _scrollAmount;

if (yPos < (this.Height - b.Height))

{

_direction =ScrollDirection.Down;

yPos =this.Height - b.Height;

}

}

else

{

yPos += _scrollAmount;

if (yPos > 0)

{

_direction =ScrollDirection.Up;

yPos = 0;

}

}

this.Invalidate();

this.Update();

}

}

publicSizeF CalculateSize(Graphics g,Font f,string[] s)

{

float maxWidth = 0f;

float maxHeight = 0f;

for (int i = 0; i < s.Length; i++)

{

SizeF size = g.MeasureString(sIdea, f);

if (size.Width > maxWidth)

maxWidth = size.Width;

maxHeight += size.Height + _LineSpacing;

}

returnnewSizeF(maxWidth, maxHeight);

}

void DrawText(Graphics g,string s)

{

string[] lines = s.Split(newchar[] {'\n' } );

SizeF size = CalculateSize(g,this.Font, lines);

if (b !=null)

b.Dispose();

if (size.Width == 0f)

size.Width = 10f;

b =newBitmap((int)size.Width, (int)size.Height);

Graphics bufferGraphics =Graphics.FromImage(b);

using (Brush brush =newSolidBrush(this.ForeColor))

{

for (int i = 0; i < lines.Length; i++)

{

SizeF lineSize = bufferGraphics.MeasureString(linesIdea,this.Font);

bufferGraphics.DrawString(linesIdea,this.Font, brush,newPointF(0f, ((lineSize.Height + _LineSpacing) * i)));

}

}

}

protectedoverridevoid OnPaint(PaintEventArgs pe)

{

DrawText(pe.Graphics,this.Text);

if (b.Height <this.Height)

{

yPos = 0;

_scrolling =false;

}

pe.Graphics.DrawImage(b,newPoint(0, yPos));

// Calling the base class OnPaint

base.OnPaint(pe);

}

protectedoverridevoid OnMouseEnter(EventArgs e)

{

_scrolling =false;

base.OnMouseEnter(e);

}

protectedoverridevoid OnMouseLeave(EventArgs e)

{

_scrolling =true;

base.OnMouseLeave(e);

}

}

[11020 byte] By [ShanePoppleton] at [2007-12-22]
# 1

Shane,

I got your code and added the control to the Visual Studio designer's toolbox and dragged your control to a dummy form and it worked fine no crash. I added the control to the toolbox by doing a browse to the dll itself after it was compiled. If you are doing it a different way, like letting Visual Studio automatically add the control via a project reference or something, then you have other problems.

I then took your code and put it into the MSDN sample by Dinesh Chandnani at http://msdn.microsoft.com/msdnmag/issues/06/03/DesignerHosting/

It worked fine. The designer doesn't crash.

I also put it into a custom desginer that I'm building and it worked there also.

This pretty much tells me that it is not your control, but the specific environment you are using it in.

Expand on how you are adding it to the toolbox and that will help narrow it down.

Ken

Ken_Bussell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 2

I think i may have solved it, my guess is the timer tick is happening before the constructor is finished, maybe my computer is slower than yours, if i leave the timer disabled and add a property to enable it than it is fine and doesn't crash, this was so frustrating because every time i opened the solution file i had form1's designer displayed and as soon as it opened it would crash, i worked out i could delete the SUO file for it to default of opening no files from the solution, then i could comment out most of the control's code and recompile so it would let me open the main form again.

I am developing on XP Media Center Edition, i know it is not the best choice, but it's all i have outside of work time.

ShanePoppleton at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 3
Also i forgot to mention how i was adding the control, i have a solution that contains a windows forms project and a class library, i just compile and Visual studio adds the control to the toolbox, then i just open the form and double click the control on the toolbox to add it to the form.
ShanePoppleton at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 4

Shane,

If the timer isn't needed at design time don't instanciate it. Actually you should not use anything at design time that you don't need. It will save you a bunch of headaches.

Ken

Ken_Bussell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 5
I am very new to control development, so excuse my ignorance, how do i determine if i am in design time or run time?
ShanePoppleton at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...
# 6

Not a problem shane.

Look into the DesignMode property in help. That should help you out.

Ken

Ken_Bussell at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Designer...