• We just launched and are currently in beta. Join us as we build and grow the community.

Creating your own calendar in VB.NET

omar353531

SERP Dominator
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
192
Likes
200
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
This is a tutorial that will teach you how to create and design your own calendar using vb.net.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.

2. Next, add a Label named labelMonth for displaying the month, in label, combobox named comboMonth to display the Month, textbox named textBoxYear for the year, panel named panel1 for displaying the days, 3 buttons named button1 for displaying next date, button2 for displaying the previous date, and buttonGo for searching the desired date. You must design your interface like this:

calendardes.png


3. Now, we will do the coding.

First, declare the variables below.
  1. Dim

    lblDayz As

    Label
  2. Dim

    y As

    Int32 =

    0
  3. Dim

    x As

    Int32
  4. Dim

    ndayz As

    Int32
  5. Dim

    Dayofweek, CurrentCulture As

    String

Next, we will code for our Form_Load to display the current date.
  1. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  2. 'display the current month
  3. comboBoxMonth.

    Text

    =

    DateTime.

    Now

    .

    Month

    .

    ToString

    (

    )
  4. 'Get there windows culture
  5. CurrentCulture =

    Globalization.

    CultureInfo

    .

    CurrentCulture

    .

    Name
  6. 'display the full name of the current month
  7. labelMonth.

    Text

    =

    Application.

    CurrentCulture

    .

    DateTimeFormat

    .

    GetMonthName

    (

    Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )

    )
  8. 'get the number of days in the selected month and year
  9. My.

    Application

    .

    ChangeCulture

    (

    "en-za"

    )
  10. Dim

    Dayz As

    Int32 =

    DateTime.

    DaysInMonth

    (

    DateTime.

    Now

    .

    Year

    , DateTime.

    Now

    .

    Month

    )
  11. 'display the current year in the textbox
  12. textBoxYear.

    Text

    =

    DateTime.

    Now

    .

    Year

    .

    ToString

    (

    )
  13. 'call the checkday function
  14. CheckDay(

    )
  15. For

    i As

    Int32 =

    1

    To

    Dayz
  16. ndayz +=

    1
  17. lblDayz =

    New

    Label(

    )
  18. lblDayz.

    Name

    =

    "B"

    &

    i
  19. lblDayz.

    Text

    =

    i.

    ToString

    (

    )
  20. lblDayz.

    BorderStyle

    =

    BorderStyle.

    Fixed3D
  21. If

    i =

    DateTime.

    Now

    .

    Day

    Then
  22. lblDayz.

    BackColor

    =

    Color.

    Green
  23. ElseIf

    ndayz =

    1

    Then
  24. lblDayz.

    BackColor

    =

    Color.

    Red
  25. Else
  26. lblDayz.

    BackColor

    =

    Color.

    Aquamarine
  27. End

    If
  28. lblDayz.

    Font

    =

    label31.

    Font
  29. lblDayz.

    SetBounds

    (

    x, y, 37

    , 27

    )
  30. x +=

    42
  31. If

    ndayz =

    7

    Then
  32. x =

    0
  33. ndayz =

    0
  34. y +=

    29
  35. End

    If
  36. panel1.

    Controls

    .

    Add

    (

    lblDayz)
  37. Next
  38. 'return all values to default
  39. x =

    0
  40. ndayz =

    0
  41. y =

    0
  42. End

    Sub

