Question Problem with my code by programing a calendar

Tobi

New member
Joined
Jul 24, 2021
Messages
3
Programming Experience
3-5
Hi,

I'm trying to program a calendar. It works but there is a little bug. The Appointments have the right day but when I switch the month they already stand in the day.

I would be really thankful when anybody could help me with this. I'm german so my variables are not in English but I hope you still understand everything.

Here is my Code:

Thank you for helping me!

C#:
private void laden(int anzahl)
        {
            int x = 15;
            int y = 15;
            KFPliste.Clear();
            for (int i = 1; i <= anzahl;i++)
            {
                FlowLayoutPanel KPTag = new FlowLayoutPanel();
                KPTag.Name = "KFPTag"+i;
                KPTag.Size = new Size(120,66);
                KPTag.BackColor = Color.White;
                KPTag.BorderStyle = BorderStyle.FixedSingle;
                //KPTag.AutoScroll = true; Falls mehrere Termine benötigt werden sollten und dies essentiel ist
                
              
                KPTag.Location = new Point(x,y);
                x = x + 125;
                if(x >= 890)
                {
                    x = 15;
                    y = y + 70;
                }
                KPFelder.Controls.Add(KPTag);
                KFPliste.Add(KPTag);
            }
        }

        private void datumladen(int start,int ende)
        {
            foreach (FlowLayoutPanel KPTage in KFPliste)
            {
                KPTage.Controls.Clear();
            }
            for(int i = 1;i <= ende;i++)
            {
                Label lbl = new Label();
                lbl.Name = "KL"+i;
                lbl.AutoSize = false;
                lbl.TextAlign = ContentAlignment.MiddleRight;
                lbl.Size = new Size(118,18);
                lbl.Location = new Point(0,0);
                lbl.Text = ""+i;
                KFPliste[(i-1)+(start-1)].Controls.Add(lbl);
            }
        }

        public void datum()
        {
            SLJahr.Text = DatumZeit.ToString("yyyy");
            SLMonat.Text = DatumZeit.ToString("MMMM");
            int erstertagnummer = ersterTagMonat();
            int komplettertag = letzterTagMonat();
            datumladen(erstertagnummer,komplettertag);
            terminladen(erstertagnummer);
        }

        private void zurückMonat()
        {
            DatumZeit = DatumZeit.AddMonths(-1);
            datum();
        }

        private void vorwärtsMonat()
        {
            DatumZeit = DatumZeit.AddMonths(+1);
            datum();
        }

        private void SBtMonatZurueck_Click(object sender, EventArgs e)
        {
            zurückMonat();
        }
        private void SBtMonatVor_Click(object sender, EventArgs e)
        {
            vorwärtsMonat();
        }

        private void heute()
        {
            DatumZeit = DateTime.Today;
            datum();
        }

        private int ersterTagMonat()
        {
            DateTime ersterTagMonat = new DateTime(DatumZeit.Year,DatumZeit.Month,1);
            return (int)(ersterTagMonat.DayOfWeek +1);
        }
        private int letzterTagMonat()
        {
            DateTime ersterTag = new DateTime(DatumZeit.Year, DatumZeit.Month, 1);
            return ersterTag.AddMonths(1).AddDays(-1).Day;
        }

      
        private void terminladen(int starttag)
        {
            DateTime startdatum = new DateTime(DatumZeit.Year,DatumZeit.Month,1);
            DateTime enddatum = startdatum.AddMonths(1).AddDays(-1);
            string sqltext = "SELECT * FROM Termine WHERE TerminAnfang BETWEEN '" + startdatum.ToShortDateString() + "' AND '" + enddatum.ToShortDateString() + "'";
            DataTable datatable = sqladapter(sqltext);

            foreach(DataRow row in datatable.Rows)
            {
                DateTime TerminTag = DateTime.Parse((string)row["TerminAnfang"]);
                LinkLabel label = new LinkLabel();
                label.Name = "label" + (int)row["IdTermin"];
                label.Text = row["TerminName"].ToString();
                KFPliste[(TerminTag.Day-1) + (starttag - 1)].Controls.Add(label);
            }
        }

        private DataTable sqladapter(string sqltext)
        {
            SqlConnection cn = new SqlConnection(cn_string);
            cn.Open();
                
            SqlDataAdapter sql_adapt = new SqlDataAdapter(sqltext,cn);

            DataTable tblData = new DataTable();
            sql_adapt.Fill(tblData);
            return tblData;
        }
    }

Here is how I insert the date:
cn = new SqlConnection(cn_string);
                cn.Open();
                string sqltext = "INSERT INTO Termine ([TerminName],[TerminBeschreibung],[TerminAnfang],[TerminFarbe]) " +
                                     "VALUES ('" + TeTbName.Text + "','" + TeTbBeschreibung.Text + "','" + TeDtpDatum.Value.ToShortDateString() + "','"+farbe+"')";
                cmd = new SqlCommand(sqltext, cn);
                cmd.ExecuteNonQuery();
                this.Hide();
 
The Appointments have the right day but when I switch the month they already stand in the day.
Can you explain this problem you are encountering in a little bit more detail? Do you have some screenshots of your program running that shows the problem?
 
Can you explain this problem you are encountering in a little bit more detail? Do you have some screenshots of your program running that shows the problem?
Yeah of course. I created an appointment for 24.07.21. You can see the LinkLabel for it down below in the picture. It#s in the right box. In the next picture, I switched the month to august but the appointment is still on day 24. I hope you understand now. I'm really sure I found the problem but I don't know how to fix it. In line 99 the program should differentiate between the months to give the only appointments in this month. This isn't working the program gives every appointment through so the appointment stands in every month. Now I need the best way to fix this. I have a few ideas but maybe you know the best way.

2021-07-24 (1).png


2021-07-24 (2).png
 
Back
Top Bottom