ComponentOne GanttView for WinForms
Adding a Predecessor Programmatically
GanttView for WinForms Task-Based Help > Creating Predecessors > Adding a Predecessor Programmatically

To programmatically add a predecessor, complete the following:

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub btnAddPredecessor_Click(sender As Object, e As EventArgs)
   ' find task1 and task2
   Dim task1 As Task = ganttView.Tasks.Search("Task 1")
   Dim task2 As Task = ganttView.Tasks.Search("Task 2")

   If task1 IsNot Nothing AndAlso task2 IsNot Nothing AndAlso task2.Predecessors.Count = 0 Then
      ' switch to auto-scheduling mode
      task2.Mode = TaskMode.Automatic

      Dim p As New Predecessor()
      p.PredecessorTask = task1
      task2.Predecessors.Add(p)

      ' restore the manual mode
      task2.Mode = TaskMode.Manual
   End If
End Sub

To write code in C#

C#
Copy Code
private void btnAddPredecessor_Click(object sender, EventArgs e)
{
    // find task1 and task2
    Task task1 = ganttView.Tasks.Search("Task 1");
    Task task2 = ganttView.Tasks.Search("Task 2");
 
    if (task1 != null && task2 != null && task2.Predecessors.Count == 0)
    {
        // switch to auto-scheduling mode
        task2.Mode = TaskMode.Automatic;
 
        Predecessor p = new Predecessor();
        p.PredecessorTask = task1;
        task2.Predecessors.Add(p);
 
        // restore the manual mode
        task2.Mode = TaskMode.Manual;
    }
}
See Also