-1

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;
            }
        }
 
  • Does this answer your question? [Get a Windows Forms control by name in C#](https://stackoverflow.com/questions/1536739/get-a-windows-forms-control-by-name-in-c-sharp) – GSerg Feb 15 '23 at 09:45
  • Hello @GSerg ! I have tried your suggestion, but get errors. Have written in Update-1 under my question –  Feb 15 '23 at 10:08
  • You have glanced over the single line of code, [`Controls.Find()`](https://stackoverflow.com/a/1536756/11683), and dumped it inside the expression without understanding what it is and [how to use it](https://stackoverflow.com/questions/1536739/get-a-windows-forms-control-by-name-in-c-sharp/1536756#comment13044825_1536756), or even whether it is the more fitting of the [solutions presented](https://stackoverflow.com/a/1639106/11683)? – GSerg Feb 15 '23 at 10:10
  • Sorry, but I didn't get you. I have looked to all the solutions in your link. But I cannot overcome the Errors. I think they are not suitable for my case. Could you please explaine if you see anything wrong? –  Feb 15 '23 at 11:17

1 Answers1

0

I am not sure whether this suits your needs, but you could derive the target chart and tabPage using a switch expression, and then use them for drawing the chart and updating the selected tab:

(Chart chart, TabPage tabPage) = step_no switch
{
    "1" => (chart1, tabPage1),
    "2" => (chart2, tabPage2),
    "3" => (chart3, tabPage3),
    _ => (defaultChart, defaultTabPage),
};

Draw_Chart(chart);
tabControl1.SelectedTab = tabPage;

Here, I am assuming that you have fallback values for the chart and the tab page, if step_no does not match any of the values it is compared to (defaultChart, defaultTabPage). Depending on your use case, you may rather want to return null or some other objects for the default case (_).

Astrid E.
  • 2,280
  • 2
  • 6
  • 17