Then we will create a function named CheckDay() As Int32 to check if the given day is valid or not.

  1. Function

    CheckDay(

    )

    As

    Int32
  2. Dim

    time As

    DateTime =

    Convert.

    ToDateTime

    (

    comboBoxMonth.

    Text

    +

    "/01/"

    +

    textBoxYear.

    Text

    )
  3. 'get the start day of the week for the entered date and month
  4. Dayofweek =

    Application.

    CurrentCulture

    .

    Calendar

    .

    GetDayOfWeek

    (

    time)

    .

    ToString

    (

    )
  5. If

    Dayofweek =

    "Sunday"

    Then
  6. x =

    0
  7. ElseIf

    Dayofweek =

    "Monday"

    Then
  8. x =

    0

    +

    42
  9. ndayz =

    1
  10. ElseIf

    Dayofweek =

    "Tuesday"

    Then
  11. x =

    0

    +

    84
  12. ndayz =

    2
  13. ElseIf

    Dayofweek =

    "Wednesday"

    Then
  14. x =

    0

    +

    84

    +

    42
  15. ndayz =

    3
  16. ElseIf

    Dayofweek =

    "Thursday"

    Then
  17. x =

    0

    +

    84

    +

    84
  18. ndayz =

    4
  19. ElseIf

    Dayofweek =

    "Friday"

    Then
  20. x =

    0

    +

    84

    +

    84

    +

    42
  21. ndayz =

    5
  22. ElseIf

    Dayofweek =

    "Saturday"

    Then
  23. x =

    0

    +

    84

    +

    84

    +

    84
  24. ndayz =

    6
  25. End

    If
  26. Return

    x

  27. End

    Function

Next, we will go for coding the searching button then we will use the Function CheckDay() for this to validate the inputted date.

  1. Private

    Sub

    buttonGo_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    buttonGo.

    Click
  2. If

    comboBoxMonth.

    Text

    =

    Nothing

    Or

    textBoxYear.

    Text

    =

    Nothing

    Then
  3. MessageBox.

    Show

    (

    "Either year or month is incorrect"

    )
  4. Else
  5. Try
  6. Dim

    t As

    Int32 =

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )
  7. If

    Not

    textBoxYear.

    Text

    =

    "0"

    Or

    t <

    1

    Then
  8. 'remove all the controls in the panel
  9. panel1.

    Controls

    .

    Clear

    (

    )
  10. My.

    Application

    .

    ChangeCulture

    (

    CurrentCulture)
  11. 'display the selected month's fullname
  12. labelMonth.

    Text

    =

    Application.

    CurrentCulture

    .

    DateTimeFormat

    .

    GetMonthName

    (

    Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )

    )
  13. My.

    Application

    .

    ChangeCulture

    (

    "en-za"

    )
  14. Dim

    Dayz As

    Int32 =

    DateTime.

    DaysInMonth

    (

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )

    , Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )

    )
  15. CheckDay(

    )
  16. For

    i As

    Int32 =

    1

    To

    Dayz
  17. ndayz +=

    1
  18. lblDayz =

    New

    Label(

    )
  19. lblDayz.

    Text

    =

    i.

    ToString

    (

    )
  20. lblDayz.

    BorderStyle

    =

    BorderStyle.

    Fixed3D
  21. Dim

    mon As

    Int32 =

    Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )
  22. Dim

    years As

    Int32 =

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )
  23. If

    (

    (

    i =

    DateTime.

    Now

    .

    Day

    )

    And

    (

    mon =

    DateTime.

    Now

    .

    Month

    )

    And

    (

    years =

    DateTime.

    Now

    .

    Year

    )

    )

    Then
  24. 'the current day must be highlighted differently
  25. lblDayz.

    BackColor

    =

    Color.

    Green
  26. ElseIf

    ndayz =

    1

    Then
  27. lblDayz.

    BackColor

    =

    Color.

    Red
  28. Else
  29. 'set this color for other days in the selected month
  30. lblDayz.

    BackColor

    =

    Color.

    Aquamarine
  31. End

    If
  32. lblDayz.

    Font

    =

    label31.

    Font
  33. lblDayz.

    SetBounds

    (

    x, y, 37

    , 27

    )
  34. x +=

    42
  35. If

    ndayz =

    7

    Then
  36. x =

    0
  37. ndayz =

    0
  38. y +=

    29
  39. End

    If
  40. panel1.

    Controls

    .

    Add

    (

    lblDayz)
  41. Next
  42. x =

    0
  43. ndayz =

    0
  44. y =

    0
  45. Else
  46. MessageBox.

    Show

    (

    "must be between 0 and 9999"

    )
  47. textBoxYear.

    Focus

    (

    )
  48. End

    If
  49. Catch

    er As

    FormatException
  50. MessageBox.

    Show

    (

    "Year must be between 0 and 9999"

    )
  51. textBoxYear.

    Focus

    (

    )
  52. End

    Try
  53. End

    If
  54. End

    Sub

