'From: Pete (stroudpete@hotmail.com)
'Subject: An unhandled exception of type 'System.ArithmeticException' -
'SOLUTION 
'Newsgroups: microsoft.public.dotnet.languages.vb
'Date: 2002-10-17 06:50:56 PST 
'
'I was getting the following error in my small VB.net app :
'
'An unhandled exception of type 'System.ArithmeticException' occurred
'in system.drawing.dll
'Additional information: Overflow or underflow in the arithmetic
'operation.
'
'I am new to this (I've come from a Cobol background) and only been
'playing around for a couple of weeks, so forgive me if my terminology
'falls down. I've searched with google and tracked down what was
'happening from other posts of this problem, the MSKB report, and
'working out how to translate C++ into VB. Anyway, here's my take on
'what happened, why, and how to solve it.
'
'This can happen on a statement used to set a font, typically inserted
'by Windows Form Designer, such as this one:
'Me.lstBox.Font = New System.Drawing.Font("Microsoft Sans Serif",
'9.75!, System.Drawing.FontStyle.Regular,
'System.Drawing.GraphicsUnit.Point, CType(0, Byte))
'
'There is a bug in vb.net that was causing the proble. The bug is
'acknowledged by Microsoft Knowledge Base Article - Q326219
'(http://support.microsoft.com/default.aspx?scid=KB;EN-US;q326219&)
'
'It is caused by "Floating Point Control Register" being changed by
'other code; that code should reset it to it's default state, but
'hasn't
'
'In the case where I encountered it, it was an OleDbDataReader object
'that caused it, and it also happened with the SqlDataReader object
'when I converted the code from Access to SqlServer data
'
'This code fixed the problem. I simply put DotNetBugFix() just before
'enstanciating a new ListBox object that contained the above Font code
'in the InitializeComponent code called by the constructor; it resets
'the FPC Register (Word) to its default value, as expected by .Net
'framework (see Q326219, note that the C++ constant _CW_DEFAULT is hex
'9001F or decimal 589855)
'
'(in main form ....)
' do the fix
'        ResetFPCR()
' create object with the "offending" font
' statement in the constructor
'Dim dList As New dlgList()
'
'(in a separate module, for ease of inclusion in other projects ....)

Imports System.Runtime.InteropServices

Module DotNetBugFix

  <DllImport("msvcr70.dll", _
              CallingConvention:=CallingConvention.Cdecl)> _
              Function _controlfp(ByVal n As Integer, ByVal mask As Integer) _
              As Integer
  End Function

  Public Sub ResetFPCR()
    ' reset the FPC Register (Word) to its default value, 
    ' as expected by .Net framework 
    '
    _controlfp(&H9001F, &HFFFFF)
    '
  End Sub

End Module

'I hope this helps a few people out!

'Pete Jones
'Swift Systems Consultancy Ltd

