11229 : Resizing the TimeItem”s Region

Question

When the GanttRow.CollisionDetect and GanttRow.IncreaseRowHeightOnCollision properties are set to true, the TimeItems that are not in a collision-area are shown as filling the entire height of the now expanded row.
When trying to resize them using the suggested solution in Q10210, the drawing itself shows a regular sized TimeItem, but the text and all the events still apply to the old Region, which covers the entire height of the row.
How do I redefine the TimeItem’s Region so that I will have a regular-sized and regular-behaving TimeItem while still enjoing the benefits of showing the collisions on an increased row?

Answer

This is how to use the OnTimeItem_UserDrawBounds and  OnTimeItem_UserDraw together:

In the OnTimeItem_UserDraw event change the given Rect and Bounding  Region (make them as thin as you need)
Start with the rect and create a new region:
       e.Rect=rect;
       e.BoundingRegion = new Region(rect);

In the OnTimeItem_UserDrawBounds you must do the same(assign new  e.Rect and e.BoundingRegion), since this event Is fired when the  logic is finding out the size of the time item for other reasons  than drawing.

 

    private void buttonSetUpTestSizeOverride_Click(object sender, EventArgs e)
    {
      gantt1.OnTimeItem_UserDraw += new TimeItemEvent(gantt1_OnTimeItem_UserDraw);
      gantt1.OnTimeItem_UserDrawBounds += new TimeItemEvent(gantt1_OnTimeItem_UserDrawBounds);
      GridNode gn=new GridNode();
      gantt1.Grid.RootNodes.Insert(0,gn);
      GanttRow gr = GanttRow.FromGridNode(gn);
      TimeItem ti=gr.Layers[0].AddNewTimeItem();
      ti.Start=gantt1.DateScaler.StartTime.AddDays(5);
      ti.Stop=ti.Start.AddDays(10);
      ti.TimeItemLayout=ti.TimeItemLayout.Clone() as TimeItemLayout;
      ti.TimeItemLayout.TimeItemStyle=TimeItemStyle.User;
    }

    /// <summary>
    /// This event can help you override how a time item is perceived in the TimeItemFromXY (the active clickable area )
    /// </summary>
    void gantt1_OnTimeItem_UserDrawBounds(Gantt aGantt, TimeItemEventArgs e)
    {     
      GraphicsPath gp=new GraphicsPath();
      gp.AddEllipse(e.Rect);
      e.BoundingRegion=new Region(gp);
    }

    /// <summary>
    /// …and this event helps you draw something in that area.
    /// Note that the two areas can differ, its up to you, but it will a bit strange…
    /// </summary>
    void gantt1_OnTimeItem_UserDraw(Gantt aGantt, TimeItemEventArgs e)
    {
      GraphicsPath gp = new GraphicsPath();
      gp.AddEllipse(e.Rect);
      e.G.DrawPath(new Pen(Color.Blue),gp);     
    }