Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-11
12
Medium Priority
?
644 Views
Last Modified: 2008-02-01
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 ...
0
Comment
Question by:AlisonBallard
  • 6
  • 5
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16425762
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
0
 

Author Comment

by:AlisonBallard
ID: 16426061
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    
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16426091
How are you "hiding" the panel, and showing the Capture section?

Bob
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:AlisonBallard
ID: 16426424
I'm calling :
Transpanel.hide
The capture section is visible and is behind the 'TransPanel' located directly on the form
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16426462
I still can't see anything leaping off the page, yet.

Capture section = SignatureControl?
TransPanel = ?

Bob
0
 

Author Comment

by:AlisonBallard
ID: 16427558
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
0
 

Author Comment

by:AlisonBallard
ID: 16428087
.. Don't get the error but don't get the control with either (obviously) ...
Alison
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16428294
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
0
 
LVL 4

Assisted Solution

by:g_johnson
g_johnson earned 600 total points
ID: 16428647
The only time I ever had this error it was because I had a dim statement and tried to reference the variable while it was still "nothing"

Therefore I suspect this section of code:
 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) <----- maybe this line??
                    points(i).X = pt.X
                    points(i).Y = pt.Y
               Next i

This is just a wild guess based on my one experience with this error
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 900 total points
ID: 16428658
Anything is possible.  Can you put in breakpoints, and test?

Bob
0
 

Author Comment

by:AlisonBallard
ID: 16429904
Will give it a whirl in the morning
0
 

Author Comment

by:AlisonBallard
ID: 16434458
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question