Results 1 to 12 of 12

Thread: [RESOLVED] Win32Exception out of nowhere.

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [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:
    1. Imports System.ComponentModel
    2. Imports System.Drawing
    3.  
    4. Public Class EditorForm
    5.  
    6.     Private _color As Color = Color.FromArgb(32, 40, 56)
    7.     Private _font As Font = New Font("segoe ui", 8.5)
    8.     Private _formBorderStyle As Windows.Forms.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    9.     Private _foreColor As Color = Color.FromArgb(128, 144, 160)
    10.  
    11.  
    12.     <Browsable(True), DefaultValue(GetType(Color), "32, 40, 56"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    13.     Overrides Property BackColor As Color
    14.         Get
    15.             Return _color
    16.         End Get
    17.         Set(value As Color)
    18.             MyBase.BackColor = value
    19.         End Set
    20.     End Property
    21.  
    22.     <Browsable(True), DefaultValue(GetType(Color), "128, 144, 160"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    23.     Overrides Property ForeColor As Color
    24.         Get
    25.             Return MyBase.ForeColor
    26.         End Get
    27.         Set(value As Color)
    28.             If Not MyBase.ForeColor = value Then
    29.                 MyBase.ForeColor = value
    30.                 Invalidate()
    31.             End If
    32.         End Set
    33.     End Property
    34.  
    35.     Sub New()
    36.  
    37.         ' This call is required by the designer.
    38.         InitializeComponent()
    39.  
    40.         ' Add any initialization after the InitializeComponent() call.
    41.         SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or Windows.Forms.ControlStyles.SupportsTransparentBackColor Or Windows.Forms.ControlStyles.UserPaint, True)
    42.         DoubleBuffered = True
    43.  
    44.         BackColor = _color
    45.         ForeColor = _foreColor
    46.  
    47.         Font = _font
    48.         FormBorderStyle = _formBorderStyle
    49.     End Sub
    50.  
    51. End Class

    VB.NET Code:
    1. <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    2. Partial Class EditorForm
    3.     Inherits System.Windows.Forms.Form
    4.  
    5.     'UserControl overrides dispose to clean up the component list.
    6.     <System.Diagnostics.DebuggerNonUserCode()> _
    7.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    8.         Try
    9.             If disposing AndAlso components IsNot Nothing Then
    10.                 components.Dispose()
    11.             End If
    12.         Finally
    13.             MyBase.Dispose(disposing)
    14.         End Try
    15.     End Sub
    16.  
    17.     'Required by the Windows Form Designer
    18.     Private components As System.ComponentModel.IContainer
    19.  
    20.     'NOTE: The following procedure is required by the Windows Form Designer
    21.     'It can be modified using the Windows Form Designer.  
    22.     'Do not modify it using the code editor.
    23.     <System.Diagnostics.DebuggerStepThrough()> _
    24.     Private Sub InitializeComponent()
    25.         Me.SuspendLayout()
    26.         '
    27.         'EditorForm
    28.         '
    29.         Me.BackColor = System.Drawing.Color.FromArgb(CType(CType(32, Byte), Integer), CType(CType(40, Byte), Integer), CType(CType(56, Byte), Integer))
    30.         Me.ClientSize = New System.Drawing.Size(737, 590)
    31.         Me.DoubleBuffered = True
    32.         Me.Font = New System.Drawing.Font("Segoe UI", 8.5!)
    33.         Me.ForeColor = System.Drawing.Color.FromArgb(CType(CType(128, Byte), Integer), CType(CType(144, Byte), Integer), CType(CType(160, Byte), Integer))
    34.         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
    35.         Me.Name = "EditorForm"
    36.         Me.ResumeLayout(False)
    37.  
    38.     End Sub
    39.  
    40. End Class

    Attached is a screencap I grabbed of the error.
    Attached Images Attached Images  
    Prefix has no suffix, but suffix has a prefix.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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.
    My usual boring signature: Nothing

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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.
    My Github - 1d3nt

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Win32Exception out of nowhere.

    Quote Originally Posted by ident View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Win32Exception out of nowhere.

    Quote Originally Posted by techgnome View Post
    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.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.

    Prefix has no suffix, but suffix has a prefix.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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:
    1. Imports System.ComponentModel
    2.  
    3. 'Prevents the Forms designer from loading
    4. 'as this Form is a base class from which your
    5. 'application's Forms can inherit, it's not meant to be designed upon
    6. <ToolboxItem(False)> _
    7. Public Class BlackComponent
    8.     Inherits Component
    9. End Class
    10.  
    11. Public Class EditorFormBase
    12.     Inherits Form
    13.  
    14.     Private _color As Color = Color.FromArgb(32, 40, 56)
    15.     Private _font As Font = New Font("segoe ui", 8.5)
    16.     Private _formBorderStyle As Windows.Forms.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    17.     Private _foreColor As Color = Color.FromArgb(128, 144, 160)
    18.  
    19.  
    20.     <Browsable(True), DefaultValue(GetType(Color), "32, 40, 56"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    21.     Overrides Property BackColor As Color
    22.         Get
    23.             Return _color
    24.         End Get
    25.         Set(value As Color)
    26.             MyBase.BackColor = value
    27.         End Set
    28.     End Property
    29.  
    30.     <Browsable(True), DefaultValue(GetType(Color), "128, 144, 160"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    31.     Overrides Property ForeColor As Color
    32.         Get
    33.             Return MyBase.ForeColor
    34.         End Get
    35.         Set(value As Color)
    36.             If Not MyBase.ForeColor = value Then
    37.                 MyBase.ForeColor = value
    38.                 Invalidate()
    39.             End If
    40.         End Set
    41.     End Property
    42.  
    43.     Sub New()
    44.  
    45.         ' Add any initialization after the InitializeComponent() call.
    46.         SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or Windows.Forms.ControlStyles.SupportsTransparentBackColor Or Windows.Forms.ControlStyles.UserPaint, True)
    47.         DoubleBuffered = True
    48.  
    49.         BackColor = _color
    50.         ForeColor = _foreColor
    51.  
    52.         Font = _font
    53.         FormBorderStyle = _formBorderStyle
    54.     End Sub
    55.  
    56.  
    57.  
    58.  
    59.  
    60. 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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    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

  9. #9
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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:
    1. Imports System.ComponentModel
    2. Imports System.Drawing
    3. Public Class EditForm2
    4.    Inherits Form
    5.  
    6.    Private Shared _color As Color = Color.FromArgb(32, 40, 56)
    7.    Private Shared _font As Font = New Font("segoe ui", 8.5)
    8.    Private Shared _formBorderStyle As Windows.Forms.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    9.    Private Shared _foreColor As Color = Color.FromArgb(128, 144, 160)
    10.  
    11.    Sub New()
    12.       SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer Or Windows.Forms.ControlStyles.SupportsTransparentBackColor Or Windows.Forms.ControlStyles.UserPaint, True)
    13.       MyBase.DoubleBuffered = True
    14.       MyBase.BackColor = _color
    15.       MyBase.ForeColor = _foreColor
    16.       MyBase.Font = _font
    17.       MyBase.FormBorderStyle = _formBorderStyle
    18.       MyBase.ClientSize = New System.Drawing.Size(737, 590)
    19.    End Sub
    20.  
    21.  
    22.    <Browsable(True), DefaultValue(GetType(Color), "32, 40, 56"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    23.    Public Overrides Property BackColor() As Color
    24.       Get
    25.          Return MyBase.BackColor
    26.          'Return _color
    27.       End Get
    28.       Set(ByVal value As Color)
    29.           MyBase.BackColor = value
    30.       End Set
    31.    End Property
    32.  
    33.    <Browsable(True), DefaultValue(GetType(Color), "128, 144, 160"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
    34.    Overrides Property ForeColor() As Color
    35.       Get
    36.           Return MyBase.ForeColor
    37.       End Get
    38.       Set(ByVal value As Color)
    39.           If Not MyBase.ForeColor = value Then
    40.               MyBase.ForeColor = value
    41.               Me.Invalidate()
    42.           End If
    43.       End Set
    44.    End Property
    45. End Class

  10. #10

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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?
    Prefix has no suffix, but suffix has a prefix.

  11. #11
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Win32Exception out of nowhere.

    Quote Originally Posted by Troy Lundin View Post
    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.

  12. #12

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.
    Prefix has no suffix, but suffix has a prefix.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width