10097 : I would expect the TimeItems displaying in alternating colors, red and blue. But still they all get the same color?

Question

I would expect the TimeItems displaying in alternating colors, red and blue. But still they all get the same color.

I have this code:

int j=1; 
private void TIDC_OnBeforeDSToTimeItem(TimeItemDataConnect aTimeItemDataConnect, TimeItemAndDSArgs args)
  {
   if (j==2)
   {
    args.TimeItem.TimeItemLayout.Color=Color.LightBlue;
    j=1;
   }
   else
   {
    args.TimeItem.TimeItemLayout.Color=Color.Red;
    j=2;
   }
  }
 
After
 
ganttrow.Layers[0].TimeItemDataConnect().OnBeforeDSToTimeItem+=new TimeItemToDSEventHandler(TIDC_OnBeforeDSToTimeItem);
in OnNodeInserted
 
I would expect the TimeItems displaying in alternating colors, red and blue. But still they all get the same color (depending on the value of j the last time  OnBeforeDSToTimeItem was called, red or blue).

Answer

With the code above you actually change the same TimeItemLayout’s color over and over.

What you want to do is to define two time item layouts; Name one “Red_TIL” and the other “BLUE_TIL” then assign these to the alternating time items.

Create the a TimeItemLayouts in your FormLoad : (you can do this in the property grid by clicking Gantt.TimeItemLayouts)

TimeItemLayout til=new TimeItemLayout();
til.TimeItemStyle=TimeItemStyle.Square;
til.BrushKind=BrushKind.GradientVertical;
til.Color=Color.FromArgb(x.Next(0,255),x.Next(0,255),x.Next(0,255));
til.GradientColor=Color.FromArgb((int)(ti.TimeItemLayout.Color.R*0.80),(int)(ti.TimeItemLayout.Color.G*0.80),(int)(ti.TimeItemLayout.Color.B*0.80));
til.Name=”Red_TIL”;

 

Then use the defined TimeItemLayouts like this in your code above
    args.TimeItem.TimeItemLayout.Color=Gantt.TimeItemLayouts.GetFromName(“Red_TIL”);

And another thing with the code above, since the TIDC_OnBeforeDSToTimeItem will be called once for Start and once for the Stop attribute, it will not work as expected.
Change the code something like this to avoid to “many” changes:

private void Form1_OnBeforeDSToTimeItem(TimeItemDataConnect aTimeItemDataConnect, TimeItemAndDSArgs args)
{
 
if (j==2 && args.PropertyDescriptorInDS.DisplayName==”Start”)
  {
     args.TimeItem.TimeItemLayout=gantt1.TimeItemLayouts.GetFromName(“TheOtherLook”);
     j=1;
  }

Leave a Reply