I have n similar charts in my C# form app (chart1,chart2,chart3...chartn). These charts are located in the tabs of a tab control. All charts are managed over one method (Draw_Chart). I am calling the method with the related "chart" parameter like code below. The method side is OK, but for calling the chart I am currently using a "switch" control. For each "step_no" (process step) in my program I have to call the related chart. As I have a lot of steps and therefore a lot of charts the switch control has lot of cases that takes too much place in my code page. How can I shorten this part of my code?
Is it possible to shorten the cases with a loop or should I use another mechanism?
My greatest challanges for using a loop are:
1-Programming the "chart" parameter dynamically as chart1,chart2...
2-Programming the tabPage again dynamically as tabPage1,tabPage2,...
public method call_chart()
{
//This part I would like to program with loop in anyway...
switch (step_no)
{
case "1":
Draw_Chart(chart1);
tabControl1.SelectedTab = tabPage1;
break;
case "2":
Draw_Chart(chart2);
tabControl1.SelectedTab = tabPage2;
break;
case "3":
Draw_Chart(chart3);
tabControl1.SelectedTab = tabPage3;
break;
// several more case parts
}
}
public void Draw_Chart(Chart chart)
{
chart.BackColor = Color.Gray;
chart.ChartAreas[0].AxisX.Title = ".";
chart.ChartAreas[0].AxisY.Title = "Current (% or A)";
chart.ChartAreas[0].AxisY.TitleForeColor = Color.Cyan;
// rest part of chart definition.
}
Update-1
I have tried to shorten my code as below. BUt get following compile Errors:
for first row: error CS1503: Argument 1: cannot convert from 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.DataVisualization.Charting.Chart'
for second row: error CS1061: 'Control[]' does not contain a definition for 'SelectedTab' and no accessible extension method 'SelectedTab' accepting a first argument of type 'Control[]' could be found (are you missing a using directive or an assembly reference?)
for(int n=1; n<=step_no_max; n++)
{
if (step_no == n)
{
Draw_Chart(this.Controls.Find("chart"+n.ToString(),true));
this.Controls.Find("tabControl"+n.ToString(),true).SelectedTab= tabPage1;
}
}