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!
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();