• (2 Pages)
  • +
  • 1
  • 2

Making your own calendar control Rate Topic: ***** 1 Votes

#1 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 27 June 2008 - 01:24 PM

Sure, there is already a calendar control available, but it is very limited in how you can work with it. If you want to make your own calendar control, it's not as hard as you might think. First thing, like always, you will need to set up your form. Here is a list of all the controls you will need to be on it

A label for displaying the month name
You will probably want to put this near the top of your form, because the name of the month is traditionally displayed at the top of the calendar, but it's your application, you can do whatever you want. This should be named "MonthName".

7 labels for the names of the days
This isn't really required, as they aren't accessed anywhere in the code. They should be placed in a row, as seen on any calendar. You can call these whatever you want, because again, they aren't accessed anywhere in your code.

(Optional) A panel to underline the labels that display the day names
This is also not required, but it makes your calendar look better. This should be positioned under your row of 7 labels that display the names of the days. It should be between 1-3 pixels high.

A grid of labels to display the day numbers
This is by far the most important part. This grid will be used to display the numbers of the days. This grid should be 7 wide and 6 high. There is a system by which they must be named, so pay close attention

The labels should be separated into 7 columns (one for each day of the week, of course) and in each column, this is how the naming should go:
Under the "Sunday" label, the first label under that should be called "su1", the second "su2", the third "su3", and so on until you reach "su6".
Under the "Monday" label, the first label under that should be called "m1", the second "m2", the third "m3", and so on until you reach "m6".
Under the "Tuesday" label, the first label under that should be called "tu1", the second "tu2", the third "tu3", and so on until you reach "tu6".
Under the "Wednesday" label, the first label under that should be called "w1", the second "w2", the third "w3", and so on until you reach "w6".
Under the "Thursday" label, the first label under that should be called "th1", the second "th2", the third "th3", and so on until you reach "th6".
Under the "Friday" label, the first label under that should be called "f1", the second "f2", the third "f3", and so on until you reach "f6".
Under the "Saturday" label, the first label under that should be called "sa1", the second "sa2", the third "sa3", and so on until you reach "sa6".

There, now you have the hard part done. If you still don't understand the naming system, there is an image attached called "example.png".

Now the only thing left to do is get the code in place. So, shift yourself from design mode into code mode.

The first function you must make is called "ReloadCal()" here it is (you can copy-and-paste it)

Public Sub ReloadCal(ByVal ldate As Date, ByVal Selected As Integer)
        On Error Resume Next
        Me.clearall()
        MonthName.Text = monthstr(ldate.Month)
        Dim fdate As DayOfWeek = GetFirstOfMonthDay(ldate)
        Dim idate As Integer = 1
        Dim row As Integer = 1
        Do
            getlabel(fdate, row).Text = idate
            getlabel(fdate, row).ForeColor = Label18.ForeColor
            If idate = Selected Then
                getlabel(fdate, row).ForeColor = Color.Yellow
            End If
            If fdate = DayOfWeek.Saturday Then
                row += 1
            End If
            fdate = tdate(fdate)
            idate += 1
            If idate = Date.DaysInMonth(ldate.Year, ldate.Month) + 1 Then
                Exit Do
            End If
        Loop
    End Sub


You should get a few errors when you paste this code, because it access a few other subs we haven't got in place yet. The "ReloadCal()" function is the one you will use to load the calendar when your application starts. There are 2 arguments it takes, the first "ldate" is short for "Load Date", it is the date that the calendar will be loaded for. The second "Selected" is the day that will be highlighted as the current day. To load the calendar for today, use "ReloadCal(Now, Now.Day)".

