1 Attachment(s)
[RESOLVED] Win32Exception out of nowhere.
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)
<Browsable(True), DefaultValue(GetType(Color), "32, 40, 56"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Overrides Property BackColor As Color
Get
Return _color
End Get
Set(value As Color)
MyBase.BackColor = value
End Set
End Property
<Browsable(True), DefaultValue(GetType(Color), "128, 144, 160"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Overrides Property ForeColor As Color
Get
Return MyBase.ForeColor
End Get
Set(value As Color)
If Not MyBase.ForeColor = value Then
MyBase.ForeColor = value
Invalidate()
End If
End Set
End Property
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
Font = _font
FormBorderStyle = _formBorderStyle
End Sub
End Class
VB.NET Code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class EditorForm
Inherits System.Windows.Forms.Form
'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'EditorForm
'
Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(32, Byte), Integer), CType(CType(40, Byte), Integer), CType(CType(56, Byte), Integer))
Me.ClientSize = New System.Drawing.Size(737, 590)
Me.DoubleBuffered = True
Me.Font = New System.Drawing.Font("Segoe UI", 8.5!)
Me.ForeColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(144, Byte), Integer), CType(CType(160, Byte), Integer))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Name = "EditorForm"
Me.ResumeLayout(False)
End Sub
End Class
Attached is a screencap I grabbed of the error.
Re: Win32Exception out of nowhere.
That screen shot is too small/fuzzy to read any of the message, even when expanded. Perhaps you can copy the contents to a post.
Re: Win32Exception out of nowhere.
Or use WindowKey+S and select JUST the part where the error is... the forums here does fnky things with large images...
-tg
Re: Win32Exception out of nowhere.
400+ Posts you should know by now to explain your issue with out us having toi work out what you are doing.
Re: Win32Exception out of nowhere.
Quote:
Originally Posted by
ident
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.
Re: Win32Exception out of nowhere.
Quote:
Originally Posted by
techgnome
Or use WindowKey+S and select JUST the part where the error is... the forums here does fnky things with large images...
-tg
Yes, but sometimes the funky things on the forum are quite dexterous.
Re: Win32Exception out of nowhere.
I apologize for the lack of visual representation in my original post. If I may, I would like to try again.
http://i.imgur.com/PA22bqqs.png
Re: Win32Exception out of nowhere.
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)
<Browsable(True), DefaultValue(GetType(Color), "32, 40, 56"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Overrides Property BackColor As Color
Get
Return _color
End Get
Set(value As Color)
MyBase.BackColor = value
End Set
End Property
<Browsable(True), DefaultValue(GetType(Color), "128, 144, 160"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Overrides Property ForeColor As Color
Get
Return MyBase.ForeColor
End Get
Set(value As Color)
If Not MyBase.ForeColor = value Then
MyBase.ForeColor = value
Invalidate()
End If
End Set
End Property
Sub New()
' 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.
Re: Win32Exception out of nowhere.
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)
End Sub
<Browsable(True), DefaultValue(GetType(Color), "32, 40, 56"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Overrides Property BackColor() As Color
Get
Return MyBase.BackColor
'Return _color
End Get
Set(ByVal value As Color)
MyBase.BackColor = value
End Set
End Property
<Browsable(True), DefaultValue(GetType(Color), "128, 144, 160"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Overrides Property ForeColor() As Color
Get
Return MyBase.ForeColor
End Get
Set(ByVal value As Color)
If Not MyBase.ForeColor = value Then
MyBase.ForeColor = value
Me.Invalidate()
End If
End Set
End Property
End Class
Re: Win32Exception out of nowhere.
Would I be correct in assuming the problems lies in the fact that the form has designer functionality, which apparently isn't needed?
Re: Win32Exception out of nowhere.
Quote:
Originally Posted by
Troy Lundin
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.
All About Handles in Windows Forms
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.
Re: Win32Exception out of nowhere.
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.
Thank you for your help.