Click here to Skip to main content
16,004,991 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionCheckedListBox with multi dimensional arrays Pin
Fahim A17-Dec-08 13:14
Fahim A17-Dec-08 13:14 
AnswerRe: CheckedListBox with multi dimensional arrays Pin
Christian Graus17-Dec-08 17:03
protectorChristian Graus17-Dec-08 17:03 
GeneralRe: CheckedListBox with multi dimensional arrays Pin
Fahim A18-Dec-08 11:59
Fahim A18-Dec-08 11:59 
QuestionRemotely Accessing Serial Port on LAN/Ethernet Pin
fahadi8617-Dec-08 12:54
fahadi8617-Dec-08 12:54 
AnswerRe: Remotely Accessing Serial Port on LAN/Ethernet Pin
Expert Coming17-Dec-08 12:57
Expert Coming17-Dec-08 12:57 
QuestionExtending MonthCalendar Control Pin
Member 271340117-Dec-08 10:07
Member 271340117-Dec-08 10:07 
AnswerRe: Extending MonthCalendar Control Pin
Dave Kreskowiak18-Dec-08 4:03
mveDave Kreskowiak18-Dec-08 4:03 
GeneralRe: Extending MonthCalendar Control Pin
smurfman8718-Dec-08 10:45
smurfman8718-Dec-08 10:45 
Okay, well, I found similar C# code snippits, here http://forums.techpowerup.com/showthread.php?t=70554[^]

And decided that this was simple enough, no thrills not all the extra stuff.

So I converted everything to VB using SharpDevelop - (it is pretty handy )
I created a simple Control using the ctlClock example from Microsoft as a guideline. The control is simple in that all it does is add the ability to set the background on specific dates, bold or not bold, and a box or no box with color.

I am stuck... The project compiles with no errors.However when I create a test form in my project and try to add my newly created MonthCalendar, I get an error.

"Failed to create component 'ExtendedMonthCalendar'. The error message follows:
'System.NullReferenceException: Object Reference not set to instance of an object. ... " The error points to the Sub New where the testing created a control on the Control Design (at least that is what I think it was doing).

If I comment the code, when I add the control to my form it is blank.

If I try to add the class that is inheriting the monthcalendar to my control design, I get a different error related to the constructor of the MonCal

My code is below - converted from another example in trying to understand all of this... all of this is kept in the Control Code

any help would be appreciated.

J

Partial Public Class ExtendedMonthCalendar
    Private MyDates As List(Of HighlightedDates)
    Public Sub New()
        InitializeComponent()
        ' Dates would normally be passed in, in a List. For testing purposes I added the next declaration
        'MyDates.Add(New HighlightedDates(Convert.ToDateTime("9/1/2008"), Color.Red, Color.Blue, Color.Pink, True))
        'Dim mCal As New MonCal(MyDates)
        'Me.Controls.Add(mCal)
    End Sub
End Class