Let's move on to the next function. This one clears all the labels in the grid

 Sub clearall()
        su1.Text = ""
        su2.Text = ""
        su3.Text = ""
        su4.Text = ""
        su5.Text = ""
        su6.Text = ""

        m1.Text = ""
        m2.Text = ""
        m3.Text = ""
        m4.Text = ""
        m5.Text = ""
        m6.Text = ""

        tu1.Text = ""
        tu2.Text = ""
        tu3.Text = ""
        tu4.Text = ""
        tu5.Text = ""
        tu6.Text = ""

        w1.Text = ""
        w2.Text = ""
        w3.Text = ""
        w4.Text = ""
        w5.Text = ""
        w6.Text = ""

        th1.Text = ""
        th2.Text = ""
        th3.Text = ""
        th4.Text = ""
        th5.Text = ""
        th6.Text = ""

        f1.Text = ""
        f2.Text = ""
        f3.Text = ""
        f4.Text = ""
        f5.Text = ""
        f6.Text = ""

        sa1.Text = ""
        sa2.Text = ""
        sa3.Text = ""
        sa4.Text = ""
        sa5.Text = ""
        sa6.Text = ""
    End Sub


That function was fairly self-explaining.

These next functions are used by the ReloadCal() function in loading the calendar. Again, copy and paste these into your code.

    Function getlabel(ByVal day As DayOfWeek, ByVal row As Integer) As System.Windows.Forms.Label
        Select Case day
            Case DayOfWeek.Sunday
                Select Case row
                    Case 1
                        Return su1
                    Case 2
                        Return su2
                    Case 3
                        Return su3
                    Case 4
                        Return su4
                    Case 5
                        Return su5
                    Case 6
                        Return su6
                End Select
            Case DayOfWeek.Monday
                Select Case row
                    Case 1
                        Return m1
                    Case 2
                        Return m2
                    Case 3
                        Return m3
                    Case 4
                        Return m4
                    Case 5
                        Return m5
                    Case 6
                        Return m6
                End Select
            Case DayOfWeek.Tuesday
                Select Case row
                    Case 1
                        Return tu1
                    Case 2
                        Return tu2
                    Case 3
                        Return tu3
                    Case 4
                        Return tu4
                    Case 5
                        Return tu5
                    Case 6
                        Return tu6
                End Select
            Case DayOfWeek.Wednesday
                Select Case row
                    Case 1
                        Return w1
                    Case 2
                        Return w2
                    Case 3
                        Return w3
                    Case 4
                        Return w4
                    Case 5
                        Return w5
                    Case 6
                        Return w6
                End Select
            Case DayOfWeek.Thursday
                Select Case row
                    Case 1
                        Return th1
                    Case 2
                        Return th2
                    Case 3
                        Return th3
                    Case 4
                        Return th4
                    Case 5
                        Return th5
                    Case 6
                        Return th6
                End Select
            Case DayOfWeek.Friday
                Select Case row
                    Case 1
                        Return f1
                    Case 2
                        Return f2
                    Case 3
                        Return f3
                    Case 4
                        Return f4
                    Case 5
                        Return f5
                    Case 6
                        Return f6
                End Select
            Case DayOfWeek.Saturday
                Select Case row
                    Case 1
                        Return sa1
                    Case 2
                        Return sa2
                    Case 3
                        Return sa3
                    Case 4
                        Return sa4
                    Case 5
                        Return sa5
                    Case 6
                        Return sa6
                End Select
        End Select
    End Function

