locked
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll RRS feed

  • Question

  • I've read a bit about this error and maybe I have nothing to worry about.  BUT ... my simple little app gets 7 of these before it even gets to the first Dim statement in the Class Form1!  I found and followed instructions on how to have the Debugger intercept these exceptions but it seems that they are not being intercepted.  I only know that they have happened because of the messages in the Immediate Window.  In the Designer all I have is 5 Labels and 5 TextBoxes.  

    Any thoughts on how I could get to the cause of these messages?

    Thanks,  Bob

    Sunday, August 29, 2010 3:47 AM

Answers

  • I've read a bit about this error and maybe I have nothing to worry about.  BUT ... my simple little app gets 7 of these before it even gets to the first Dim statement in the Class Form1!  I found and followed instructions on how to have the Debugger intercept these exceptions but it seems that they are not being intercepted.  I only know that they have happened because of the messages in the Immediate Window.  In the Designer all I have is 5 Labels and 5 TextBoxes.  

    Any thoughts on how I could get to the cause of these messages?

    Thanks,  Bob


    Exceptions are NOT necessarily errors. They might happen from time to time during the running of your application. As long as some code (either yours or the .NET Framework's) handle the exceptions, your application will just be fine.

    Usually you don't need to know the cause of these exceptions.

    If you do want to know, in Visual Studio -> Debug (main menu) -> Exceptions dialog, expand to the node Common Lanuage Runtime Exceptions / System / System.ArgumentException, and check the 'Thrown' CheckBox. This will cause the debugger to break then a first chance ArgumentException is happens, and then you can see more information in the exception dialog.

    For more information about first and second chance exceptions please refer to this KB article:
    http://support.microsoft.com/kb/105675

    Regards,
    Jie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Proposed as answer by Jing0 Wednesday, September 8, 2010 3:06 AM
    • Marked as answer by Aland Li Wednesday, September 8, 2010 3:51 AM
    Thursday, September 2, 2010 10:41 AM

All replies

  • When you say 

    " I found and followed instructions on how to have the Debugger intercept these exceptions but it seems that they are not being intercepted."

    do you mean you checked the option Break when Exception is Thrown in Exceptions tool window (Debug-> Exceptions) for the Common Language Runtime Exceptions ?
    That should stop debugger at the line in your code that's causing the exception.

    If that didn't help, could you post the code for your form so we can reproduce the problem ?

    Best regards,
    Vladimir

     

     

    Sunday, August 29, 2010 8:18 AM
  • Hi Vladimir,

    Thanks for offering to help.  Yes I have checked the option Break when Exception is Thrown. 

    Here's the code, but the exceptions are happening before any of my code gets executed.

    Option Strict On
    Option Explicit On
    
    Imports System.Drawing.Drawing2D
    Imports System.Math
    
    ' The cells have a 1 pixel border and there is a 1 pixel border around all of the cells. 
    ' So that results in a 2 pixel between and around all cells.
    
    ' Cell n,m has an X location of (n*cellwidth) + 1 and a Y location of (m*cellheight)+1.
    
    ' To determine cell co-ordinates from the mouse location (x-1) / cellwidth and
    ' (y-1) / cellheight.
    
    
    
    Public Class Form1
    
      Dim GridX As Integer = 30
      Dim GridY As Integer = 10
    
      Dim CellSize As New Size(20, 30) ' size of each cell in pixels
    
      Dim GridSize As New Size(GridX, GridY) ' grid size in cells
    
      Dim GridOffset As New Point(50, 60) ' offset of grid within form
    
      Dim BorderRect As New Rectangle(GridOffset, New Size((CellSize.Width * GridSize.Width) + 1, _
      (CellSize.Height * GridSize.Height) + 1))
    
      'fg.DrawLine(mypen, GridOffset, _
      '          New Point(GridOffset.X + (CellSize.Width * GridSize.Width) + 3, GridOffset.Y)) ' 3 probably wrong
      '    fg.DrawLine(mypen, New Point(GridOffset.X, GridOffset.Y + (CellSize.Height * GridSize.Height)), _
      '          New Point(GridOffset.X + (CellSize.Width * GridSize.Width) + 3, _
      '               GridOffset.Y + (CellSize.Height * GridSize.Height))) ' 3 probably wrong
    
      Dim mousepixXloc As Integer
      Dim mousepixYloc As Integer
      'Dim MouseCellXLoc As Integer
      'Dim MouseCellYLoc As Integer
    
      Dim CurMouseCellLoc As New Point ' which cell the mouse is in; if it's not in a cell then -1, -1
    
      Dim LastCellLoc As New Point(-1, -1)
    
      Dim BadCellLoc As New Point(-1, -1)
    
      Dim CellInfo(GridX, GridY) As CellInformation
    
      Dim cntxmuCell As New ContextMenuStrip
    
      Dim SavedCursorLoc As New Point
    
    
    
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        'Me.Width = (GridOffset.X * 2) + (GridSize.Width * CellSize.Width)
        'Me.Height = (GridOffset.X * 2) + (GridSize.Width * CellSize.Width)
    
        AddHandler Me.Paint, AddressOf FormPaint
        AddHandler Me.MouseMove, AddressOf MouseMovement
    
        cntxmuCell.TopLevel = False '??????????? WHY IS THIS NEEDED ??????????????????
        Me.Controls.Add(cntxmuCell)
        cntxmuCell.Enabled = False
        ''AddHandler tsiOccupy.click, AddressOf tsiOccupy_click
    
      End Sub
    
      Sub MouseMovement(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        'MouseCellXLoc = CInt(Math.Truncate(((e.X - 1) - GridOffset.X) / CellSize.Width))
        'MouseCellYLoc = CInt(Math.Truncate(((e.Y - 1) - GridOffset.Y) / CellSize.Height))
        'tbxMouseXY.Text = e.X.ToString & " / " & e.Y.ToString
        'tbxCellXY.Text = MouseCellXLoc.ToString & " / " & MouseCellYLoc.ToString
        'If LastCellLoc.X = MouseCellXLoc AndAlso LastCellLoc.Y = MouseCellYLoc Then Exit Sub
        'If MouseCellXLoc < 0 OrElse MouseCellXLoc > (GridSize.Width - 1) Then Exit Sub
        'If MouseCellYLoc < 0 OrElse MouseCellYLoc > (GridSize.Height - 1) Then Exit Sub
    
        CurMouseCellLoc = GetCurMouseCellLoc(e)
        'otherwise, i.e. if mouse is within a cell in our grid ...
        'LastCellLoc.X = MouseCellXLoc
        'LastCellLoc.Y = MouseCellYLoc
    
        If CurMouseCellLoc.Equals(LastCellLoc) Then
          Return
        End If
    
    
        LastCellLoc.X = CurMouseCellLoc.X
        LastCellLoc.Y = CurMouseCellLoc.Y
        Me.Invalidate()
    
      End Sub
    
      Function GetCurMouseCellLoc(ByVal e As System.Windows.Forms.MouseEventArgs) As Point
    
        GetCurMouseCellLoc.X = CInt(Math.Truncate(((e.X - 1) - GridOffset.X) / CellSize.Width))
        GetCurMouseCellLoc.Y = CInt(Math.Truncate(((e.Y - 1) - GridOffset.Y) / CellSize.Height))
        If GetCurMouseCellLoc.X < 0 OrElse GetCurMouseCellLoc.X > (GridSize.Width - 1) OrElse _
        GetCurMouseCellLoc.Y < 0 OrElse GetCurMouseCellLoc.Y > (GridSize.Height - 1) Then
          GetCurMouseCellLoc.X = -1
          GetCurMouseCellLoc.Y = -1
        End If
        tbxMouseXY.Text = e.X.ToString & " / " & e.Y.ToString
        tbxMouseAXY.Text = (e.X - GridOffset.X).ToString & " / " & (e.Y - GridOffset.Y).ToString
        tbxCellXY.Text = GetCurMouseCellLoc.X.ToString & " / " & GetCurMouseCellLoc.Y.ToString
      End Function
    
      Sub FormPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    
        Using fg As Graphics = Me.CreateGraphics, _
        mypen As Pen = New Pen(Color.Black)
          mypen.Width = 1
    
          ' draw grid
          ' draw the border around all of the cells
          fg.DrawRectangle(mypen, BorderRect)
    
          ' horizontal lines
          For i As Integer = 0 To GridSize.Height - 1
            'top line
            fg.DrawLine(mypen, GridOffset.X + 1, GridOffset.Y + 1 + (i * CellSize.Height), _
                  GridOffset.X + 1 + (CellSize.Width * GridSize.Width), _
                  (i * CellSize.Height) + GridOffset.Y + 1)
            'bottom line
            fg.DrawLine(mypen, GridOffset.X + 1, GridOffset.Y + 1 + (i * CellSize.Height) + CellSize.Height - 1, _
                  GridOffset.X + 1 + (CellSize.Width * GridSize.Width), _
                  (i * CellSize.Height) + CellSize.Height - 1 + GridOffset.Y + 1)
            'MsgBox("?")
          Next i
    
          ' vertical lines
          For i As Integer = 0 To GridSize.Width - 1
            'left line
            fg.DrawLine(mypen, GridOffset.X + 1 + (i * CellSize.Width), GridOffset.Y + 1, _
                  GridOffset.X + 1 + (i * CellSize.Width), _
                  (GridSize.Height * CellSize.Height) + GridOffset.Y + 1)
            'right line
            fg.DrawLine(mypen, GridOffset.X + 1 + (i * CellSize.Width) + CellSize.Width - 1, GridOffset.Y + 1, _
                  GridOffset.X + 1 + (i * CellSize.Width) + CellSize.Width - 1, _
                  (GridSize.Height * CellSize.Height) + GridOffset.Y + 1)
            'MsgBox("?")
          Next i
    
          'identify mouse location
          'fg.FillRectangle(Brushes.Aquamarine, (MouseCellXLoc * CellSize.Width) + GridOffset.X + 2, _
          '         (MouseCellYLoc * CellSize.Height) + GridOffset.Y + 2, _
          '         CellSize.Width - 2, CellSize.Height - 2)
    
          If CurMouseCellLoc <> BadCellLoc Then
            fg.FillRectangle(Brushes.Aquamarine, (CurMouseCellLoc.X * CellSize.Width) + GridOffset.X + 2, _
                           (CurMouseCellLoc.Y * CellSize.Height) + GridOffset.Y + 2, _
                           CellSize.Width - 2, CellSize.Height - 2)
          End If
        End Using
    
      End Sub
    
    
      Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
    
        'If e.Button = Windows.Forms.MouseButtons.Right Then
        '  cntxmuCell.Items.Add("Occupy Cell")
        '  AddHandler cntxmuCell.Closed, AddressOf cntxmuCell_Closed
        '  'AddHandler cntxmuCell.Click, AddressOf cntxmucell_Click
        '  cntxmuCell.Enabled = True
        '  cntxmuCell.Show(Me, e.Location)
        '  SavedCursorLoc.X = e.Location.X ' save cursor loc so that it can be restored when menu item is closed
        '  SavedCursorLoc.Y = e.Location.Y
        '  tbxMouseLocSaved.Text = SavedCursorLoc.X.ToString & " / " & SavedCursorLoc.Y.ToString
        'End If
    
      End Sub
    
      Sub cntxmuCell_Closed(ByVal sender As Object, ByVal e As ToolStripDropDownClosedEventArgs)
    
        Dim temppoint As Point = Cursor.Position
    
        temppoint.X = SavedCursorLoc.X
        temppoint.Y = SavedCursorLoc.Y
    
        Cursor.Position = temppoint
        tbxRestored.Text = Cursor.Position.X.ToString & " / " & Cursor.Position.Y.ToString
      End Sub
    
    End Class
    Class CellInformation
      Public Occupied As Boolean = False
      Dim Closest(7) As Point ' if all surrounding cells are occupied then a cell can have 8 closest neighbors
    End Class
    
    

    The "Designer" file is ...

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Partial Class Form1
      Inherits System.Windows.Forms.Form
    
      'Form 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.Label1 = New System.Windows.Forms.Label
        Me.tbxMouseXY = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.tbxCellXY = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.tbxMouseLocSaved = New System.Windows.Forms.TextBox
        Me.Label4 = New System.Windows.Forms.Label
        Me.tbxRestored = New System.Windows.Forms.TextBox
        Me.Label5 = New System.Windows.Forms.Label
        Me.tbxMouseAXY = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(10, 29)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(61, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Mouse X/Y"
        '
        'tbxMouseXY
        '
        Me.tbxMouseXY.Location = New System.Drawing.Point(77, 26)
        Me.tbxMouseXY.Name = "tbxMouseXY"
        Me.tbxMouseXY.Size = New System.Drawing.Size(58, 20)
        Me.tbxMouseXY.TabIndex = 1
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(388, 29)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(46, 13)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Cell X/Y"
        '
        'tbxCellXY
        '
        Me.tbxCellXY.Location = New System.Drawing.Point(452, 26)
        Me.tbxCellXY.Name = "tbxCellXY"
        Me.tbxCellXY.Size = New System.Drawing.Size(55, 20)
        Me.tbxCellXY.TabIndex = 3
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(543, 26)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(41, 13)
        Me.Label3.TabIndex = 4
        Me.Label3.Text = "Saved:"
        '
        'tbxMouseLocSaved
        '
        Me.tbxMouseLocSaved.Location = New System.Drawing.Point(591, 26)
        Me.tbxMouseLocSaved.Name = "tbxMouseLocSaved"
        Me.tbxMouseLocSaved.Size = New System.Drawing.Size(81, 20)
        Me.tbxMouseLocSaved.TabIndex = 5
        '
        'Label4
        '
        Me.Label4.AutoSize = True
        Me.Label4.Location = New System.Drawing.Point(680, 29)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(50, 13)
        Me.Label4.TabIndex = 6
        Me.Label4.Text = "Restored"
        '
        'tbxRestored
        '
        Me.tbxRestored.Location = New System.Drawing.Point(736, 29)
        Me.tbxRestored.Name = "tbxRestored"
        Me.tbxRestored.Size = New System.Drawing.Size(70, 20)
        Me.tbxRestored.TabIndex = 7
        '
        'Label5
        '
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(157, 29)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(105, 13)
        Me.Label5.TabIndex = 8
        Me.Label5.Text = "Mouse Adjusted X/Y"
        '
        'tbxMouseAXY
        '
        Me.tbxMouseAXY.Location = New System.Drawing.Point(269, 29)
        Me.tbxMouseAXY.Name = "tbxMouseAXY"
        Me.tbxMouseAXY.Size = New System.Drawing.Size(65, 20)
        Me.tbxMouseAXY.TabIndex = 9
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(821, 694)
        Me.Controls.Add(Me.tbxMouseAXY)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.tbxRestored)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.tbxMouseLocSaved)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.tbxCellXY)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.tbxMouseXY)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Nearest Neighbor"
        Me.ResumeLayout(False)
        Me.PerformLayout()
    
      End Sub
      Friend WithEvents Label1 As System.Windows.Forms.Label
      Friend WithEvents tbxMouseXY As System.Windows.Forms.TextBox
      Friend WithEvents Label2 As System.Windows.Forms.Label
      Friend WithEvents tbxCellXY As System.Windows.Forms.TextBox
      Friend WithEvents Label3 As System.Windows.Forms.Label
      Friend WithEvents tbxMouseLocSaved As System.Windows.Forms.TextBox
      Friend WithEvents Label4 As System.Windows.Forms.Label
      Friend WithEvents tbxRestored As System.Windows.Forms.TextBox
      Friend WithEvents Label5 As System.Windows.Forms.Label
      Friend WithEvents tbxMouseAXY As System.Windows.Forms.TextBox
    
    End Class
    
    

     There are five Labels on the form, their names don't matter, and five TextBoxes, tbxMouseXY, tbxMouseAXY, tbxCellXY, tbxMouseLocSaved, and tbxRestored.

    I greatly appreciate your help.

    Bob

    Sunday, August 29, 2010 4:44 PM
  • I can't reproduce the error with this code ... no exceptions are raised ... which version of .net framework are you targeting ?

     

    BTW, I just scanned the code a bit and you re not supposed to add contextmenustrip to the Controls collection. Add one in designer and look at the generated code. Then do the same if you don't want to create it in the designer. ContextMenuStrip is TopLevel window, it's not a child control of the form and if you click on the form corner to invoke it you will see that it pops out of the form bounds.

    Second, don't create a new Graphics object in the Paint event handler, you already get the object use for drawing in the PaintEventArgs.

      Sub FormPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    
        Using mypen As Pen = New Pen(Color.Black)
          mypen.Width = 1
    
          Dim fg As Graphics = e.Graphics
          ' draw grid
          ' draw the border around all of the cells
          fg.DrawRectangle(mypen, BorderRect)
    
          ' the rest of your code ... 
    
        End Using
    
      End Sub
    

     

    Third, there is no need to redraw the grid on every mouse move ... let the system invalidate form when necessary. If you are only selecting a different cell in the grid all you need to do is erase previous selection and draw the new one.

      Sub MouseMovement(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    
        CurMouseCellLoc = GetCurMouseCellLoc(e)
        'otherwise, i.e. if mouse is within a cell in our grid ...
        'LastCellLoc.X = MouseCellXLoc
        'LastCellLoc.Y = MouseCellYLoc
    
        If CurMouseCellLoc.Equals(LastCellLoc) Then
          Return
        End If
    
    
        Using fg As Graphics = Me.CreateGraphics(), backColorBrush As Brush = New SolidBrush(Me.BackColor)
          'erase last seletced cell
          fg.FillRectangle(backColorBrush, (LastCellLoc.X * CellSize.Width) + GridOffset.X + 2, _
            (LastCellLoc.Y * CellSize.Height) + GridOffset.Y + 2, _
            CellSize.Width - 2, CellSize.Height - 2)
          ' select new cell
          If (CurMouseCellLoc.X >= 0 AndAlso CurMouseCellLoc.Y >= 0) Then
            fg.FillRectangle(Brushes.Aquamarine, (CurMouseCellLoc.X * CellSize.Width) + GridOffset.X + 2, _
              (CurMouseCellLoc.Y * CellSize.Height) + GridOffset.Y + 2, _
              CellSize.Width - 2, CellSize.Height - 2)
          End If
        End Using
    
    
        LastCellLoc.X = CurMouseCellLoc.X
        LastCellLoc.Y = CurMouseCellLoc.Y
        'Me.Invalidate()
    
      End Sub
    
    

    and with this code you won't have all that flickering you get when you move the mouse around. You need to correct the code for getting the current cell when mouse is above or left of the grid and offset is less then a cell height/width.

    Best regards,
    Vladimir

    Sunday, August 29, 2010 6:11 PM
  • Vladimir,

    Advanced Compiler Settings > Target framework (all configurations): .NET Framework 3.5

    I'm using VS 2008 Version 9.0.30729.1 SP / .NET Framework Version 3.5 SP1

    And, by the way, this is an x64 system with an AMD CPU if that might matter.

    Thank you VERY much for looking over the code and pointing out my several mistakes.  I haven't done a lot of graphics programming so I really had no idea I was making those mistakes.

    I was confused about the ContextMenuStrip problem I had and solved it using advice I found somewhere on the Internet.  I'll take your advice and see how the Designer handles it.  But I am still confused about this TopLevel stuff and will probably soon start a new thread asking for help in understanding that.  

    Again, thank you VERY much.  (Don't drive yourself crazy with the "first chance exceptions."  I can live with them if I have to.)

    Bob

     

    Monday, August 30, 2010 4:08 AM
  • You're welcome,

    I got the same VS / .Net framework, but OS is Vista Business SP1 32 bit, with the latest updates installed.

    Maybe someone will have better luck reproducing it on a 64 bit OS. 

    Monday, August 30, 2010 6:56 AM
  • I've read a bit about this error and maybe I have nothing to worry about.  BUT ... my simple little app gets 7 of these before it even gets to the first Dim statement in the Class Form1!  I found and followed instructions on how to have the Debugger intercept these exceptions but it seems that they are not being intercepted.  I only know that they have happened because of the messages in the Immediate Window.  In the Designer all I have is 5 Labels and 5 TextBoxes.  

    Any thoughts on how I could get to the cause of these messages?

    Thanks,  Bob


    Exceptions are NOT necessarily errors. They might happen from time to time during the running of your application. As long as some code (either yours or the .NET Framework's) handle the exceptions, your application will just be fine.

    Usually you don't need to know the cause of these exceptions.

    If you do want to know, in Visual Studio -> Debug (main menu) -> Exceptions dialog, expand to the node Common Lanuage Runtime Exceptions / System / System.ArgumentException, and check the 'Thrown' CheckBox. This will cause the debugger to break then a first chance ArgumentException is happens, and then you can see more information in the exception dialog.

    For more information about first and second chance exceptions please refer to this KB article:
    http://support.microsoft.com/kb/105675

    Regards,
    Jie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Proposed as answer by Jing0 Wednesday, September 8, 2010 3:06 AM
    • Marked as answer by Aland Li Wednesday, September 8, 2010 3:51 AM
    Thursday, September 2, 2010 10:41 AM