0

On my current project the user can create a control that gets docked in a TableLayoutPanel. The controls name gets saved in a StringCollection and on every start of the program the controls get recreated. I would like to implement a feature that allows the user to change the order of the controls. The moving part is working, the problem is that next time the program gets started, the controls get recreated in the old order because they get created from the StringCollection. That means to change the order of the controls and keep that for the future i would have to change the sorting of the StringCollection. Is there any way to do that? And if yes, how would i go about that?

Currently i would move the control up with this code from a context menu:

if (this.Parent == null)
    return;

var index = this.Parent.Controls.GetChildIndex(this);
if (index <= this.Parent.Controls.Count)
    this.Parent.Controls.SetChildIndex(this, index - 1);

and obv. move it down with +1 instead. In the loading event i just go through the StringCollection with foreach and create the controls.

foreach (string line in Properties.Settings.Default.MessageStringCollection)
{
    if (!String.IsNullOrEmpty(line))
    {
        createNewMessageButton(line);
    }
}
Backslash
  • 99
  • 1
  • 10

3 Answers3

0

I have not worked with properties yet, but why not creating a custom properties type e.g. "SortedControlsList". You could check out an implementation suggestion on codeproject

  • It would be useless work to create a new type a property if an existing one can already do everything i need. I just have a lot of strings that need to be saved and loaded. All of that is possible with the StringCollection. I found a solution to fix the problem. For some reason i couldnt find anything about IndexOf before. Now that i found it, its easy to get the index, remove the string and re-add it to the correct place. – Backslash Aug 13 '18 at 06:40
0

Sometimes i should either not try so solve problems if i'm too tired or just not create questions without sleeping or spending more time on thinking about a solution. I was able to solve the problem on my own, the solution is pretty easy if i just try to use what i'm already using for the normal sorting and changing that to the StringCollection.

var SCindex = Properties.Settings.Default.MessageStringCollection.IndexOf(Message);
if (SCindex > 0)
{
    Properties.Settings.Default.MessageStringCollection.Remove(String.Format("{0}", Message));
    Properties.Settings.Default.MessageStringCollection.Insert(SCindex - 1, Message);
    Properties.Settings.Default.Save();
}
Backslash
  • 99
  • 1
  • 10
0

You'll need to update your property for it to remain the way you want it to on the next startup.

see: https://msdn.microsoft.com/en-us/library/xb5dd1f1(v=vs.110).aspx

Campiotti
  • 205
  • 1
  • 10
  • The only thing i'm saving is a string, nothing else. Because i'm not changing anything about the string, i dont need to update anything to that. I just had to change the index inside of the StringCollection to solve that problem. I added how i solved my problem as an answer. For some reason i cant accept that as the correct answer yet. But thank you for trying to solve my problem! – Backslash Aug 13 '18 at 06:38