Gantt Chart Control


Hello tyrodevelopers, now i want to show how to do a Gantt Chart Control with C#. It is easy,...

Check this example:




First you must to crate a Library Class in your Visual Studio and add a new user control called "GanttChart".

Open the code and add this:

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

namespace TyroDeveloper.Windows.Forms
{
    public partial class GanttChart : UserControl
    {
        public GanttChart()
        {
            InitializeComponent();
        }

        DateTime start_date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        /// <summary>
        /// Displayed period start date
        /// </summary>
        [Category("Period")]
        [Description("Displayed period start date")]
        public DateTime StartDate { 
            get { return start_date; } set { start_date = value; } 
        }
        DateTime end_date = new DateTime(DateTime.Now.Year, 
            DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
        /// <summary>
        /// Displayed period end date
        /// </summary>
        [Category("Period")]
        [Description("Displayed period end date")]
        public DateTime EndDate { 
            get { return end_date; } set { end_date = value; } 
        }

        string taskTitle = "Task title here";
        public string TaskTitle { 
            get { return taskTitle; } set { taskTitle = value; } 
        }

        int taskTitleWidth = 0;
        public int TaskTitleWidth { 
            get { return taskTitleWidth; } set { taskTitleWidth = value; } 
        }

        
        public int Duration { 
            get {  TimeSpan span = end_date - start_date; return span.Days; } 
        }

        

        int periodItemWidth = 30;
        int periodItemHeight = 30;
        int periodStartX = 10;//Horizontal
        int periodStartY = 10;//Vertical

        /// <summary>
        /// Draw the period
        /// </summary>
        public void DrawPeriod() {
            int currentX = periodStartX;
            this.Controls.Clear();
            Label lblTaskTitle = new Label();
            lblTaskTitle.Name = "lblTaskTitle";
            lblTaskTitle.Text = taskTitle;
            lblTaskTitle.Location = new Point(currentX, periodStartY);
            lblTaskTitle.BackColor = Color.Silver;
            lblTaskTitle.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            lblTaskTitle.TextAlign = ContentAlignment.MiddleCenter;
            lblTaskTitle.Height = periodItemHeight;
            if (taskTitleWidth != 0) {
                lblTaskTitle.Width = taskTitleWidth;
            }
            this.Controls.Add(lblTaskTitle);
            currentX += lblTaskTitle.Width;//increase Xposition
            TimeSpan span = end_date - start_date;
            DateTime currentDate = new 
                DateTime(start_date.Year, start_date.Month, start_date.Day);
            //MessageBox.Show(String.Format("{0}",span.Days));
            for (int i=0; i <= span.Days; i++) {

                Label lbl = new Label();
                lbl.Name = String.Format("lbl{0}", i);
                lbl.Text = currentDate.Day.ToString();
                lbl.Location = new Point(currentX, periodStartY);
                lbl.AutoSize = false;
                lbl.Size = new Size(periodItemWidth, periodItemHeight);
                lbl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                lbl.TextAlign = ContentAlignment.MiddleCenter;

                if ((currentDate.DayOfWeek == DayOfWeek.Saturday) 
                    || (currentDate.DayOfWeek == DayOfWeek.Sunday)) {
                    lbl.BackColor = Color.Silver;
                }
      
                this.Controls.Add(lbl);
                currentX += periodItemWidth;
                currentDate = currentDate.AddDays(1);
                
            }
        }
        public void AddTask(Task task) {
            DateTime currentDate = start_date;
            int itemWidth = 0;
      
            int currentX = periodStartX + taskTitleWidth; 

            for (int i = 0; i <= Duration; i++)
            {
                if (currentDate == task.StartDate)
                {
                    currentX += i * periodItemWidth;//increase Width
                }
               if (currentDate >= task.StartDate) 
               { itemWidth += periodItemWidth; }
                if (currentDate == task.EndDate) { break; }
                currentDate = currentDate.AddDays(1);
            }

            

            Label lbl = new Label();
            lbl.AutoSize = false;
            lbl.Text = task.TaskTitle;


            lbl.Width = itemWidth;
           

            lbl.Height = periodItemHeight;
            lbl.BorderStyle = task.BorderStyle;
            lbl.TextAlign = ContentAlignment.MiddleCenter;
            lbl.Location = new Point(currentX, 
                (periodItemHeight * task.Row) + periodStartY);
            lbl.BackColor = task.BackColor;
            lbl.ForeColor = task.ForeColor;

            //ToolTip
            ToolTip tip = new ToolTip();
            if (tip != null)
            {
                tip.SetToolTip(lbl, task.ToolTip);
            }

            this.Controls.Add(lbl);
        }
        public void AddTaskHeader(TaskHeader taskHeader) {
            

            Label lbl = new Label();
            lbl.AutoSize = false;
            lbl.Text = taskHeader.Title;


            lbl.Width = taskTitleWidth;


            lbl.Height = periodItemHeight;
            lbl.BorderStyle = taskHeader.BorderStyle; ;
            lbl.TextAlign = ContentAlignment.MiddleCenter;
            lbl.Location = new Point(periodStartX, 
                (periodItemHeight * taskHeader.Row) + periodStartY);
            lbl.BackColor = taskHeader.BackColor;
            lbl.ForeColor = taskHeader.ForeColor;

            //ToolTip
            ToolTip tip = new ToolTip();
            if (tip != null)
            {
                tip.SetToolTip(lbl, taskHeader.ToolTip);
            }

            this.Controls.Add(lbl);
        }
    }
    public class Task{