For displaying the next date, we will code for Button1_click. Have this code below:

  1. Private

    Sub

    button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    button1.

    Click
  2. Try
  3. Dim

    currentmonth, currentyear As

    Int32
  4. currentyear =

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )
  5. currentmonth =

    Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )
  6. If

    (

    currentmonth =

    12

    )

    Then
  7. 'move to the next month
  8. currentyear +=

    1
  9. currentmonth =

    1
  10. textBoxYear.

    Text

    =

    currentyear.

    ToString

    (

    )
  11. comboBoxMonth.

    Text

    =

    currentmonth.

    ToString

    (

    )
  12. Else
  13. currentmonth +=

    1
  14. comboBoxMonth.

    Text

    =

    currentmonth.

    ToString

    (

    )
  15. End

    If
  16. 'remove all the controls in the panel
  17. panel1.

    Controls

    .

    Clear

    (

    )
  18. 'Display the month's name in the windows culture
  19. My.

    Application

    .

    ChangeCulture

    (

    CurrentCulture)
  20. 'display the selected month's fullname
  21. labelMonth.

    Text

    =

    Application.

    CurrentCulture

    .

    DateTimeFormat

    .

    GetMonthName

    (

    currentmonth)
  22. 'This project was created in a computer using en-za
  23. My.

    Application

    .

    ChangeCulture

    (

    "en-za"

    )
  24. Dim

    Dayz As

    Int32 =

    DateTime.

    DaysInMonth

    (

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )

    , Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )

    )
  25. CheckDay(

    )
  26. For

    i As

    Int32 =

    1

    To

    Dayz
  27. ndayz +=

    1
  28. lblDayz =

    New

    Label(

    )
  29. lblDayz.

    Text

    =

    i.

    ToString

    (

    )
  30. lblDayz.

    BorderStyle

    =

    BorderStyle.

    Fixed3D
  31. Dim

    mon As

    Int32 =

    Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )
  32. Dim

    years As

    Int32 =

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )
  33. If

    (

    (

    i =

    DateTime.

    Now

    .

    Day

    )

    And

    (

    mon =

    DateTime.

    Now

    .

    Month

    )

    And

    (

    years =

    DateTime.

    Now

    .

    Year

    )

    )

    Then
  34. 'the current day must be highlighted differently
  35. lblDayz.

    BackColor

    =

    Color.

    Green
  36. ElseIf

    (

    ndayz =

    1

    )

    Then
  37. lblDayz.

    BackColor

    =

    Color.

    Red
  38. Else
  39. 'set this color for other days in the selected month
  40. lblDayz.

    BackColor

    =

    Color.

    Aquamarine
  41. End

    If
  42. lblDayz.

    Font

    =

    label31.

    Font
  43. lblDayz.

    SetBounds

    (

    x, y, 37

    , 27

    )
  44. x +=

    42
  45. If

    (

    ndayz =

    7

    )

    Then
  46. x =

    0
  47. ndayz =

    0
  48. y +=

    29
  49. End

    If
  50. panel1.

    Controls

    .

    Add

    (

    lblDayz)
  51. Next
  52. x =

    0
  53. ndayz =

    0
  54. y =

    0
  55. Catch

    et As

    FormatException
  56. MessageBox.

    Show

    (

    "Invalid date has been entered"

    )
  57. textBoxYear.

    Focus

    (

    )
  58. Catch

    ex As

    NullReferenceException
  59. MessageBox.

    Show

    (

    "Invalid date has been entered"

    )
  60. textBoxYear.

    Focus

    (

    )
  61. End

    Try
  62. End

    Sub

