Search This Blog

Thursday, March 24, 2011

Visual Basic Program to Display the Number of Days in a Month

Write a program with a List Box that displays the months. When a month is selected, it should display the days in the month. The program should look like this: (Click to enlarge)















Here is the code:

Public Class MonthDay
    'Declare global variables
    Dim Jan As String = "31"
    Dim Feb As String = "28 or 29"
    Dim Mar As String = "31"
    Dim Apr As String = "30"
    Dim May As String = "31"
    Dim Jun As String = "30"
    Dim Jul As String = "31"
    Dim Aug As String = "31"
    Dim Sep As String = "30"
    Dim Oct As String = "31"
    Dim Nov As String = "30"
    Dim Dec As String = "31"

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Exit and close program
        Me.Close()

    End Sub


    Private Sub MonthDay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.listMonth.Items.Clear()

        'Add items to listbox
        listMonth.Items.Add("January")
        listMonth.Items.Add("February")
        listMonth.Items.Add("March")
        listMonth.Items.Add("April")
        listMonth.Items.Add("May")
        listMonth.Items.Add("June")
        listMonth.Items.Add("July")
        listMonth.Items.Add("August")
        listMonth.Items.Add("September")
        listMonth.Items.Add("October")
        listMonth.Items.Add("November")
        listMonth.Items.Add("December")

    End Sub


    Private Sub listMonth_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listMonth.SelectedIndexChanged


        'Display # of days in January if selected in label below the listbox
        If listMonth.Text = listMonth.Items(0) Then
            lblDays.Text = Jan
        ElseIf listMonth.Text = listMonth.Items(1) Then
            lblDays.Text = Feb
        ElseIf listMonth.Text = listMonth.Items(2) Then
            lblDays.Text = Mar
        ElseIf listMonth.Text = listMonth.Items(3) Then
            lblDays.Text = Apr
        ElseIf listMonth.Text = listMonth.Items(4) Then
            lblDays.Text = May
        ElseIf listMonth.Text = listMonth.Items(5) Then
            lblDays.Text = Jun
        ElseIf listMonth.Text = listMonth.Items(6) Then
            lblDays.Text = Jul
        ElseIf listMonth.Text = listMonth.Items(7) Then
            lblDays.Text = Aug
        ElseIf listMonth.Text = listMonth.Items(8) Then
            lblDays.Text = Sep
        ElseIf listMonth.Text = listMonth.Items(9) Then
            lblDays.Text = Oct
        ElseIf listMonth.Text = listMonth.Items(10) Then
            lblDays.Text = Nov
        ElseIf listMonth.Text = listMonth.Items(11) Then
            lblDays.Text = Dec
        End If

    End Sub
End Class

No comments:

Post a Comment