在我当前的项目中,用户可以创建一个停靠在 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);
}
}