For displaying the previous date, we will code for Button2_click. Have this code below:

  1. Private

    Sub

    button2_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    button2.

    Click
  2. Try
  3. Dim

    currentmonth, currentyear As

    Int32
  4. currentyear =

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )
  5. currentmonth =

    Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )
  6. If

    currentmonth =

    1

    Then
  7. 'go to the previous month
  8. currentyear -=

    1
  9. 'go to the last month
  10. currentmonth =

    12
  11. textBoxYear.

    Text

    =

    currentyear.

    ToString

    (

    )
  12. comboBoxMonth.

    Text

    =

    currentmonth.

    ToString

    (

    )
  13. Else
  14. 'go to the previous month
  15. currentmonth -=

    1
  16. comboBoxMonth.

    Text

    =

    currentmonth.

    ToString

    (

    )
  17. End

    If
  18. 'remove all the controls in the panel
  19. panel1.

    Controls

    .

    Clear

    (

    )

  20. My.

    Application

    .

    ChangeCulture

    (

    CurrentCulture)
  21. 'display the selected month's fullname
  22. labelMonth.

    Text

    =

    Application.

    CurrentCulture

    .

    DateTimeFormat

    .

    GetMonthName

    (

    currentmonth)
  23. My.

    Application

    .

    ChangeCulture

    (

    "en-za"

    )
  24. Dim

    Dayz As

    Int32 =

    DateTime.

    DaysInMonth

    (

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )

    , Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )

    )
  25. CheckDay(

    )
  26. For

    i As

    Int32 =

    1

    To

    Dayz
  27. ndayz +=

    1
  28. lblDayz =

    New

    Label(

    )
  29. lblDayz.

    Text

    =

    i.

    ToString

    (

    )
  30. lblDayz.

    BorderStyle

    =

    BorderStyle.

    Fixed3D
  31. Dim

    mon As

    Int32 =

    Convert.

    ToInt32

    (

    comboBoxMonth.

    Text

    )
  32. Dim

    years As

    Int32 =

    Convert.

    ToInt32

    (

    textBoxYear.

    Text

    )
  33. If

    (

    (

    i =

    DateTime.

    Now

    .

    Day

    )

    And

    (

    mon =

    DateTime.

    Now

    .

    Month

    )

    And

    (

    years =

    DateTime.

    Now

    .

    Year

    )

    )

    Then
  34. 'the current day must be highlighted differently
  35. lblDayz.

    BackColor

    =

    Color.

    Green
  36. ElseIf

    ndayz =

    1

    Then
  37. 'highlight the sunday's in red color
  38. lblDayz.

    BackColor

    =

    Color.

    Red
  39. Else
  40. 'set this color for other days in the selected month
  41. lblDayz.

    BackColor

    =

    Color.

    Aquamarine
  42. End

    If
  43. lblDayz.

    Font

    =

    label31.

    Font
  44. lblDayz.

    SetBounds

    (

    x, y, 37

    , 27

    )
  45. x +=

    42
  46. If

    (

    ndayz =

    7

    )

    Then
  47. x =

    0
  48. ndayz =

    0
  49. y +=

    29
  50. End

    If
  51. panel1.

    Controls

    .

    Add

    (

    lblDayz)
  52. Next
  53. x =

    0
  54. ndayz =

    0
  55. y =

    0
  56. Catch

    er As

    FormatException
  57. MessageBox.

    Show

    (

    "Invalid date has been entered"

    )
  58. textBoxYear.

    Focus

    (

    )
  59. Catch

    ex As

    NullReferenceException
  60. MessageBox.

    Show

    (

    "Invalid date has been entered"

    )
  61. textBoxYear.

    Focus

    (

    )
  62. End

    Try
  63. End

    Sub

Output:

calendarout.png


For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,360

324,368

Top