Control not creating after adding a call to a custom class.method

mooman_fl

Well-known member
Joined
Nov 3, 2002
Messages
193
Location
Florida, USA
In making my usercontrol I have found it necessary to use the System.Drawing class to draw some areas on my control. Up till now this has been going great. I made a new friend class MooGraphics and put in some Enums and Methods to make it easier to use from my program since many of the drawing task are repetitive. Calls to these methods in my class were called once from Initialize (after all other initialization) and once in Paint. Pretty standard.

All of my calls have gone as expected until I added this line to the DrawOpen sub:

Code:
Moo.LeftLines(CollapseLineLeft.Handle, bIsCollapsed, CollapseLineLeft.BackColor, 3, 3)

Moo is an instance of my class. CollapseLineLeft is the control I am drawing to, and bIsCollapsed is a boolean to tell if my Control is collapsed or not. The above line calls this sub from my class:

Code:
    Public Sub LeftLines(ByVal CtrlHandle As System.IntPtr, ByVal Collapsed As Boolean, ByVal FillColor As Color, ByVal x As Integer, ByVal y As Integer)
        Dim myGraphics As Graphics
        Dim Shadow As New Pen(Color.FromKnownColor(KnownColor.ControlDark))
        Dim Highlight As New Pen(Color.FromKnownColor(KnownColor.ControlLightLight))

        If Collapsed = True Then
            myGraphics.DrawLine(Shadow, x, y, x + 5, y)
            myGraphics.DrawLine(Highlight, x, y + 1, x + 5, y + 1)
            myGraphics.DrawLine(Shadow, x, y, x, y + 5)
            myGraphics.DrawLine(Highlight, x + 1, y + 1, x + 1, y + 4)
        Else
            myGraphics.DrawLine(Shadow, x, y, x + 5, y)
            myGraphics.DrawLine(Highlight, x, y + 1, x + 5, y + 1)
            myGraphics.DrawLine(Shadow, x, y, x, y + 5)
            myGraphics.DrawLine(Highlight, x + 1, y + 1, x + 1, y + 4)
        End If

        myGraphics.Dispose()
    End Sub

What is happening is after I compile (no errors with Option Strict enabled) and try to add the control to the startup project is an error:

An exception occurred while trying to create an instance of ToolScroll.ToolPanel. The exception was "Object reference not set to an instance of an object."

If I REM out the call to Moo.LeftLines it will add just fine. I am not doing anything different that I can tell in that sub that I am in my other drawing subs... but the others work just fine.

What could be causing this?
 
BTW... if you are wondering why some values are not used in the sub it is simply because I hadnt implemented them yet. I was trying to test the line placement before I did rectangle fills to clear areas of the control.
 
Im no GDI man, but
--> is CollapseLineLeft.BackColor definitely instantiated?
--> myGraphics appears not to be instantiated at all (wheres the New() ?
--> You never use the handle that you passed as a parameter.
 
Heiko beat you to it this time divil <g> Thanks for the answer though.

You are both right though. Heiko caught both parts of the problem though. (Only the last two. CollapseLineLeft is a simple Panel control so BackColor is definitly instatiated)

This is what I get for not sleeping in over 24 hours. I left out this line:

Code:
myGraphics = Graphics.FromHwnd(CtrlHandle)

I think I better head to bed before I try to save my work and accidently reformat my drive instead. LOL
 

Similar threads

Back
Top