I have a simple application reading IO and plotting chart on UI. There is one instance that I need to plot three graphs at a time, and to make this easier, I made a graph form where I declared all non-fixed chart settings during form load and load it to chart as settings when the graph form is called as a new child form on the main/parent form.
Here is my code to call out graph form on parent form:
System.Windows.Forms.Integration.WindowsFormsHost DemoTabContentMiniGraph1 = new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Integration.WindowsFormsHost DemoTabContentMiniGraph2 = new System.Windows.Forms.Integration.WindowsFormsHost();
System.Windows.Forms.Integration.WindowsFormsHost DemoTabContentMiniGraph3 = new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the window control.
F_CWndw_Graph FormMiniGraph1 = new F_CWndw_Graph(true, false, false, false, 0.5, 100, 0.5, 10, "Time (s)", 20, 100, 100, "Flow Rate (l/s)", 2, true, "DEMO");
F_CWndw_Graph FormMiniGraph2 = new F_CWndw_Graph(false, true, true, false, 0.5, 10, 0.5, 10, "Time (s)", 50, 250, 250, "Height (mm)", 3);
F_CWndw_Graph FormMiniGraph3 = new F_CWndw_Graph(false, false, false, true, 0.5, 10, 0.5, 10, "Time (s)", 400, 2000, 2000, "Height (mm)", 4);
// Assign the grid control as the host control's child.
DemoTabContentMiniGraph1.Child = FormMiniGraph1;
DemoTabContentMiniGraph2.Child = FormMiniGraph2;
DemoTabContentMiniGraph3.Child = FormMiniGraph3;
// Add the interop host control to the Grid
Grid_miniGraph1.Children.Add(DemoTabContentMiniGraph1);
Grid_miniGraph2.Children.Add(DemoTabContentMiniGraph2);
Grid_miniGraph3.Children.Add(DemoTabContentMiniGraph3);
Will this be a good and safe way with UI programming, knowing that each of these graphs will run a timer ticking at 500ms to do the plotting of data?
And will those private variables inside the new child forms I created out of one layout form be totally separate/independent from each other?
-
\$\begingroup\$ There appears to be something missing. This isn't a function, at most a part of it. \$\endgroup\$Mast– Mast ♦2016年01月15日 12:12:24 +00:00Commented Jan 15, 2016 at 12:12
1 Answer 1
Your code is very small for a good review, but a few things can be mentioned:
1) naming - it is better to name your variables by what they represent. E.g. DemoTabContentMiniGraph1
, 2
, 3
should can be named something like MiniGraphFS1View
, MiniGraphPs1Ps2View
etc.
Also, F_CWndw_GraphFS1
can be adjusted to Pascal/Camel notation used in C# (they look to have C++ naming style)
2) passing parameters to instances - providing many parameters to a constructor works, but it may render maintainability problems in the future:
it is very hard to know what the third
false
inF_CWndw_GraphFS1(true, false, false, false, 0.5, 100, 0.5, 10, "Time (s)", 20, 100, 100, "Flow Rate (l/s)", 2, true, "DEMO");
meansif a new parameter must be added to the graphic, you may have to change in multiple places (constructor prototype, callers have to provide the new parameter, if it does not have a default value).
A better way is to define a class named GraphOptions or similar to encapsulate your options:
class GraphOptions
{
float MinValue { get; set; }
float MaxValue { get; set; }
String XLabel { get; set; }
// other properties come here
// you can also define a constructor to assign default values
}
Your construction code will look something like this (much longer, but much easier to understand):
var FormMiniGraph1 = new F_CWndw_GraphFS1(new GraphOptions()
{
Prop1 = true,
Prop2 = false,
Prop3 = false,
MinValue = 0.5,
MaxValue = 100,
XLabel = "Time (s)"
// all other properties remain to their default value or their value does not matter for current graph
}
3) (possible) AddRange optimization:
If Children
implements IEnumerable
, AddRange
can be used
Grid_miniGraph1.Children.Add(DemoTabContentMiniGraph1);
Grid_miniGraph2.Children.Add(DemoTabContentMiniGraph2);
Grid_miniGraph3.Children.Add(DemoTabContentMiniGraph3);
becomes
Grid_miniGraph1.Children.AddRange(new List<WindowsFormsHost>() { DemoTabContentMiniGraph1, DemoTabContentMiniGraph2, DemoTabContentMiniGraph3 };
Other aspect is the harcoded number of possible graphs. Are they so different that they must be defined one by one? Do they share common functionality?
-
\$\begingroup\$ Thanks bro. I appreciate your time and knowledge shared with my post. I am ware of naming of variables though i have some reason behind why i need to name them that way. :) Regarding "passing parameters to instances - providing many parameters to a constructor works, but it may render maintainability problems in the future", you mean by maintainability problem is only the reconstruction as overall if we need to modify something right? Code-wise and performance-wise should not have any issues? \$\endgroup\$G. Pacete– G. Pacete2016年01月19日 06:04:42 +00:00Commented Jan 19, 2016 at 6:04