使用 C# ILOG 处理 ActivityDialog 上的 GanttChart
本文档展示了如何在 ActivityDialog 上处理甘特图。
引言
我最近参与了一个使用 C# .NET 框架提供的甘特模型开发的日程管理系统。我扩展了 ActivityDialog
并设计了一个新的对话框,用于处理特定于客户的活动、资源、约束和预订。在这个项目中,我遇到一个难题:如何在 ActivityDialog
上处理 GanttChart
。我将在本文中提供我的解决方案。
背景
我添加了一个新的 TabPage
(包含一个 DataGridView
)到 ActivityDialog
上,以便能够添加/删除/更新活动的新约束。这个 DataGridView
有两列(活动和提前期)。活动组合框列表示约束的 From-Activity。要求是在 ActivityDialog
上添加新的约束时,在 GanttChart
上显示所选活动。
使用代码
我需要一个新的组合框来处理 GanttChart
,因此我定义了 DataGridViewExtendedComboBoxColumn
类用于活动组合框。在这个类中,我重写了 InitializeEditingControl
以添加两个事件处理程序:SelectedIndexChanged
处理程序在 GanttChart
上显示所选活动。
public class ProgressActivityDialog : ILOG.Views.Gantt.Windows.Forms.ActivityDialog
{
public ProgressActivityDialog(InitParamActivityDialog param)
{
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
// When this Dialog opens
if (this.Visible)
{
// Check Return Conditions
if ((this.tabControl.TabPages == null) ||
(this.tabControl.TabPages.Count == 0) ||
(this.currentActivity == null))
{
return;
}
// Remove Default predecessorsTab on ActivityDialog
this.tabControl.TabPages.Remove(predecessorsTab);
// Kojun Activity Only Use resourcesTab on ActivityDialog
if (currentActivity.BNodeLevel != (int)ActivityNodeLevel.Kojun)
{
// Remove Default resourcesTab on ActivityDialog
this.tabControl.TabPages.Remove(resourcesTab);
}
// In case of Event Activity, Set some components of resourcesTab invisible
if (currentActivity.BEventFlg == 1)
{
this.resourcesLabel.Visible = false;
this.resourcesGrid.Visible = false;
this.resourceNewGrid.Visible = false;
}
// In case of Schedule Organization, Set New Activity's StartTime and EndTime
if (initParams.dialogType == EnumDialogType.ScheduleOrganization)
{
if ((initParams.isNewActivity) &&
(currentActivity.BNodeLevel == (int)ActivityNodeLevel.Kojun))
{
if (currentActivity.BEventFlg == 0)
{
currentActivity.StartTime = DateTime.Now.Date + new TimeSpan(8, 0, 0);
currentActivity.EndTime = DateTime.Now.Date + new TimeSpan(17, 0, 0);
currentActivity.Duration = new TimeSpan(9, 0, 0);
}
else
{
currentActivity.StartTime = DateTime.Now.Date + new TimeSpan(8, 0, 0);
currentActivity.EndTime = currentActivity.StartTime;
currentActivity.Duration = new TimeSpan(0, 0, 0);
}
this.startDateTimePicker.Value = currentActivity.StartTime;
this.finishDateTimePicker.Value = currentActivity.EndTime;
}
}
// EventHandler When TabPage of TabControl is changed
tabControl.SelectedIndexChanged +=
new EventHandler(TabControl_SelectedIndexChanged);
// EventHandler When KojunName ComboBox Selected Index is changed
kojunNameCombo.SelectedIndexChanged +=
new EventHandler(KojunNameCombo_SelectedIndexChanged);
// EventHandler When EventName ComboBox Selected Index is changed
eventNameCombo.SelectedIndexChanged +=
new EventHandler(EventNameCombo_SelectedIndexChanged);
// EventHandler When IgnoreCalendar Checkbox Checked is changed
ignoreCalendarChkBox.CheckedChanged +=
new EventHandler(IgnoreCalendarChkBox_CheckedChanged);
// Set notesTab.noteTextBox
this.noteTextBox.Clear();
if (currentActivity.BNodeLevel == (int)ActivityNodeLevel.Kojun)
{
this.noteTextBox.Text = currentActivity.BCmnt;
}
}
}
// Set Columns of PredecessorNewGrid DataGridView
private void SetPredecessorNewGridColumns()
{
predecessorsNewGrid.Columns.Clear();
// Set ExtendedComboBoxColumn for activity field
DataGridViewExtendedComboBoxColumn activityCombo =
new DataGridViewExtendedComboBoxColumn();
activityCombo.relatedGanttChart = initParams.ganttChart;
activityCombo.DataPropertyName = "Activity";
activityCombo.DisplayMember = "NODE_NM";
activityCombo.ValueMember = "NODE_ID";
SetItemsonConstraintFromActivityNameComboBoxColumn(
this.Activity.GanttModel.Activities, activityCombo);
predecessorsNewGrid.Columns.Add(activityCombo);
// Set TextBox for leadTime field
DataGridViewTextBoxColumn leadTimeText = new DataGridViewTextBoxColumn();
leadTimeText.DataPropertyName = "leadTime";
predecessorsNewGrid.Columns.Add(leadTimeText);
}
//
//
//
/// Internal Class: Define predecessorsNewGrid'S DataGridViewComboBoxCell
internal class DataGridViewExtendedComboBoxCell : DataGridViewComboBoxCell
{
public override void InitializeEditingControl(int rowIndex,
object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
if (base.DataGridView != null)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
ComboBox comboBox = base.DataGridView.EditingControl as ComboBox;
if (comboBox != null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// Add Event
comboBox.SelectedIndexChanged +=
new EventHandler(comboBox_SelectedIndexChanged);
comboBox.Leave += new EventHandler(comboBox_Leave);
}
}
}
private void comboBox_Leave(object sender, EventArgs e)
{
// Delete Event
ComboBox comboBox = base.DataGridView.EditingControl as ComboBox;
if (comboBox != null)
{
comboBox.SelectedIndexChanged -=
new EventHandler(comboBox_SelectedIndexChanged);
}
comboBox.Leave -= new EventHandler(comboBox_Leave);
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (base.DataGridView != null)
{
DataGridViewComboBoxEditingControl cbo =
sender as DataGridViewComboBoxEditingControl;
if ((cbo.Text.Trim() != string.Empty) &&
(cbo.FindStringExact(cbo.Text) != -1))
{
// Move Selected Activity to Left-Up Position on GanttSheet
DataGridViewExtendedComboBoxColumn column =
(this.DataGridView.Columns[0]) as DataGridViewExtendedComboBoxColumn;
if (column != null)
{
if (column.relatedGanttChart != null)
{
GanttChart ganttChart = column.relatedGanttChart;
ProgressActivity selectedActivity =
(ProgressActivity)ganttChart.GanttModel.FindActivity(
(string)cbo.SelectedValue);
// Expand selectedActivity
ganttChart.GanttTable.ExpandAll();
// Move to Left-Up Position
SetFirstVisibleActivityOnGanttChart(ganttChart, selectedActivity);
}
}
}
}
}
private void SetFirstVisibleActivityOnGanttChart(GanttChart ganttChart,
ProgressActivity activity)
{
// Set GanttChart's FristVisibleTime
if (activity.StartTime != DateTime.Parse("0001/1/1"))
{
ganttChart.FirstVisibleTime = activity.StartTime;
}
// Set GanttChart's FirstVisibleRow
ganttChart.FirstVisibleRow = ganttChart.GanttTable.IndexOfRow(activity);
}
}
/// Internal Class: Define predecessorsNewGrid's DataGridViewComboBoxColumn
internal class DataGridViewExtendedComboBoxColumn : DataGridViewComboBoxColumn
{
public GanttChart relatedGanttChart = null;
public DataGridViewExtendedComboBoxColumn()
{
DataGridViewExtendedComboBoxCell comboBoxCell =
new DataGridViewExtendedComboBoxCell();
// Set Cell Template
this.CellTemplate = comboBoxCell;
}
}
}
关注点
这个示例展示了一种通过在 ActivityDialog
上选择组合框元素来处理 GanttChart
的方法。