10885 : I have placed the horizontal scroll bar on the TimeItemArea and i want to scroll the TimeItemArea from the scroll bar.

Question

I have placed the horizontal scroll bar on the TimeItemArea and i want to scroll the TimeItemArea from the scroll bar. How’s this possible ??

Answer

Drop a HScrollBar and a Gantt to your form, then implement these events:

    private void Form1_Load(object sender, EventArgs e)
    {
      gantt1.DateScaler.LowerBound=DateTime.Today.AddDays(-365);
      gantt1.DateScaler.UpperBound = DateTime.Today.AddDays(+365);
      gantt1.DateScaler.TimeSpanSet(DateTime.Today, DateTime.Today.AddDays(30));

      // Add a scrollbar to timeitemarea

      hScrollBar1.Parent=gantt1.TimeItemArea;
      hScrollBar1.Dock=DockStyle.Bottom;
      hScrollBar1.Maximum=1000;
      hScrollBar1.Minimum=0;
           
    }

    private void gantt1_OnDateScalerScaleChangeEvent(object sender, EventArgs e)
    {
      // Update the scrollbar whenever the DateScaler change
      
      TimeSpan ts1=gantt1.DateScaler.UpperBound.Subtract(gantt1.DateScaler.LowerBound);
      TimeSpan ts2 = gantt1.DateScaler.StartTime.Subtract(gantt1.DateScaler.LowerBound);
      TimeSpan ts3 = gantt1.DateScaler.StopTime.Subtract(gantt1.DateScaler.StartTime);    

      hScrollBar1.Value = (int)(hScrollBar1.Maximum * ts2.Ticks / ts1.Ticks);
      hScrollBar1.LargeChange=(int)(hScrollBar1.Maximum * ts3.Ticks/ts1.Ticks);
    }

    private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
      // Update the DateScaler whenever the scrollbar change…
     
      TimeSpan ts1=gantt1.DateScaler.UpperBound.Subtract(gantt1.DateScaler.LowerBound);
      TimeSpan ts2 = new TimeSpan(hScrollBar1.Value * ts1.Ticks / hScrollBar1.Maximum);
      TimeSpan ts3 = gantt1.DateScaler.StopTime.Subtract(gantt1.DateScaler.StartTime);
      gantt1.DateScaler.TimeSpanSet(gantt1.DateScaler.LowerBound.Add(ts2),gantt1.DateScaler.LowerBound.Add(ts2).Add(ts3));
    }

Leave a Reply