Friend Class MonCal
    Inherits MonthCalendar
    Protected Shared WM_PAINT As Integer = 15
    Private dayBox As Rectangle
    Private dayTop As Integer = 0
    Private range As SelectionRange
    Private highlightedDates As New List(Of HighlightedDates)()
    Public Sub New(ByVal HighlightedDates As List(Of HighlightedDates))
        Me.ShowTodayCircle = False
        Me.highlightedDates = HighlightedDates
        range = GetDisplayRange(False)
        SetDayBoxSize()
        SetPosition(Me.highlightedDates)
    End Sub
    ' This method figures out the size of the entire date area portion of the control 
    '   and then divides it up o create a Rectagle for painting to individual dates
    Private Sub SetDayBoxSize()
        Dim bottom As Integer = Me.Height
        While HitTest(1, dayTop).HitArea <> HitArea.[Date] AndAlso HitTest(1, dayTop).HitArea <> HitArea.PrevMonthDate
            dayTop += 1
        End While
        While HitTest(1, bottom).HitArea <> HitArea.[Date] AndAlso HitTest(1, bottom).HitArea <> HitArea.NextMonthDate
            bottom -= 1
        End While
        dayBox = New Rectangle()
        dayBox.Size = New Size(Me.Width / 7, (bottom - dayTop) / 6)
    End Sub
    ' This method determines where in the 7 x 6 array of dates on the control our highlighted dates reside.
    Private Sub SetPosition(ByVal hlDates As List(Of HighlightedDates))
        Dim row As Integer = 0, col As Integer = 0
        hlDates.ForEach(AddressOf ConvertedAnonymousMethod1)
    End Sub
    ' This overrides the message pump and traps the WM_PAINT call
    Protected Overloads Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        If m.Msg = WM_PAINT Then
            Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
            Dim pea As New PaintEventArgs(g, New Rectangle(0, 0, Me.Width, Me.Height))
            OnPaint(pea)
        End If
    End Sub
    ' Here is where we use our information to selectively draw what we want
    Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
        Dim g As Graphics = e.Graphics
        Dim backgroundRect As Rectangle
        highlightedDates.ForEach(AddressOf ConvertedAnonymousMethod2)
    End Sub
    Private Sub ConvertedAnonymousMethod1(ByVal [date] As HighlightedDates)
        If [date].[Date] >= range.Start AndAlso [date].[Date] <= range.[End] Then
            Dim span As TimeSpan = [date].[Date].Subtract(range.Start)
            Dim row = span.Days / 7
            Dim col = span.Days Mod 7
            [date].Position = New Point(row, col)
        End If
    End Sub
    Private Sub ConvertedAnonymousMethod2(ByVal [date] As HighlightedDates)
        Dim g As Graphics
        Dim backgroundRect = New Rectangle([date].Position.Y * dayBox.Width + 1, [date].Position.X * dayBox.Height + dayTop, dayBox.Width, dayBox.Height)
        If [date].BackgroundColor <> Color.Empty Then
            Using brush As Brush = New SolidBrush([date].BackgroundColor)
                g.FillRectangle(brush, backgroundRect)
            End Using
        End If
        If [date].Bold OrElse [date].DateColor <> Color.Empty Then
            Using textFont As New Font(Font, (IIf([date].Bold, FontStyle.Bold, FontStyle.Regular)))
                TextRenderer.DrawText(g, [date].[Date].Day.ToString(), textFont, backgroundRect, [date].DateColor, TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
            End Using
        End If
        If [date].BoxColor <> Color.Empty Then
            Using pen As New Pen([date].BoxColor)
                Dim boxRect As New Rectangle([date].Position.Y * dayBox.Width + 1, [date].Position.X * dayBox.Height + dayTop, dayBox.Width, dayBox.Height)
                g.DrawRectangle(pen, boxRect)
            End Using
        End If
    End Sub

End Class

Friend Class HighlightedDates
    Public [Date] As DateTime
    Public Position As New Point(0, 0)
    Public DateColor As Color
    Public BoxColor As Color
    Public BackgroundColor As Color
    Public Bold As Boolean
    ' This constructor is used if you only want to make dates bold. All colors are set to "Empty"(null color)
    Public Sub New(ByVal [date] As DateTime)
        Me.[Date] = [date]
        Me.DateColor = Color.Empty
        Me.BoxColor = Color.Empty
        Me.BackgroundColor = Color.Empty
        Me.Bold = True
    End Sub
    ' This constructor is used if you want colored and/or bolded dates
    Public Sub New(ByVal [date] As DateTime, ByVal dateColor As Color, ByVal bold As Boolean)
        Me.[Date] = [date]
        Me.DateColor = dateColor
        Me.BoxColor = Color.Empty
        Me.BackgroundColor = Color.Empty
        Me.Bold = bold
    End Sub
    ' This constructor is used when you want to control everything
    Public Sub New(ByVal [date] As DateTime, ByVal dateColor As Color, ByVal boxColor As Color, ByVal backgroundColor As Color, ByVal bold As Boolean)
        Me.[Date] = [date]
        Me.DateColor = dateColor
        Me.BoxColor = boxColor
        Me.BackgroundColor = backgroundColor
        Me.Bold = bold
    End Sub
End Class

GeneralRe: Extending MonthCalendar Control Pin
Dave Kreskowiak18-Dec-08 10:53
mveDave Kreskowiak18-Dec-08 10:53 
GeneralRe: Extending MonthCalendar Control Pin
smurfman8718-Dec-08 11:04
smurfman8718-Dec-08 11:04 
GeneralRe: Extending MonthCalendar Control Pin
Dave Kreskowiak18-Dec-08 16:44
mveDave Kreskowiak18-Dec-08 16:44 
GeneralRe: Extending MonthCalendar Control Pin
smurfman8719-Dec-08 3:30
smurfman8719-Dec-08 3:30 
GeneralRe: Extending MonthCalendar Control Pin
smurfman8719-Dec-08 6:09
smurfman8719-Dec-08 6:09 
QuestionHelp: ASP.NET Ajax client-side framework failed to load. [modified] Pin
Kip Smith17-Dec-08 10:00
Kip Smith17-Dec-08 10:00 
AnswerRe: Help: ASP.NET Ajax client-side framework failed to load. Pin
Christian Graus17-Dec-08 17:04
protectorChristian Graus17-Dec-08 17:04 
AnswerRe: Help: ASP.NET Ajax client-side framework failed to load. Pin
Kip Smith18-Dec-08 4:11
Kip Smith18-Dec-08 4:11 
Questionhow to send and read data through serial port using visual studio 2005? Pin
vivekmenon170617-Dec-08 8:03
vivekmenon170617-Dec-08 8:03 
AnswerRe: how to send and read data through serial port using visual studio 2005? Pin
Ben Fair17-Dec-08 8:51
Ben Fair17-Dec-08 8:51 
GeneralRe: how to send and read data through serial port using visual studio 2005? Pin
Luc Pattyn17-Dec-08 8:57
sitebuilderLuc Pattyn17-Dec-08 8:57 
QuestionOOOOOOOPS!!!!! Pin
kshincsk17-Dec-08 7:58
kshincsk17-Dec-08 7:58 
GeneralRe: OOOOOOOPS!!!!! Pin
Luc Pattyn17-Dec-08 9:00
sitebuilderLuc Pattyn17-Dec-08 9:00 
QuestionMulti User Application with Access database Pin
kshincsk17-Dec-08 7:56
kshincsk17-Dec-08 7:56 
AnswerRe: Multi User Application with Access database Pin
Dave Kreskowiak18-Dec-08 2:10
mveDave Kreskowiak18-Dec-08 2:10 
GeneralRe: Multi User Application with Access database Pin
kshincsk19-Dec-08 4:10
kshincsk19-Dec-08 4:10 
GeneralRe: Multi User Application with Access database Pin
Dave Kreskowiak19-Dec-08 12:08
mveDave Kreskowiak19-Dec-08 12:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.