Link to home
Start Free TrialLog in
Avatar of AlisonBallard
AlisonBallard

asked on

On Panel.hide - An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll .NETCF

An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll
Everytime I try to hide one particular panel in my application I am getting the error above
I have several other panels which I am showing and hiding throughout the application without any problems - I have tried creating a new panel and copying the controls from the old one into the new one ... I'm losing my mind here!  This was working and I don't know why it isn't now ...
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

It's kind of difficult to troubleshoot these kinds of errors over the wire without code or more information to explain what you are trying to do.

What .NET version are you working with?

Bob
Avatar of AlisonBallard
AlisonBallard

ASKER

Sorry ... I think its nothing to do with hiding the panel ... I am using a signature control which I downloaded ... it wa working fine ... However when I hide my panels and want to display the Signature Capture section I get the error.  I'm developing for Windows Mob 2003 Pocket PC (using VB.NET CF)

The code for the control is as follows :
***********************************************
'      SignatureControl class
'      --
'      Collects and displays a signature. The signature is made up of
'      a list of line segments. Each segment contains an array of points
'      (x and y coordinates).
'
'      Draws to a memory bitmap to prevent flickering.
'
'      Raises the SignatureUpdate event when a new segment is added to
'      the signature.


' Custom control that collects and displays a signature.
Public Class SignatureControl
      Inherits Control

      ' gdi objects
      Private _bmp As Bitmap
      Private _graphics As Graphics
      Private _pen As New Pen(Color.Black)

      ' list of line segments
      Private _lines As New ArrayList

      ' the current line segment
      Private _points As New ArrayList
      Private _lastPoint As New Point(0, 0)

      ' if drawing signature or not
      Private _collectPoints As Boolean = False

      ' notify parent that line segment was updated
      Public Event SignatureUpdate As EventHandler

      ' List of signature line segments.
      Public ReadOnly Property Lines() As ArrayList
            Get
                  Return _lines
            End Get
      End Property

      ' Return the signature flattened to a stream of bytes.
      Public ReadOnly Property SignatureBits() As Byte()
            Get
                  Return SignatureData.GetBytes(Me.Width, Me.Height, _lines)
            End Get
      End Property

      Public Sub New()
      End Sub

      Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            ' blit the memory bitmap to the screen
            ' we draw on the memory bitmap on mousemove so there
            ' is nothing else to draw at this time (the memory
            ' bitmap already contains all of the lines)
            CreateGdiObjects()
            e.Graphics.DrawImage(_bmp, 0, 0)
      End Sub

      Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
            ' don't pass to base since we paint everything, avoid flashing
      End Sub

      Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
            MyBase.OnMouseDown(e)

            ' process if currently drawing signature
            If Not _collectPoints Then
                  ' start collecting points
                  _collectPoints = True

                  ' use current mouse click as the first point
                  _lastPoint.X = e.X
                  _lastPoint.Y = e.Y

                  ' this is the first point in the list
                  _points.Clear()
                  _points.Add(_lastPoint)
            End If
      End Sub


      Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
            MyBase.OnMouseUp(e)

            ' process if drawing signature
            If _collectPoints Then
                  ' stop collecting points
                  _collectPoints = False

                  ' add current line to list of segments
                  Dim points(_points.Count - 1) As Point
                  Dim i As Integer
                  For i = 0 To _points.Count - 1
                        Dim pt As Point = CType(_points(i), Point)
                        points(i).X = pt.X
                        points(i).Y = pt.Y
                  Next i

                  _lines.Add(points)

                  ' start over with a new line
                  _points.Clear()

                  ' notify container a new segment was added
                  RaiseSignatureUpdateEvent()
            End If
      End Sub

      Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
            MyBase.OnMouseMove(e)

            ' process if drawing signature
            If _collectPoints Then
                  ' add point to current line segment
                  _points.Add(New Point(e.X, e.Y))

                  ' draw the new segment on the memory bitmap
                  _graphics.DrawLine(_pen, _lastPoint.X, _lastPoint.Y, e.X, e.Y)

                  ' update the current position
                  _lastPoint.X = e.X
                  _lastPoint.Y = e.Y

                  ' display the updated bitmap
                  Invalidate()
            End If
      End Sub

      ' Clear the signature.
      Public Sub Clear()
            _lines.Clear()
            InitMemoryBitmap()
            Invalidate()
      End Sub

      ' Create any GDI objects required to draw signature.
      Private Sub CreateGdiObjects()
            ' only create if don't have one or the size changed
            If _bmp Is Nothing Then
                  InitMemoryBitmap()
            ElseIf _bmp.Width <> Me.Width Or _bmp.Height <> Me.Height Then
                  InitMemoryBitmap()
            End If
      End Sub

      ' Create a memory bitmap that is used to draw the signature.
      Private Sub InitMemoryBitmap()
            ' load the background image
            _bmp = Global.LoadImage("sign here.png")

            ' get graphics object now to make drawing during mousemove faster
            _graphics = Graphics.FromImage(_bmp)
      End Sub

      ' Notify container that a line segment has been added.
      Private Sub RaiseSignatureUpdateEvent()
            RaiseEvent SignatureUpdate(Me, EventArgs.Empty)
      End Sub
End Class
***********************************************


I Add the control to my form as follows

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.IO
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Runtime.InteropServices
Imports FieldSoftware.PrinterCE_NetCF

Public Class MainFrm

    Inherits System.Windows.Forms.Form
...
    ' custom signature control
    Private WithEvents _signature As SignatureControl = New SignatureControl
...

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        'add signature control to form


        _signature.Location = areaSignature.Location
        _signature.Size = areaSignature.Size
         Me.Controls.Add(_signature)

End Sub
(areaSignature is a panel with    
How are you "hiding" the panel, and showing the Capture section?

Bob
I'm calling :
Transpanel.hide
The capture section is visible and is behind the 'TransPanel' located directly on the form
I still can't see anything leaping off the page, yet.

Capture section = SignatureControl?
TransPanel = ?

Bob
Sorry ... I'm in a bit of a flap today
The signature control is positioned on the form
The TransPanel is a Panel contained a variety of control including some text boxes and a listview
When I hide the transpanel so that the only signature control (area) is visible I get the error
   An unhandled exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll
   The line
   Public Class MainFrm
   is highlighted (just MainFrm)

When I comment out
  Me.Controls.Add(_signature)
I don't get the error ...

Alison
.. Don't get the error but don't get the control with either (obviously) ...
Alison
That exception happens when you have an unhandled exception in something like a Paint event handler, and the debugger gets confused, and can't display the correct information.  This is a very difficult problem to narrow down.  I can't give you a ready answer to this one, unfortunately.  :(

Bob
SOLUTION
Avatar of g_johnson
g_johnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Will give it a whirl in the morning
I think I have got the error ...  Very stupid.  I had changed the Root Namespace .. so my LoadImage function was unable to find the graphic ... I'm going to split the points ...

      Private Sub CreateGdiObjects()
            ' only create if don't have one or the size changed
            If _bmp Is Nothing Then
                  InitMemoryBitmap()
            ElseIf _bmp.Width <> Me.Width Or _bmp.Height <> Me.Height Then
                  InitMemoryBitmap()
            End If
      End Sub

      ' Create a memory bitmap that is used to draw the signature.
      Private Sub InitMemoryBitmap()
            ' load the background image
            _bmp = Global.LoadImage("sign here.png")

            ' get graphics object now to make drawing during mousemove faster
            _graphics = Graphics.FromImage(_bmp)
      End Sub