[GanttView for WinForms] タスク情報ダイアログに任意のコントロールを追加する方法

文書番号 : 81987     文書種別 : 使用方法     登録日 : 2017/02/17     最終更新日 : 2017/02/17
文書を印刷する
対象製品
ComponentOne for WinForms
詳細
C1C1GanttViewのShowDialogイベント内でControls.Addメソッドを使用し、ボタンやテキストボックスなど任意のコントロールを追加して、タスク情報ダイアログをカスタマイズすることができます。

以下に、ボタンとテキストボックスを追加し、ボタンのクリックイベントをハンドリングするコードを記載します。

◎サンプルコード(VB)
Imports C1.Win.C1GanttView
Public Class Form1
  Dim task As Task
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    C1GanttView1.Tasks.Clear()
    task = New Task()
    task.Name = "Task 1"
    task.Start = DateTime.Now
    task.Finish = DateTime.Now.AddDays(4)
    C1GanttView1.Tasks.Add(task)

    task = New Task()
    task.Name = "Task 2"
    task.Start = DateTime.Now.AddDays(4)
    task.Finish = DateTime.Now.AddDays(6)
    C1GanttView1.Tasks.Add(task)

    task = New Task()
    task.Name = "Task 3"
    task.Start = DateTime.Now.AddDays(6)
    task.Finish = DateTime.Now.AddDays(10)
    C1GanttView1.Tasks.Add(task)

    task = New Task()
    task.Name = "Task 4"
    task.Start = DateTime.Now.AddDays(10)
    task.Finish = DateTime.Now.AddDays(14)
    C1GanttView1.Tasks.Add(task)
  End Sub

  Dim f As Form
  Dim but As Button
  Dim texbox1 As TextBox

  Private Sub C1GanttView1_ShowDialog(sender As Object, e As ShowDialogEventArgs) Handles C1GanttView1.ShowDialog
    If e.DialogType = DialogType.TaskInfo Then
      f = e.Dialog

      but = New Button()
      but.Text = "My Button"
      but.Left = f.Controls(17).Left
      but.Top = f.Controls(17).Top - 50
      AddHandler but.Click, AddressOf but_Click
      f.Controls.Add(but)

      texbox1 = New TextBox()
      texbox1.Text = "MyTextBox"
      texbox1.Left = f.Controls(17).Left
      texbox1.Top = f.Controls(17).Top - 150
      texbox1.Width = 75
      f.Controls.Add(texbox1)

    End If
  End Sub

  Dim r As New Random

  Private Sub but_Click(sender As Object, e As EventArgs)
    f.BackColor = Color.FromArgb(r.Next(25, 245), r.Next(25, 245), r.Next(25, 245))
  End Sub
End Class

◎サンプルコード(C#)
using C1.Win.C1GanttView;

namespace prj_Gantt
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    C1.Win.C1GanttView.Task task;
    private void Form1_Load(object sender, EventArgs e)
    {
      c1GanttView1.Tasks.Clear();
      task = new C1.Win.C1GanttView.Task();
      task.Name = "Task 1";
      task.Start = DateTime.Now;
      task.Finish = DateTime.Now.AddDays(4);
      c1GanttView1.Tasks.Add(task);

      task = new C1.Win.C1GanttView.Task();
      task.Name = "Task 2";
      task.Start = DateTime.Now.AddDays(4);
      task.Finish = DateTime.Now.AddDays(6);
      c1GanttView1.Tasks.Add(task);

      task = new C1.Win.C1GanttView.Task();
      task.Name = "Task 3";
      task.Start = DateTime.Now.AddDays(6);
      task.Finish = DateTime.Now.AddDays(10);
      c1GanttView1.Tasks.Add(task);

      task = new C1.Win.C1GanttView.Task();
      task.Name = "Task 4";
      task.Start = DateTime.Now.AddDays(10);
      task.Finish = DateTime.Now.AddDays(14);
      c1GanttView1.Tasks.Add(task);
    }

    Form f;
    Button but = default(Button);
    TextBox texbox1 = default(TextBox);

    private void c1GanttView1_ShowDialog(object sender, ShowDialogEventArgs e)
    {
      if (e.DialogType == DialogType.TaskInfo)
      {
        f = e.Dialog;

        but = new Button();
        but.Text = "My Button";
        but.Left = f.Controls[17].Left;
        but.Top = f.Controls[17].Top - 50;
        but.Click += but_Click;
        f.Controls.Add(but);

        texbox1 = new TextBox();
        texbox1.Text = "MyTextBox";
        texbox1.Left = f.Controls[17].Left;
        texbox1.Top = f.Controls[17].Top - 150;
        texbox1.Width = 75;
        f.Controls.Add(texbox1);
      }
    }

    Random r = new Random();
    private void but_Click(object sender, EventArgs e)
    {
      f.BackColor = Color.FromArgb(r.Next(25, 245), r.Next(25, 245), r.Next(25, 245));
    }
  }
}
関連情報