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(smaxWidth = 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(linesbufferGraphics.DrawString(lines
,
}
}
}
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 OnPaintbase.OnPaint(pe);}
protectedoverridevoid OnMouseEnter(EventArgs e){
_scrolling =
false;base.OnMouseEnter(e);}
protectedoverridevoid OnMouseLeave(EventArgs e){
_scrolling =
true;base.OnMouseLeave(e);}
}

