Skip to content

Navigable Gantt Chart user control

Derek Antrican edited this page Jul 22, 2019 · 1 revision

The Gantt Chart is a great control but, on its own, it doesn't have any controls to allow the user to easily navigate dates. For this I created a user control to "wrap" the gantt chart:

screenshot

The code is here:

    public partial class NavGanttChart : UserControl
    {
        public NavGanttChart()
        {
            InitializeComponent();
        }

        private void ButtonLeft_Click(object sender, EventArgs e)
        {
            OnBeginNavigate?.Invoke();

            DateTime ganttStart = ganttChart.StartDate;
            DateTime ganttEnd = ganttChart.EndDate;
            ganttChart.StartDate = ganttStart.AddDays(-7);
            ganttChart.EndDate = ganttEnd.AddDays(-7);

            ganttChart.UpdateView();
        }

        private void ButtonRight_Click(object sender, EventArgs e)
        {
            OnBeginNavigate?.Invoke();

            DateTime ganttStart = ganttChart.StartDate;
            DateTime ganttEnd = ganttChart.EndDate;
            ganttChart.StartDate = ganttStart.AddDays(7);
            ganttChart.EndDate = ganttEnd.AddDays(7);

            ganttChart.UpdateView();
        }

        private void ButtonGoTo_Click(object sender, EventArgs e)
        {
            OnBeginNavigate?.Invoke();

            DateTime targetMon = Common.GetMondayForWeek(dateTimePickerGoTo.Value);
            DateTime targetSat = Common.GetFridayForWeek(dateTimePickerGoTo.Value).AddDays(1);

            ganttChart.StartDate = targetMon;
            ganttChart.EndDate = targetSat;

            ganttChart.UpdateView();
        }

        public Chart GanttChart
        {
            get { return ganttChart; }
            set { ganttChart = value; }
        }

        public delegate void OnBeginNavigateDelegate();
        public event OnBeginNavigateDelegate OnBeginNavigate;
    }
Clone this wiki locally