Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Problem with my custom control crashing the Windows Forms Designer
 

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:

enum ScrollDirection { Up, Down };

public partial class ScrollingText : Control

{

private int _LineSpacing;

private Timer _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 = new Size(50, 50);

_timer = new Timer();

_timer.Interval = 100;

_timer.Tick += new EventHandler(_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();

}

}

public SizeF 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;

}

return new SizeF(maxWidth, maxHeight);

}

void DrawText(Graphics g, string s)

{

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

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

if (b != null)

b.Dispose();

if (size.Width == 0f)

size.Width = 10f;

b = new Bitmap((int)size.Width, (int)size.Height);

Graphics bufferGraphics = Graphics.FromImage(b);

using (Brush brush = new SolidBrush(this.ForeColor))

{

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

{

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

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

}

}

}

protected override void OnPaint(PaintEventArgs pe)

{

DrawText(pe.Graphics, this.Text);

if (b.Height < this.Height)

{

yPos = 0;

_scrolling = false;

}

pe.Graphics.DrawImage(b, new Point(0, yPos));

// Calling the base class OnPaint

base.OnPaint(pe);

}

protected override void OnMouseEnter(EventArgs e)

{

_scrolling = false;

base.OnMouseEnter(e);

}

protected override void OnMouseLeave(EventArgs e)

{

_scrolling = true;

base.OnMouseLeave(e);

}

}

Shane Poppleton  Tuesday, August 01, 2006 7:44 AM

Not a problem shane.

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

Ken

Ken_Bussell  Thursday, August 03, 2006 1:47 AM

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  Tuesday, August 01, 2006 6:37 PM

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.

Shane Poppleton  Tuesday, August 01, 2006 9:40 PM
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.
Shane Poppleton  Tuesday, August 01, 2006 9:44 PM

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  Wednesday, August 02, 2006 1:05 PM
I am very new to control development, so excuse my ignorance, how do i determine if i am in design time or run time?
Shane Poppleton  Wednesday, August 02, 2006 8:41 PM

Not a problem shane.

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

Ken

Ken_Bussell  Thursday, August 03, 2006 1:47 AM

You can use google to search for other answers

Custom Search

More Threads

• ExpandableObjectConverter with multiple objects.
• New designers for existing controls.
• Doesnot show image in datagridview for the first time when set an MDIParent property
• Need opinion on solution for creating Clickable labels for Smartphones
• Disabling the .NET IDE option
• NumericUpDown - Prevent User Deleting Entry
• Wandering Controls in VS2003
• GroupBox in a UserControl, redux
• VB.NET Toolbars
• Search Combobox for value