C# Winform 甘特图






4.69/5 (20投票s)
C# Winforms 甘特图
引言
我工作中需要一个简单的甘特图,但发现针对 C# 和 Winforms 的例子几乎没有。不过,我在 Visual Basic 中找到一个不错的例子,虽然我不常用 Visual Basic,但我决定将其转换为 C#。
背景
我在 2008 年 10 月 2 日发现 Adagio.81 发布的甘特图,并以此为起点。你可以在 Gantt-Chart 看到它。这个甘特图具有 Adagio81 的甘特图的所有相同功能,但我修复了一些小问题,并在移动条形图后更新了其值,你可以通过工具提示看到新值。
Using the Code
我在 Form1.cs 中添加了我的代码。在 Load
中有三个示例,你还必须在那里连接你的事件。
//first Gantt Chart
ganttChart1 = new GanttChart();
ganttChart1.AllowChange = false;
ganttChart1.Dock = DockStyle.Fill;
ganttChart1.FromDate = new DateTime(2015, 12, 12, 0, 0, 0);
ganttChart1.ToDate = new DateTime(2015, 12, 24, 0, 0, 0);
tableLayoutPanel1.Controls.Add(ganttChart1, 0, 1);
ganttChart1.MouseMove += new MouseEventHandler(ganttChart1.GanttChart_MouseMove);
ganttChart1.MouseMove += new MouseEventHandler(GanttChart1_MouseMove);
ganttChart1.MouseDragged += new MouseEventHandler(ganttChart1.GanttChart_MouseDragged);
ganttChart1.MouseLeave += new EventHandler(ganttChart1.GanttChart_MouseLeave);
ganttChart1.ContextMenuStrip = ContextMenuGanttChart1;
List<BarInformation> lst1 = new List<BarInformation>();
lst1.Add(new BarInformation("Row 1", new DateTime(2015, 12, 12),
new DateTime(2015, 12, 16), Color.Aqua, Color.Khaki, 0));
lst1.Add(new BarInformation("Row 2", new DateTime(2015, 12, 13),
new DateTime(2015, 12, 20), Color.AliceBlue, Color.Khaki, 1));
lst1.Add(new BarInformation("Row 3", new DateTime(2015, 12, 14),
new DateTime(2015, 12, 24), Color.Violet, Color.Khaki, 2));
lst1.Add(new BarInformation("Row 2", new DateTime(2015, 12, 21),
new DateTime(2015, 12, 22, 12, 0, 0), Color.Yellow, Color.Khaki, 1));
lst1.Add(new BarInformation("Row 1", new DateTime(2015, 12, 17),
new DateTime(2015, 12, 24), Color.LawnGreen, Color.Khaki, 0));
foreach (BarInformation bar in lst1)
{
ganttChart1.AddChartBar
(bar.RowText, bar, bar.FromTime, bar.ToTime, bar.Color, bar.HoverColor, bar.Index);
}