ComponentOne GanttView for WinForms
Assigning Resources to a Task
GanttView for WinForms Task-Based Help > Assigning Resources to a Task

You can assign resources to a specific task first by creating the resource and then assigning the resource to the specific task. Resources can be created at design time, run time, or programmatically. At run time they can be created using the Project Resources dialog box and then assigned to the task using the Task Information dialog box. At design time they can be created using the C1GanttView.Resources Collection Editor and then assigned to the task using the Task.ResourceRefs Collection Editor. Resources can also be created programmatically using the Resource class and Add method.

Add a resource to task1 at design time

To add a resource to task1 at design time, complete the following:

  1. Click on the smart tag to open the C1GanttView Tasks menu.
  2. Click on Edit Resources to open the C1GanttView.Resources Collection Editor.
  3. The C1GanttView.Resources Collection Editor appears.
  4. Click the Add button to add a resource to the collection.
  5. Set Resource 1 Name to Resource 1.
  6. Click OK to save and close the C1GanttView.Resources Collection Editor.
  7. Right-click on the control and select Edit Tasks.
  8. The C1GanttView.Tasks Collection Editor appears.
  9. Select the task1 task and click on the ellipsis button next to ResourceRefs.
  10. The Task.ResourceRefs Collection Editor appears.
  11. Click Add to add a reference to Resource 1.
  12. Set the Resource to Resource 1.
  13. Click OK to save and close the Task.ResourceRefs Collection Editor.

Add a resource to task1 programmatically

To programmatically add a resource to task1, complete the following:

To write code in Visual Basic

Visual Basic
Copy Code
Private Sub btnAddResource_Click(sender As Object, e As EventArgs)
   ' add the new Resource object
   Dim r As New Resource()
   r.Name = "Resource 1"
   r.Cost = 300D
   ganttView.Resources.Add(r)

   ' find task1
   Dim task1 As Task = ganttView.Tasks.Search("Task 1")
   If task1 IsNot Nothing AndAlso r IsNot Nothing AndAlso task1.ResourceRefs.Count = 0 Then
          ' add a resource reference to the task
          Dim rRef As New ResourceRef()
          rRef.Resource = r
          rRef.Amount = 0.5
          task1.ResourceRefs.Add(rRef)
   End If
End Sub

To write code in C#

C#
Copy Code
private void btnAddResource_Click(object sender, EventArgs e)
{
    // add the new Resource object
    Resource r = new Resource();
    r.Name = "Resource 1";
    r.Cost = 300m;
    ganttView.Resources.Add(r);
 
    // find task1
    Task task1 = ganttView.Tasks.Search("Task 1");
    if (task1 != null && task1.ResourceRefs.Count == 0)
    {
        // add a resource reference to the task
        ResourceRef rRef = new ResourceRef();
        rRef.Resource = r;
        rRef.Amount = 0.5;
        task1.ResourceRefs.Add(rRef);
    }
}
See Also