0

在我当前的项目中,用户可以创建一个停靠在 TableLayoutPanel 中的控件。控件名称保存在 StringCollection 中,并且在程序每次启动时都会重新创建控件。我想实现一个功能,允许用户更改控件的顺序。移动部分正在工作,问题是下次程序启动时,控件会以旧顺序重新创建,因为它们是从 StringCollection 创建的。这意味着要更改控件的顺序并保留它以备不时之需,我将不得不更改 StringCollection 的排序。有没有办法做到这一点?如果是的话,我会怎么做?

目前我会从上下文菜单中使用此代码向上移动控件:

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);

和观察。改为 +1 将其向下移动。在加载事件中,我只需使用 foreach 遍历 StringCollection 并创建控件。

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

3 回答 3

0

我还没有使用过属性,但为什么不创建自定义属性类型,例如“SortedControlsList”。您可以查看有关codeproject的实施建议

于 2018-08-13T06:09:33.080 回答
0

有时,如果我太累了,我不应该尝试解决问题,或者不睡觉就不要提出问题,或者花更多时间思考解决方案。我能够自己解决问题,如果我只是尝试使用我已经使用的进行正常排序并将其更改为 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();
}
于 2018-08-13T06:34:22.173 回答
0

您需要更新您的属性,使其在下次启动时保持您希望的状态。

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

于 2018-08-13T06:34:57.313 回答