Private Function GetFirstOfMonthDay(ByVal ThisDay As Date) As DayOfWeek
        Dim tday As DayOfWeek = ThisDay.DayOfWeek
        Dim tint As Integer = ThisDay.Day
        If tint = 1 Then
            Return tday
            Exit Function
        End If
        Do
            tint -= 1
            tday = ydate(tday)
            If tint = 1 Then Exit Do
        Loop
        Return tday
    End Function

    Private Function ydate(ByVal tday As DayOfWeek) As DayOfWeek
        Dim rday As DayOfWeek
        Select Case tday
            Case DayOfWeek.Sunday
                rday = DayOfWeek.Saturday
            Case DayOfWeek.Monday
                rday = DayOfWeek.Sunday
            Case DayOfWeek.Tuesday
                rday = DayOfWeek.Monday
            Case DayOfWeek.Wednesday
                rday = DayOfWeek.Tuesday
            Case DayOfWeek.Thursday
                rday = DayOfWeek.Wednesday
            Case DayOfWeek.Friday
                rday = DayOfWeek.Thursday
            Case DayOfWeek.Saturday
                rday = DayOfWeek.Friday
        End Select
        Return rday
    End Function

    Private Function tdate(ByVal tday As DayOfWeek) As DayOfWeek
        Dim rday As DayOfWeek
        Select Case tday
            Case DayOfWeek.Sunday
                rday = DayOfWeek.Monday
            Case DayOfWeek.Monday
                rday = DayOfWeek.Tuesday
            Case DayOfWeek.Tuesday
                rday = DayOfWeek.Wednesday
            Case DayOfWeek.Wednesday
                rday = DayOfWeek.Thursday
            Case DayOfWeek.Thursday
                rday = DayOfWeek.Friday
            Case DayOfWeek.Friday
                rday = DayOfWeek.Saturday
            Case DayOfWeek.Saturday
                rday = DayOfWeek.Sunday
        End Select
        Return rday
    End Function

    Public Function monthstr(ByVal month As Integer) As String
        Select Case month
            Case 1
                Return "January"
            Case 2
                Return "Febuary"
            Case 3
                Return "March"
            Case 4
                Return "April"
            Case 5
                Return "May"
            Case 6
                Return "June"
            Case 7
                Return "July"
            Case 8
                Return "August"
            Case 9
                Return "September"
            Case 10
                Return "October"
            Case 11
                Return "November"
            Case 12
                Return "December"
        End Select
    End Function



That's all there is to it. Your calendar should work now. If you copied all the code right, you should get no errors.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 1
  • +

Replies To: Making your own calendar control

#2 DataPriest  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 57
  • Joined: 29-April 09

Posted 01 May 2009 - 12:27 AM

Good stuff.
Was This Post Helpful? 0
  • +
  • -

#3 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Posted 27 October 2009 - 07:08 AM

