I am creating a video parser in C# WinForms. You select files, they get parsed then a custom control (ViewWideNarrow ) is created showing the video's name, duration and so forth, which is then added to FlowLayoutPanel1.
The user then selects in what order to display said custom controls, titles alphabetically or by Bitrate of the video. My code below works just fine, but it takes a couple of seconds to reorganize around two hundred controls, and I fear on weaker machines it might take even longer, and I wish to optimize the code, or how I handle the custom controls to begin with in respect with modest memory usage. Right now I do not have leaks or high usage.
I decided to use an ID'ing system like so to save memory:
//ID of the control, and ViewWideNarrow is the custom control itself shown in the FlowLayoutPanel
public Dictionary<int, ViewWideNarrow> Lookup_VideoView = new Dictionary<int, ViewWideNarrow>();
//ID of the control and its respective video's bitrate.
public Dictionary<int, int> Lookup_Bitrate = new Dictionary<int, int>();
//ID of the control and its respective video's title.
public Dictionary<int, string> Lookup_Alphabetical = new Dictionary<int, string>();
Which I then organize accordingly to use FI-FO kind of system using:
Lookup_Alphabetical = Lookup_Alphabetical.OrderBy(item => item.Value, new ComparerAlphabet()).ToDictionary(item => item.Key, item => item.Value);
Lookup_Bitrate = Lookup_Bitrate.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
And below is the actual control organization, which is what actually takes a while to complete and is what I suspect needs working on.
private void OrganizeControls()
{
if (SortingOption == Sorting.Alphabetical)
{
int h = 0;
foreach (var item in Lookup_Alphabetical)
{
int key = item.Key;
string value = item.Value;
ViewWideNarrow View = Lookup_VideoView[key];
if (FlowLayoutPanel1.Controls.Contains(View))
{
FlowLayoutPanel1.Controls.SetChildIndex(View, h);
h++;
}
}
}
}