JavascriptProva

lunedì 26 marzo 2018

Sviluppo di un calendario su form in VB.NET

Ecco. Ricominciamo a creare un semplice calendario...

Usare delle Labels.
Non ricordo come si creano le date in VB.

Obiettivo: creare date con somme e sottrazioni di giorni fra un mese e l'altro.
Per prima cosa ho trovato il modo di sommare le date.
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim d As New Date(2018, 3, 2)
        Debug.Print(d)
        d = d.AddDays(10)
        Debug.Print(d)
    End Sub
End Class
02/03/2018
12/03/2018
Vado su calcoli più complessi: attraverso la fine dell'anno...
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim d As New Date(2018, 12, 20)
        Debug.Print(d)
        d = d.AddDays(12)
        Debug.Print(d)
    End Sub
End Class
20/12/2018
01/01/2019
Perfetto.

Anni bisestili:
2016 (anno bisestile): sommo 9 giorni alla data del 20 febbraio:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim d As New Date(2016, 2, 20)
        Debug.Print(d)
        d = d.AddDays(9)
        Debug.Print(d)
    End Sub
End Class
20/02/2016
29/02/2016


2017 (anno non bisestile): sommo 9 giorni alla data del 20 febbraio:
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim d As New Date(2017, 2, 20)
        Debug.Print(d)
        d = d.AddDays(9)
        Debug.Print(d)
    End Sub
End Class
20/02/2017
01/03/2017
Perfetto!


Obiettivo n.2: creare una disposizione delle labels in un form secondo l'algoritmo che io chiamo "divisione intera - modulo"
Public Class Form1
    Dim mLabel As Label
    Const vertSpace = 10
    Const horSpace = 30
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For n = 0 To 29
            mLabel = New Label
            mLabel.Width = 100
            mLabel.Height = 50
            mLabel.BorderStyle = BorderStyle.FixedSingle
            mLabel.AutoSize = False
            mLabel.Left = (mLabel.Width + horSpace) * (n \ 5)
            mLabel.Top = (mLabel.Height + vertSpace) * (n Mod 5)
            Controls.Add(mLabel)
        Next


    End Sub
End Class




Bene.



Obiettivo n.3: inserire nelle labels i giorni di un mese.

Vediamo come si fa a ottenere il numero di giorni di un mese.

Ecco, ho ottenuto tutto il necessario:
Imports System.DateTime
Public Class Form1
    Dim mLabel As Label
    Const vertSpace = 10
    Const horSpace = 30
    Const perColumn = 10
    Const anno = 2016
    Const mese = 2

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim giorno As New Date(anno, mese, 1)
        For n = 0 To DaysInMonth(anno, mese) - 1
            mLabel = New Label
            mLabel.Text = giorno
            giorno = giorno.AddDays(1)
            mLabel.Width = 100
            mLabel.Height = 50
            mLabel.BorderStyle = BorderStyle.FixedSingle
            mLabel.AutoSize = False
            mLabel.Left = (mLabel.Width + horSpace) * (n \ perColumn)
            mLabel.Top = (mLabel.Height + vertSpace) * (n Mod perColumn)
            Controls.Add(mLabel)
        Next
    End Sub
End Class





Obiettivo n.4: formattare le date

Per prima cosa ho trovato il modo di scrivere per esteso il giorno della settimana.
Ma poi ho avuto l'esigenza di mettere a capo la data per isolare il nome del giorno della settimana nella riga superiore, e ho trovato Environment.newLine. Inoltre il toUpper per fare tutte maiuscole.
Ultima modifica: il mese scritto in lettere anziché in numero:
        For n = 0 To DaysInMonth(anno, mese) - 1
            mLabel = New Label
            mLabel.Text = giorno.ToString("dddd" + Environment.NewLine + "dd/MMMM/yyyy").ToUpper
            giorno = giorno.AddDays(1)
            mLabel.Width = 120
            mLabel.Height = 50
            mLabel.BorderStyle = BorderStyle.FixedSingle
            mLabel.AutoSize = False
            mLabel.Left = (mLabel.Width + horSpace) * (n \ perColumn)
            mLabel.Top = (mLabel.Height + vertSpace) * (n Mod perColumn)
            Controls.Add(mLabel)
        Next
Ed è tutto:



Per il momento mi sembra soddisfacente!

Nessun commento:

Posta un commento