我正在开发的WinForm中有一个ComboBox。当ComboBox上的选择被更改时,表单将重置它所拥有的ProjectManager
,并使用PM的名称作为键从Dictionary中检索它。
ComboBox应该只是将名称传递给词典,而是传递完全不同的东西
frm_About_PM
代码:
public partial class frm_About_PM : Form
{
private string _Name;
private ProjectManager _Manager;
private Dictionary<string, Project> _Projects;
private Dictionary<string, ProjectManager> _OtherManagers;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public frm_About_PM()
{
InitializeComponent();
}
public frm_About_PM(string Name)
{
InitializeComponent();
_Name = Name;
lbl_PM_Name.Text = "Statistics - " + _Name;
}
public frm_About_PM(ProjectManager projectManager)
{
InitializeComponent();
_Manager = projectManager;
_Name = _Manager.Name;
InitiateCharts();
}
public frm_About_PM(ProjectManager projectManager, Dictionary<string, ProjectManager> OtherManagers)
{
InitializeComponent();
_Manager = projectManager;
_Name = _Manager.Name;
_OtherManagers = OtherManagers;
//Load the ComboBox
if ((_OtherManagers.Count > 0) || (_OtherManagers != null))
{
cmbo_ManagerSelect.DataSource = new BindingSource(_OtherManagers,null);
cmbo_ManagerSelect.DisplayMember = "Key";
cmbo_ManagerSelect.ValueMember = "Value";
}
InitiateCharts();
}
private void InitiateCharts()
{
lbl_PM_Name.Text = "Statistics - " + _Name + ":: " + _Manager.ProjectCount + " Projects";
Dictionary<string, double> BudgetValues = new Dictionary<string, double>();
Dictionary<string, double> ActualValues = new Dictionary<string, double>();
Dictionary<string, DateTime> StartDateValues = new Dictionary<string, DateTime>();
Dictionary<string, DateTime> EndDateValues = new Dictionary<string, DateTime>();
Dictionary<string, double> CompletionValues = new Dictionary<string, double>();
Dictionary<string, double> DurationValues = new Dictionary<string, double>();
//Load the Chart
foreach (Project P in _Manager.ProjectList.Values)
{
TimeSpan Duration = P.PlannedEnd - P.PlannedStart;
BudgetValues.Add(P.ProjectNumber, P.BudgetCost);
ActualValues.Add(P.ProjectNumber, P.ActualCost);
StartDateValues.Add(P.ProjectNumber, P.PlannedStart);
EndDateValues.Add(P.ProjectNumber, P.PlannedStart.Subtract(Duration));
CompletionValues.Add(P.ProjectNumber, P.PercentComplete);
DurationValues.Add(P.ProjectNumber, Duration.TotalDays);
}
chart_Projects.Series["Budget"].Points.DataBindXY(BudgetValues.Keys.ToArray(), BudgetValues.Values.ToArray());
chart_Projects.Series["Actual"].Points.DataBindXY(ActualValues.Keys.ToArray(), ActualValues.Values.ToArray());
chart_Completion.Series["Completion"].Points.DataBindXY(CompletionValues.Keys.ToArray(), CompletionValues.Values.ToArray());
chart_Dates.Series["Start Fill"].Points.DataBindXY(StartDateValues.Keys.ToArray(), StartDateValues.Values.ToArray());
chart_Dates.Series["Duration"].Points.DataBindXY(DurationValues.Keys.ToArray(), DurationValues.Values.ToArray());
chart_Dates.ChartAreas["Dates"].AxisY.Minimum = _Manager.EarliestStart.ToOADate();
chart_Dates.ChartAreas["Dates"].AxisY.Maximum = _Manager.LatestEnd.ToOADate();
chart_Dates.ChartAreas["Dates"].AxisX.Interval = 1;
chart_Projects.ChartAreas["BudgetChart"].AxisX.Interval = 1;
}
private void cmbo_ManagerSelect_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.Print("Manager Name is: '{0}'", cmbo_ManagerSelect.Text);
_Manager = Globals.PMO_Workbook.Controller.Managers[cmbo_ManagerSelect.SelectedText];
_Name = _Manager.Name;
InitiateCharts();
}
}
我从上一个Debug.Print
语句得到的输出是:
Manager Name is: '[John Snow, PMO_KPAT_Dashboard.Classes.ProjectManager]'
但它应该只是传递John Snow
。
答案 0 :(得分:1)
Combobox元素是KeyValuePair<string,ProjectManager>
,所以你得到的输出是合适的。你需要拥有Key
SelectedItem
属性,你可以通过这种方式实现:
Debug.Print("Manager Name is: '{0}'", ((KeyValuePair<string,ProjectManager>)comboBox1.SelectedItem).Key);