Per fare questo ho dovuto aumentare la larghezza delle mie caselle rispetto a quella standard predeterminata.
Dim mese As Integer = 9
Dim anno As Integer = 2012
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Calcolo del numero dei giorni del mese
Dim numero As Integer
numero = DateTime.DaysInMonth(anno, mese)
'definizione della variabile oggetto
Dim casella As Label
For n = 0 To numero - 1
casella = New Label
'attribuzione delle proprietà alla label appena istanziata
With casella
.BorderStyle = BorderStyle.FixedSingle
.BackColor = Color.White
.Width = 200
.Left = 0
.Top = .Height * (n Mod numero)
.Text = WeekdayName(Weekday(CDate(n + 1 & "/" & mese & "/" & anno)), , 1) & " " & CDate(n + 1 & "/" & mese & "/" & anno)
End With
Me.Controls.Add(casella)
Next
End Sub
Ma forse potrei inserire tutto quel po' po' di codice in una funzione usando come parametro la data.Vediamo di ingegnarci.
Dim mese As Integer = 9
Dim anno As Integer = 2012
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Calcolo del numero dei giorni del mese
Dim numero As Integer
numero = DateTime.DaysInMonth(anno, mese)
'definizione della variabile oggetto
Dim casella As Label
For n = 0 To numero - 1
casella = New Label
'attribuzione delle proprietà alla label appena istanziata
With casella
.BorderStyle = BorderStyle.FixedSingle
.BackColor = Color.White
.Width = 200
.Left = 0
.Top = .Height * (n Mod numero)
.Text = GiornoDellaSettimana(CDate(n + 1 & "/" & mese & "/" & anno))
End With
Me.Controls.Add(casella)
Next
End Sub
Function GiornoDellaSettimana(ByVal d As Date) As String
Dim testo As String = WeekdayName(Weekday(d), , 1) & " " & d
Return testo
End Function
Questa funzione posso toglierla di qua per non appesantire il codice del form e metterla in un modulo a parte, magari da dedicare ad altre eventuali funzioni.
(Tolgo i commenti per evitare la scocciatura di rimarcarli in verde ogni volta)
Codice del Form (ci aggiungo anche la definizione della classe Form1):
Public Class Form1
Dim mese As Integer = 9
Dim anno As Integer = 2012
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim numero As Integer
numero = DateTime.DaysInMonth(anno, mese)
Dim casella As Label
For n = 0 To numero - 1
casella = New Label
With casella
.BorderStyle = BorderStyle.FixedSingle
.BackColor = Color.White
.Width = 200
.Left = 0
.Top = .Height * (n Mod numero)
.Text = GiornoDellaSettimana(CDate(n + 1 & "/" & mese & "/" & anno))
End With
Me.Controls.Add(casella)
Next
End Sub
End Class
Codice del modulo Funzioni.vb:
Module Funzioni
Function GiornoDellaSettimana(ByVal d As Date) As String
Dim testo As String = WeekdayName(Weekday(d), , 1) & " " & d
Return testo
End Function
End Module
E funziona ugualmente, perchè la funzione del modulo è pubblica e quindi viene "vista" dal codice del Form1.
Nessun commento:
Posta un commento