简体   繁体   English

更改StringCollection的排序

[英]Change sorting of StringCollection

On my current project the user can create a control that gets docked in a TableLayoutPanel. 在我当前的项目中,用户可以创建一个停靠在TableLayoutPanel中的控件。 The controls name gets saved in a StringCollection and on every start of the program the controls get recreated. 控件名称保存在StringCollection中,并且在程序每次启动时都会重新创建控件。 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. 活动部分正在工作,问题在于,下次启动程序时,控件将以旧顺序重新创建,因为它们是从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. 这意味着要更改控件的顺序,并在将来保留该顺序,我将不得不更改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. 用+1向下移动。 In the loading event i just go through the StringCollection with foreach and create the controls. 在加载事件中,我只是通过带有foreach的StringCollection并创建控件。

foreach (string line in Properties.Settings.Default.MessageStringCollection)
{
    if (!String.IsNullOrEmpty(line))
    {
        createNewMessageButton(line);
    }
}

I have not worked with properties yet, but why not creating a custom properties type eg "SortedControlsList". 我尚未使用属性,但是为什么不创建自定义属性类型,例如“ SortedControlsList”。 You could check out an implementation suggestion on codeproject 您可以在codeproject上查看实施建议

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. 我能够自己解决问题,如果我只是尝试使用已经用于常规排序并将其更改为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();
}

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 参见: https : //msdn.microsoft.com/zh-cn/library/xb5dd1f1(v=vs.110).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM