Oppure

Loading
09/01/23 2:50
A.Maury
Ciao a tutti da Maurizio
Il mio problema e questo:
Sto' tentando con l'uso di Visual Studio 2022
di creare una sorta di calendario ad oggetti; Come avevo già fatto in passato usando il VBA.

Ora per certi versi : Come formula sembra funzionare
tranne che per il fatto che mi continua a darmi errore in certe parti del listato che non so come risolvere il tutto .
C'é qualcuno di voi che se la sentirebbe gentilmente di darmi una mano in merito Grazie.
Il listato e questo:

Option Explicit On
Public Class Form1
    Dim YrNum As Long, MonthNum As Long, DayNum As Long, MonthDays As Integer, StartNum As Integer, EndNum As Integer
    Dim StartDate As Date, SelectedDate As Date, ButtonNum As Integer, DateCounter As Long, CurMonth As Boolean

    Private Sub MonthChange_Scroll(sender As Object, e As ScrollEventArgs) Handles MonthChange.Scroll

    End Sub

    Public Property WorksheetFunction As Object

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Lbl_Giorno_Mese_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Cella1_Click(sender As Object, e As EventArgs) Handles Cella1.Click
        On Error Resume Next
        Me.("Cella" & ButtonNum).ForeColor = &HFF0000
        ButtonNum = 1
        SelectedDate = Cella1.Text & "/" & MonthNum & "/" & YrNum
        SwitchBack()
    End Sub

    Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        On Error Resume Next
        'Foglio1.Range("A5:F5").ClearContents
        Me.Label1.Text = ""
        Me.Label2.Text = ""
        DateCounter = 1
        YrNum = Year(Now())
        DayNum = 1
        Me.MonthChange.Value = Month(Now())
        ButtonNum = 1
        StartDate = DayNum & "/" & MonthNum & "/" & YrNum
        Me.DateLabel.Text = Format(StartDate, "mmm yyyy")
        Me.MonthChange_Change()
    End Sub

    Private Sub MonthChange_Change()
        Throw New NotImplementedException()
    End Sub

    Private Function GetWorksheetFunction() As Object
        Return WorksheetFunction
    End Function

    Private Sub MonthChange_Change(worksheetFunction As Object)
        On Error Resume Next
        If MonthChange.Value = 13 Then
            YrNum = YrNum + 1
            MonthChange.Value = 1
        End If
        If MonthChange.Value = 0 Then
            YrNum = YrNum - 1
            MonthChange.Value = 12
        End If

        MonthNum = MonthChange.Value
        StartDate = DayNum & "/" & MonthNum & "/" & YrNum
        Dim worksheetFunction1 As Object = worksheetFunction

        StartNum = worksheetFunction1.Weekday(StartDate, 2)
        MonthDays = EoMonth(StartDate, 0) - StartDate + &H1
        DateLabel.Text = Format(StartDate, "mmm yyyy")
        UpDateDisplay()
    End Sub

    Private Function EoMonth(startDate As Date, v As Integer) As Date
        Throw New NotImplementedException()
    End Function

    Sub UpDateDisplay()
        On Error Resume Next
        Dim X As Integer

        If MonthNum = Month(Now()) And YrNum = Year(Now()) Then
            CurMonth = True
        Else
            CurMonth = False
        End If

        Me.SelectedDateLabel.Text = ""
        'Reimposta la Visualizzazione Dei Tasti *********************************
        For X = 28 To 38
            Me.("Cella" & X).Visible = True
        Next X
        For X = 1 To StartNum - 1
            Dim form1 As Form1 = Me
            Me.("Cella" & X).Visible = False
        Next X
        Dim WorksheetFunction As Object = Nothing

        For X = WorksheetFunction.Weekday(StartDate, 2) To 7
            Me.("Cella" & X).Visible = True
        Next X

        'Rietichettare il pulsanti di visualizzazione *******************************
        For X = StartNum To StartNum + MonthDays - 1
            Me.("Cella" & X).Caption = X - StartNum + 1
            If CurMonth = True And X - StartNum + 1 = Day(Now()) Then
                Me.("Cella" & X).BackColor = &H80FFFF
            Else
                If ((X - 1) Mod 7) + 1 = 6 Or ((X - 1) Mod 7) + 1 = 7 Then
                    Me.("Cella" & X).ForeColor = 1
                    Me.("Cella" & X).BackColor = &H80FF80
                Else
                    Me..("Cella" & X).ForeColor = 1
                    Me.("Cella" & X).BackColor = &H8000000F
                End If
            End If
        Next X

        'Nasconde i Tasti di fine Corsa ***********************************
        For X = StartNum + MonthDays To 38
            Me.("Cella" & X).Visible = False
        Next X
    End Sub

    Private Function Day([date] As Date) As Integer
        Throw New NotImplementedException()
    End Function

    Sub SwitchBack()
        On Error Resume Next
        Me.("Cella" & ButtonNum).ForeColor = &HFF&
        SelectedDateLabel.Text = Format(SelectedDate, "dddd dd mmmm yyyy")
        Me.Label1.Text = Format(SelectedDate, "dddd dd mmmm yyyy")
        Me.Label2.Text = Format(SelectedDate, "mm/dd/yyyy")
        DateCounter = DateCounter + 1
        MonthChange.Focus()
    End Sub
End Class

Tutto qui Grazie da A.Maurizio
09/01/23 9:33
Snogar
Non ci hai detto che errori ti da.
aaa
09/01/23 12:41
Carlo
Mancano moltissime informazioni per testare il tuo progetto.

Che strumenti sono:

MonthChange?
Cella1?

Se pubblichi anche Form1.Designer.vb è meglio.
Oppure l'immagine del form in modo che si possa capire facilmente gli strumenti usati.

con questa istruzione cosa vorresti ottenere?

Me.("Cella" & ButtonNum).ForeColor = &HFF0000

un bottone o label indicizzato?
Ultima modifica effettuata da Carlo 09/01/23 12:43
in programmazione tutto è permesso
10/01/23 5:34
A.Maury
Ciao Carlo grazie per prima cosa per il tuo intervento
Per il resto siccome e un progettino abbastanza articolato nel suo insieme ; Ma decisamente molto semplice
Ti invio il link per scaricare il mio progetto di prova
Cosicché tu possa essere agevolato nella lettura.
Grazie infinite Saluti da A.Maurizio

Questo e il link : app.box.com/s/…

Grazie
10/01/23 18:22
Carlo
In VB .Net si programma diversamente da VBA, ho lasciato il codice il più possibile uguale al tuo ma molte sono le modifiche fatte.
Si capisce che lo hai fatto per imparare, spero che il codice che ho modificato ti sia utile per migrare a .Net. Chiedi se hai delle perplessità, le migliorie attuabili sono molteplici.
Scusa se salta fuori qualche inesattezza, non l'ho testato.
In VB .Net Me. si usa per identificare il form della classe in uso che ha lo stesso nome, non c'è differenza con il Form. di VB6, nel caso specifico del tuo codice, visto che tutti gli strumenti sono nel form principale il Me. può essere omesso.


In allegato il progetto VS, completo dell'eseguibile, zippato.

il codice VB.Net