when i run it nothing happens :S
what am i doing wrong?
Public NotInheritable Class SplashScreen1

	Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

	End Sub

	Public Sub ReloadCal(ByVal ldate As Date, ByVal Selected As Integer)
		On Error Resume Next
		Me.clearall()
		MonthName.Text = monthstr(ldate.Month)
		Dim fdate As DayOfWeek = GetFirstOfMonthDay(ldate)
		Dim idate As Integer = 1
		Dim row As Integer = 1
		Do
			getlabel(fdate, row).Text = idate
			getlabel(fdate, row).ForeColor = MonthName.ForeColor
			If idate = Selected Then
				getlabel(fdate, row).ForeColor = Color.Yellow
			End If
			If fdate = DayOfWeek.Saturday Then
				row += 1
			End If
			fdate = tdate(fdate)
			idate += 1
			If idate = Date.DaysInMonth(ldate.Year, ldate.Month) + 1 Then
				Exit Do
			End If
		Loop
	End Sub
	Sub clearall()
		su1.Text = ""
		su2.Text = ""
		su3.Text = ""
		su4.Text = ""
		su5.Text = ""
		su6.Text = ""

		m1.Text = ""
		m2.Text = ""
		m3.Text = ""
		m4.Text = ""
		m5.Text = ""
		m6.Text = ""

		tu1.Text = ""
		tu2.Text = ""
		tu3.Text = ""
		tu4.Text = ""
		tu5.Text = ""
		tu6.Text = ""

		w1.Text = ""
		w2.Text = ""
		w3.Text = ""
		w4.Text = ""
		w5.Text = ""
		w6.Text = ""

		th1.Text = ""
		th2.Text = ""
		th3.Text = ""
		th4.Text = ""
		th5.Text = ""
		th6.Text = ""

		f1.Text = ""
		f2.Text = ""
		f3.Text = ""
		f4.Text = ""
		f5.Text = ""
		f6.Text = ""

		sa1.Text = ""
		sa2.Text = ""
		sa3.Text = ""
		sa4.Text = ""
		sa5.Text = ""
		sa6.Text = ""
	End Sub
	Function getlabel(ByVal day As DayOfWeek, ByVal row As Integer) As System.Windows.Forms.Label
		Select Case day
			Case DayOfWeek.Sunday
				Select Case row
					Case 1
						Return su1
					Case 2
						Return su2
					Case 3
						Return su3
					Case 4
						Return su4
					Case 5
						Return su5
					Case 6
						Return su6
				End Select
			Case DayOfWeek.Monday
				Select Case row
					Case 1
						Return m1
					Case 2
						Return m2
					Case 3
						Return m3
					Case 4
						Return m4
					Case 5
						Return m5
					Case 6
						Return m6
				End Select
			Case DayOfWeek.Tuesday
				Select Case row
					Case 1
						Return tu1
					Case 2
						Return tu2
					Case 3
						Return tu3
					Case 4
						Return tu4
					Case 5
						Return tu5
					Case 6
						Return tu6
				End Select
			Case DayOfWeek.Wednesday
				Select Case row
					Case 1
						Return w1
					Case 2
						Return w2
					Case 3
						Return w3
					Case 4
						Return w4
					Case 5
						Return w5
					Case 6
						Return w6
				End Select
			Case DayOfWeek.Thursday
				Select Case row
					Case 1
						Return th1
					Case 2
						Return th2
					Case 3
						Return th3
					Case 4
						Return th4
					Case 5
						Return th5
					Case 6
						Return th6
				End Select
			Case DayOfWeek.Friday
				Select Case row
					Case 1
						Return f1
					Case 2
						Return f2
					Case 3
						Return f3
					Case 4
						Return f4
					Case 5
						Return f5
					Case 6
						Return f6
				End Select
			Case DayOfWeek.Saturday
				Select Case row
					Case 1
						Return sa1
					Case 2
						Return sa2
					Case 3
						Return sa3
					Case 4
						Return sa4
					Case 5
						Return sa5
					Case 6
						Return sa6
				End Select
		End Select
	End Function

	Private Function GetFirstOfMonthDay(ByVal ThisDay As Date) As DayOfWeek
		Dim tday As DayOfWeek = ThisDay.DayOfWeek
		Dim tint As Integer = ThisDay.Day
		If tint = 1 Then
			Return tday
			Exit Function
		End If
		Do
			tint -= 1
			tday = ydate(tday)
			If tint = 1 Then Exit Do
		Loop
		Return tday
	End Function

	Private Function ydate(ByVal tday As DayOfWeek) As DayOfWeek
		Dim rday As DayOfWeek
		Select Case tday
			Case DayOfWeek.Sunday
				rday = DayOfWeek.Saturday
			Case DayOfWeek.Monday
				rday = DayOfWeek.Sunday
			Case DayOfWeek.Tuesday
				rday = DayOfWeek.Monday
			Case DayOfWeek.Wednesday
				rday = DayOfWeek.Tuesday
			Case DayOfWeek.Thursday
				rday = DayOfWeek.Wednesday
			Case DayOfWeek.Friday
				rday = DayOfWeek.Thursday
			Case DayOfWeek.Saturday
				rday = DayOfWeek.Friday
		End Select
		Return rday
	End Function

	Private Function tdate(ByVal tday As DayOfWeek) As DayOfWeek
		Dim rday As DayOfWeek
		Select Case tday
			Case DayOfWeek.Sunday
				rday = DayOfWeek.Monday
			Case DayOfWeek.Monday
				rday = DayOfWeek.Tuesday
			Case DayOfWeek.Tuesday
				rday = DayOfWeek.Wednesday
			Case DayOfWeek.Wednesday
				rday = DayOfWeek.Thursday
			Case DayOfWeek.Thursday
				rday = DayOfWeek.Friday
			Case DayOfWeek.Friday
				rday = DayOfWeek.Saturday
			Case DayOfWeek.Saturday
				rday = DayOfWeek.Sunday
		End Select
		Return rday
	End Function

	Public Function monthstr(ByVal month As Integer) As String
		Select Case month
			Case 1
				Return "January"
			Case 2
				Return "Febuary"
			Case 3
				Return "March"
			Case 4
				Return "April"
			Case 5
				Return "May"
			Case 6
				Return "June"
			Case 7
				Return "July"
			Case 8
				Return "August"
			Case 9
				Return "September"
			Case 10
				Return "October"
			Case 11
				Return "November"
			Case 12
				Return "December"
		End Select
	End Function

