I have been using this control for over a year now and suddenly it randomly causes exceptions. And by random, I mean I can rebuild the solution and the form with load correctly. Sometimes I have to rebuild two or three times, but eventually it will work properly. The stack trace is vague at best. This might be easier if the exception was consistent.
Here is the entire code of the control:
VB.NET Code:
Imports System.ComponentModel
Imports System.Drawing
Public Class EditorForm
Private _color As Color = Color.FromArgb(32, 40, 56)
Private _font As Font = New Font("segoe ui", 8.5)
Private _formBorderStyle As Windows.Forms.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Private _foreColor As Color = Color.FromArgb(128, 144, 160)
' Add any initialization after the InitializeComponent() call.
SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or Windows.Forms.ControlStyles.SupportsTransparentBackColor Or Windows.Forms.ControlStyles.UserPaint, True)
400+ Posts you should know by now to explain your issue with out us having toi work out what you are doing.
Actually, I thought he explained it quite well.
I can't see anything wrong with the code though. I'd really like to see a better picture of that exception. I've run into designer problems like this numerous times when writing my own controls. The designer doesn't play well if you're not careful about bugs in your controls.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
Apparently the designer cannot create the window for the Form. I tried your code and it worked fine for me as is so I don't know what the problem is. However, this is not really how you're supposed to derive from Form. Here is the proper way:-
vbnet Code:
Imports System.ComponentModel
'Prevents the Forms designer from loading
'as this Form is a base class from which your
'application's Forms can inherit, it's not meant to be designed upon
<ToolboxItem(False)> _
Public Class BlackComponent
Inherits Component
End Class
Public Class EditorFormBase
Inherits Form
Private _color As Color = Color.FromArgb(32, 40, 56)
Private _font As Font = New Font("segoe ui", 8.5)
Private _formBorderStyle As Windows.Forms.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Private _foreColor As Color = Color.FromArgb(128, 144, 160)
' Add any initialization after the InitializeComponent() call.
SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or Windows.Forms.ControlStyles.SupportsTransparentBackColor Or Windows.Forms.ControlStyles.UserPaint, True)
DoubleBuffered = True
BackColor = _color
ForeColor = _foreColor
Font = _font
FormBorderStyle = _formBorderStyle
End Sub
End Class
I put the above in its own code file. When I need an editor form I simply inherit from EditorFormBase by changing the designer file for whatever Form I'm making.
C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter
There's just no reason to use garbage like InputBox. - jmcilhinney
The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber
The only thing that I see as possibly suspicious, is the Invalidate call in in the ForeColor Set Method. I vaguely recall something about calls to such methods that are executed in constructors being a problem.
Code:
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or Windows.Forms.ControlStyles.SupportsTransparentBackColor Or Windows.Forms.ControlStyles.UserPaint, True)
DoubleBuffered = True
BackColor = _color
'ForeColor = _foreColor
MyBase.ForeColor = _foreColor
Font = _font
FormBorderStyle = _formBorderStyle
End Sub
Also, do you really want to return "_color" in the BackColor Get Method instead of MyBase.BackColor?
Are you using this as an Inherited Form? If so, why the designer code? Maybe its just a style preference, but the following makes more sense to me as the color and font setting will be done only once.
VB.Net Code:
Imports System.ComponentModel
Imports System.Drawing
Public Class EditForm2
Inherits Form
Private Shared _color As Color = Color.FromArgb(32, 40, 56)
Private Shared _font As Font = New Font("segoe ui", 8.5)
Private Shared _formBorderStyle As Windows.Forms.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Private Shared _foreColor As Color = Color.FromArgb(128, 144, 160)
Sub New()
SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or Windows.Forms.ControlStyles.SupportsTransparentBackColor Or Windows.Forms.ControlStyles.UserPaint, True)
MyBase.DoubleBuffered = True
MyBase.BackColor = _color
MyBase.ForeColor = _foreColor
MyBase.Font = _font
MyBase.FormBorderStyle = _formBorderStyle
MyBase.ClientSize = New System.Drawing.Size(737, 590)
Would I be correct in assuming the problems lies in the fact that the form has designer functionality, which apparently isn't needed?
Possibly, yes but probably not.
Tracing the source of these types of errors (at least for me) often involves removing code until the error goes away and then trying to figure out why the offending statement is causing it.
The reason I mentioned the Invalidate method as a possible culprit is that it causes the control to be redrawn, but at the point in which it executed, the handle probably has not been created yet. So it is forcing creation at a non optimal time. I really do not have a firm enough grasp on these concepts myself to explain properly, so I will point you to this article for more info.
To help us help you, please indicate what version of Visual Studio you are using and whether it is an Express (free, noncommercial) edition. The information may not be needed, but it can not hurt to provide it.
Edit:
P.S: In case it is not obvious, by setting MyBase.ForeColor the call to Invalidate in your overridden property is not called.
Microsoft Visual Studio Ultimate 2012 is what the About Dialog says.
The reason I was returning _color was for testing purposes, as MyBase gives me trouble sometimes. That was my error. I removed the Invalidate call, as well. The whole point of this class is to set default values so I don't have to set them manually each time I inherit the form.
What you say about invalidating during execution makes sense, but the error wasn't happening consistently. I could rebuild the solution two or three time and the error would stop. It seemed hit and miss. I haven't gotten the error since I made some changes, so I will mark as Solved for now.