Public Class Form1
    Dim YrNum As Long, MonthNum As Long, DayNum As Long, MonthDays As Integer, StartNum As Integer
    Dim StartDate As Date, SelectedDate As Date, ButtonNum As Integer, CurMonth As Boolean
    Dim Cella(42) As Label ' matrice di label

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Riempimento della matrice di label, usabili con l'indice come in VB6
        Dim indice As Integer = 0
        Dim offsetLEFT As Integer = 70
        Dim offsetTOP As Integer = 118
        For riga = 0 To 5
            For colonna = 0 To 6
                Cella(indice) = New Label
                Cella(indice).Top = 56 * riga + offsetTOP
                Cella(indice).Left = 77 * colonna + offsetLEFT
                Cella(indice).Size = New Size(62, 42)
                Cella(indice).BorderStyle = BorderStyle.FixedSingle
                Cella(indice).Tag = indice
                Cella(indice).TextAlign = ContentAlignment.MiddleCenter
                Me.Controls.Add(Cella(indice))
                AddHandler Cella(indice).Click, AddressOf Cella_Click
                indice += 1
                If indice > 37 Then Exit For ' le celle superflue non vengono create
            Next colonna
        Next riga
        '-------------------------------------------------

        Label1.Text = ""
        Label2.Text = ""
        YrNum = Now.Year
        DayNum = Now.Day
        MonthNum = Now.Month - 1 ' meno 1 perché verrà incrementato da ButtMESEpiu_Click
        ButtonNum = 1
        StartDate = Today
        DateLabel.Text = "Oggi " & StartDate
        ButtMESEpiu_Click(ButtMESEpiu, New EventArgs)
    End Sub

    Private Sub Cella_Click(sender As Object, e As EventArgs)
        ' su sender la label cliccata
        ' su sender.tag l'indice inserito in fase di creazione label
        sender.ForeColor = Color.Red
        SelectedDate = sender.Text & "/" & MonthNum & "/" & YrNum
        SwitchBack()
        ButtonNum = sender.tag
    End Sub

    Private Sub ButtMESEpiu_Click(sender As Object, e As EventArgs) Handles ButtMESEpiu.Click
        MonthNum += 1
        If MonthNum = 13 Then
            YrNum = YrNum + 1
            MonthNum = 1
        End If

        StartDate = 1 & "/" & MonthNum & "/" & YrNum
        UpDateDisplay()
    End Sub

    Private Sub ButMESEmeno_Click(sender As Object, e As EventArgs) Handles ButMESEmeno.Click
        MonthNum -= 1
        If MonthNum = 0 Then
            YrNum = YrNum - 1
            MonthNum = 12
        End If

        StartDate = 1 & "/" & MonthNum & "/" & YrNum
        UpDateDisplay()
    End Sub

    Private Sub ButANNOmeno_Click(sender As Object, e As EventArgs) Handles ButANNOmeno.Click
        StartDate = StartDate.AddYears(-1)
        YrNum = StartDate.Year
        UpDateDisplay()
    End Sub

    Private Sub ButANNOpiu_Click(sender As Object, e As EventArgs) Handles ButANNOpiu.Click
        StartDate = StartDate.AddYears(1)
        YrNum = StartDate.Year
        UpDateDisplay()
    End Sub

    Sub UpDateDisplay()
        StartNum = DateAndTime.Weekday(StartDate, FirstDayOfWeek.Monday) - 1 ' Domenica = 6, Lunedi = 0
        MonthDays = DateTime.DaysInMonth(YrNum, MonthNum)
        If DateTime.DaysInMonth(YrNum, 2) = 29 Then Label1.Text = "Bisestile" Else Label1.Text = ""
        Dim X As Integer

        If MonthNum = Now.Month And YrNum = Now.Year Then
            CurMonth = True
        Else
            CurMonth = False
        End If

        SelectedDateLabel.Text = StartDate.ToString("Y") ' visualizza mese e anno

        ' Le celle superflue invisibili *********************************
        For X = 0 To StartNum
            Cella(X).Visible = False
        Next X
        For X = StartNum + MonthDays - 1 To 37
            Cella(X).Visible = False
        Next X

        'Rietichettare e visualizzare le celle *******************************
        For X = StartNum To StartNum + MonthDays - 1
            Cella(X).Visible = True
            Cella(X).Text = X - StartNum + 1
            If CurMonth = True And X - StartNum + 1 = Now.Day Then
                Cella(X).ForeColor = Color.DarkBlue
                Cella(X).BackColor = Color.LightCyan
            Else
                If X Mod 7 = 6 Or X Mod 7 + 1 = 6 Then ' sabato e domenica
                    Cella(X).ForeColor = Color.DarkBlue
                    Cella(X).BackColor = Color.LightGray
                Else ' gli altri giorni
                    Cella(X).ForeColor = Color.DarkBlue
                    Cella(X).BackColor = Color.White
                End If
            End If
        Next X

        Label2.Text = StartDate
    End Sub

    Sub SwitchBack()
        Cella(ButtonNum).ForeColor = Color.DarkBlue ' ripristino colore
        SelectedDateLabel.Text = SelectedDate.ToLongDateString
        Label1.Text = "Tot Giorni = " & SelectedDate.DayOfYear.ToString
        Label2.Text = SelectedDate.DayOfWeek.ToString
    End Sub

End Class


Vedi allegato qui sotto (Calendario_Maury.zip)
Ultima modifica effettuata da Carlo 11/01/23 11:35
in programmazione tutto è permesso
11/01/23 14:24
A.Maury
Ciao Carlo scusami se ti rispondo solo ora
Ma come sempre sono stato molto impegnato con il mio lavoro, e solo ora ho potuto scaricare e provare il tuo progetto; E Devo ammettere che sei fantastico in tutti sensi
Funziona a meraviglia e con questo non potevi farmi un regalo più grande di cosi ; Per natale e tutti gli anni a venire.
Perché con questo Progetto avrò sicuramente molto da Studiare e a prendere spunto per altri progetti futuri.
Grazie ancora di tutto
Da A.Maurizio :k:
11/01/23 15:23
nessuno
Bentornato Maurizio,

l'ultima volta che eri passato da qui ci avevi scritto

"Penso Solo che voi siate dei Degerrimi Burloni ...
... Questa e la prima e l'ultima volta che Tenterò di Chiedere una qualsiasi cosa a Questo forum ...

... Pieno di boria e nulla di più Sinceri saluti da A. Maurizio"


Ci hai ripensato?
Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
11/01/23 18:49
Carlo
Postato originariamente da A.Maury:
...Funziona a meraviglia...
Da A.Maurizio :k:


Non funziona a meraviglia, se apri il calendario e sul mese corrente gennaio clicchi il giorno 5, diventa rosso, ora se incrementi di un mese febbraio e clicchi 9, non diventa rosso come dovrebbe.
E' un bug logico, se capirai perché succede e sarai in grado di correggere l'errore, significa che sei in grado di seguire il flusso del programma e come i valori cambiano all'interno delle variabili.
Correggere gli errori logici è una parte fondamentale dell'apprendimento alla programmazione.
Non ti do nessun indizio per ora, se ti va di provare e se ti seve aiuto, siamo qui.

Ripeto che altro si potrebbe fare per rendere più compatto, elegante e leggibile il programma ma come recita la mia firma: in programmazione tutto è permesso :asd:
in programmazione tutto è permesso