        public Task() { }

        int row = 0;
        public int Row { get { return row; } set { row = value; } }

        DateTime start_date = 
            new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        /// <summary>
        /// Start Date of the Task
        /// </summary>

        public DateTime StartDate { 
            get { return start_date; } set { start_date = value; } 
        }
        DateTime end_date = new DateTime(DateTime.Now.Year, 
            DateTime.Now.Month, 
            DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
        /// <summary>
        /// End Date of the Task
        /// </summary>

        public DateTime EndDate { 
            get { return end_date; } set { end_date = value; } 
        }

        string taskTitle = "Task";
        public string TaskTitle { 
            get { return taskTitle; } set { taskTitle = value; } 
        }

        Color back_color = Form.DefaultBackColor;
        public Color BackColor { 
            get { return back_color; } set { back_color = value; } 
        }

        Color fore_color = Form.DefaultForeColor;
        public Color ForeColor { 
            get { return fore_color; } set { fore_color = value; } 
        }

        BorderStyle borderStyle = BorderStyle.None;
        public BorderStyle BorderStyle { 
            get { return borderStyle; } set { borderStyle = value; } 
        }
       
        public int TaskDuration { 
            get { TimeSpan span = end_date - start_date; return span.Days; }
        }

        String toolTip = "";
        public String ToolTip { 
            get { return toolTip; } set { toolTip = value; }
        }
    }

    public class TaskHeader
    {

        public TaskHeader() { }

        int row = 0;
        public int Row { 
            get { return row; } set { row = value; }
        }

        string title = "Task";
        public string Title {
            get { return title; } set { title = value; }
        }

        Color back_color = Form.DefaultBackColor;
        public Color BackColor { 
            get { return back_color; } set { back_color = value; }
        }

        Color fore_color = Form.DefaultForeColor;
        public Color ForeColor { 
            get { return fore_color; } set { fore_color = value; }
        }

        BorderStyle borderStyle = BorderStyle.None;
        public BorderStyle BorderStyle {
            get { return borderStyle; } set { borderStyle = value; } 
        }

        String toolTip = "";
        public String ToolTip { 
            get { return toolTip; } set { toolTip = value; } 
        }

    }
}


How to use:
ganttChart1.StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); 
ganttChart1.EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 
    DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));
ganttChart1.TaskTitleWidth = 200;
ganttChart1.DrawPeriod();

//TaskHeader
TaskHeader taskHeader = new TaskHeader();
taskHeader.Row = 1;
taskHeader.Title = "Room 1";
taskHeader.BackColor = Color.Lime;        
taskHeader.ToolTip = "Message to Display\nThanks\nfor\nvisite\nmy web site";
ganttChart1.AddTaskHeader(taskHeader);
           
//Add task
Task task = new Task();
task.Row = 1;
task.TaskTitle = "Jesus Dario Chirinos";
task.StartDate = new DateTime(2012, 08, 01);
task.EndDate = new DateTime(2012, 09, 03);
task.BackColor = Color.Green;
task.ForeColor = Color.White;
task.ToolTip = "HEllo\nHello";
ganttChart1.AddTask(task);

////Add task
task = new Task();
task.Row = 1;
task.TaskTitle = "Jesus Adrián";
task.StartDate = new DateTime(2012, 09, 04);
task.EndDate = new DateTime(2012, 09, 25);
task.BackColor = Color.Red;
ganttChart1.AddTask(task);

//Header
    taskHeader = new TaskHeader();
taskHeader.Row = 2;
taskHeader.Title = "Room 2";
ganttChart1.AddTaskHeader(taskHeader);

////Add task
task = new Task();
task.Row = 2;
task.TaskTitle = "Monica Arriaga de la Garza";
task.StartDate = new DateTime(2012, 08, 1);
task.EndDate = new DateTime(2012, 10, 20);
int x = task.TaskDuration;
task.BackColor = Color.Yellow;
ganttChart1.AddTask(task);

The result:






6 comentarios:

  1. Can you tell me where should I put the second part of code? Because I created new class GanttChart, but I don't know where induce second part. By the way what kind of class is ganttChart1? Because when I try call private DateTime ganttChart1 I've got errors.

    Good job!

    ResponderEliminar
  2. please send me a picture of ypu error to tyrodeveloper@gmail.com. Regards

    ResponderEliminar
  3. It's necesary add the ControlUser to the form:
    GanttChart.GanttChart ganttChart1 = new GanttChart.GanttChart();
    this.Controls.Add(ganttChart1); <<<<<<<<<<
    ganttChart1.StartDate= ...

    ResponderEliminar
  4. Bien !, Funciona (it works) !!
    Gracias

    ResponderEliminar
  5. Hi! How update it for Visual Studio 2015? Have you example?

    ResponderEliminar
  6. I know this is old but it would be great if you could tell us where to put the second part of the code you provided. Thanks

    ResponderEliminar