End Class


Was This Post Helpful? 0
  • +
  • -

#4 Guest_Shawn*


Reputation:

Posted 27 January 2011 - 12:12 PM

View PostEndLessMind, on 27 October 2009 - 07:08 AM, said:

when i run it nothing happens :S
what am i doing wrong?


Private Sub SplashScreen1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  ReloadCal(Date.Today, Date.Today.Day)
End Sub



Probably not the best coding practice but it worked for me.
Was This Post Helpful? 0

#5 babyeileen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-April 11

Posted 11 April 2011 - 07:14 PM

hie..
I copy the full codes but nothing happens when i run..
kindly assist
Was This Post Helpful? 0
  • +
  • -

#6 cj777  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-March 12

Posted 09 March 2012 - 10:19 PM

is this code for the month only or a year?
Was This Post Helpful? 0
  • +
  • -

#7 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 50
  • View blog
  • Posts: 226
  • Joined: 10-March 12

Posted 10 March 2012 - 03:06 PM

hello, sir! i have used your code for creating a custom calendar, but instead of labels for displaying the dates, i used buttons, because i want to be able to eneter or read a note when i click on date (something like a diary). But now, i am stuck with a problem: i dont know how i could add two buttons to this calendar, one for displaying future months, the other for backward months. I tryed to modify the code for myself, but didnt have much luck with it. Also, searching for a similar code or tutorial over the internet, was unconclusive... could you point me in the right direction, please?
thank you very much!
Was This Post Helpful? 1
  • +
  • -

#8 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Posted 10 March 2012 - 04:06 PM

@rusoaica
Try setting up as new "Date" and then move time forward by one month.
and then use that new Date to reload the calender.
Was This Post Helpful? 1
  • +
  • -

#9 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 50
  • View blog
  • Posts: 226
  • Joined: 10-March 12

Posted 10 March 2012 - 04:11 PM

i was thinking to that myself..but say, if i want the calendar to be for the next ten years, do you imagine how many "DATE" functions i should have? i was also trying something like incrementing a variable on the button click and pass it to the function... i must have done it wrong, cause it wont work :(
Was This Post Helpful? 0
  • +
  • -

#10 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Posted 10 March 2012 - 04:13 PM

well, to make it work for 10 years +, then i would do a variable that increase for each button-click :)
Was This Post Helpful? 1
  • +
  • -

#11 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 50
  • View blog
  • Posts: 226
  • Joined: 10-March 12

Posted 10 March 2012 - 04:17 PM

my thoughts same..but, as far as i tryed, i was not able to achieve this... nor do i find a tutorial for incrementing the month on a button click... they are all about the standard calendar control and date picker..
anyway, i saw that you asked at one moment the same question..did you managed to solve it? i dont need the code, i just need guidance to think the code for my own..
Was This Post Helpful? 1
  • +
  • -

#12 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Posted 10 March 2012 - 04:22 PM

Yes i did. I did a diary as well, but i don't remember how i can find the code.
Got a hard-drive crash some of mouths ago :/
Was This Post Helpful? 0
  • +
  • -

#13 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 50
  • View blog
  • Posts: 226
  • Joined: 10-March 12

Posted 10 March 2012 - 04:24 PM

thanks anyway... i will also try to work on it this night... the best chance of mine would be the creator of this custom calendar to answer our questions :D
Was This Post Helpful? 1
  • +
  • -

#14 rusoaica  Icon User is offline

  • D.I.C Head

Reputation: 50
  • View blog
  • Posts: 226
  • Joined: 10-March 12

Posted 14 March 2012 - 06:22 AM

@EndLessMind, i finally managed to create the calendar, add the next/previews month and year buttons, implemented the leap year function, and now... voila`! it works :)

Live long, and prosper!\\//_
Was This Post Helpful? 0
  • +
  • -

#15 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Posted 14 March 2012 - 07:49 AM

Great! :) Good job solving it! :)
It would be nice if you could post your solution so others can be helped :)

This post has been edited by EndLessMind: 14 March 2012 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2