Hello guys,

I am making a calendar. My problem is that it when I click the Forward button, it does not display the calendar in an instant... it does not behave like the calendar in Windows 7...
There is a delay before the calendar is displayed.

Is there a way for it to solve my problem...?
Please help me guys...

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace test2
{
    public partial class Form1 : Form
    {
        string connString = "server=127.0.0.1;database=sample;uid=root;password=march15";        
        public int currentYear, currentMonth, lastDay, firstDay;

        public Form1()
        {
            InitializeComponent();
            initializeCalendar();
        }

        private void lblClick(System.Object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            MessageBox.Show(ctrl.Name);
        }

        private void lblMouseHover(System.Object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            ctrl.ForeColor = Color.Blue;
            //ctrl.BackColor = Color.White;
        }

        private void lblMouseLeave(System.Object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            ctrl.ForeColor = Color.Black;
            //ctrl.BackColor = Color.Black;
        }

        public void initializeCalendar()
        {
            int initYear, initMonth;
            MySqlConnection conn = new MySqlConnection(connString);            

            try
            {
                conn.Open();
                string query = "SELECT year(now()) as initYear, month(now()) as initMonth";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    initYear = Convert.ToInt32(reader["initYear"].ToString());
                    initMonth = Convert.ToInt32(reader["initMonth"].ToString());                    

                    displayCalendar(initYear,initMonth);                   
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();      
        }

        public void displayCalendar(int year, int month)
        {
            MySqlConnection conn = new MySqlConnection(connString);
            try
            {
                conn.Open();
                string query = "SELECT monthname('" + year + "-" + month + "-01') as monthName, year('" + year + "-" + month + "-01') as currentYear, dayofmonth(last_day('" + year + "-" + month + "-01')) as lastDay, month('" + year + "-" + month + "-01') as currentMonth";
                MySqlCommand cmd = new MySqlCommand(query, conn);
                MySqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    currentYear = Convert.ToInt32(reader["currentYear"].ToString());
                    currentMonth = Convert.ToInt32(reader["currentMonth"].ToString());
                    lastDay = Convert.ToInt32(reader["lastDay"].ToString());

                    lblMonth.Text = reader["monthName"].ToString() + " " + reader["currentYear"].ToString();
                }
                reader.Close();

                query = "SELECT dayofweek('" + currentYear + "-" + currentMonth + "-01') as firstDay";
                cmd = new MySqlCommand(query, conn);
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    firstDay = Convert.ToInt32(reader["firstDay"].ToString());
                    //MessageBox.Show(currentYear + "," + currentMonth + "," + lastDay + "," + firstDay);

                    int dayLabel = 0;
                    for (var i = 1; i <= (lastDay + firstDay - 1); i++)
                    {
                        Label lbl = new Label();
                        if (i >= firstDay)
                        {
                            dayLabel++;

                            lbl.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                            lbl.Size = new System.Drawing.Size(36, 23);
                            lbl.ForeColor = System.Drawing.Color.Black;
                            //lbl.BackColor = System.Drawing.Color.Black;
                            lbl.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
                            lbl.Name = "day_" + dayLabel.ToString();
                            lbl.Text = dayLabel.ToString();
                            lbl.Cursor = Cursors.Hand;
                            lbl.Visible = true;
                            lbl.Click += new System.EventHandler(this.lblClick);
                            lbl.MouseHover += new EventHandler(this.lblMouseHover);
                            lbl.MouseLeave += new System.EventHandler(this.lblMouseLeave);
                        }
                        else
                        {
                            lbl.Name = "day_0";
                        }

                        tableLayoutPanel1.Controls.Add(lbl);
                        tableLayoutPanel1.Show();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();
        } 

        private void lblNext_Click(object sender, EventArgs e)
        {
            currentMonth++;
            tableLayoutPanel1.Hide();
            tableLayoutPanel1.Controls.Clear();
            displayCalendar(currentYear, currentMonth);
        }

        private void lblPrevious_Click(object sender, EventArgs e)
        {
            currentMonth--;            
            tableLayoutPanel1.Controls.Clear();
            displayCalendar(currentYear, currentMonth);
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void lblGoToDate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            tableLayoutPanel1.Controls.Clear();
            initializeCalendar();
        }
    }    
}

Recommended Answers

All 6 Replies

It looks like every time you display the calendar you make a couple of database calls. This will slow down the display. Why do you make these calls?

the database queries are the date and time functions in MySQL...that is used in making the calendar...

Why don't you use the date/time functions of C#/.NET?

Although I personally don't like it, maybe the MonthCalendar control is something for you. If you set its calendardimensions property to 4;3 you can show a calendar for a whole year.
Have a look: http://msdn.microsoft.com/en-us/library/bb760913(VS.85).aspx

yeah thanks for the replies...
But i am not just creating a calendar...I will be making my calendar as something like a diary or alarm where i can set my reminders or any important events on the specific dates...
Besides, the MonthCalendar can not be customized to my desired design...

@Momerath: I will also be using the MySQL database for the storage of the events...

I'd draw the calendar, then fetch the events and add them to the days as needed. If you set it up right, you can start another thread to do the fetching while you draw the calendar to save some time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.