在常规面板中重新排序控件 [英] Reordering controls in a regular panel
问题描述
ContolPanel类型:FlowLayoutPanel 发件人是我单击的面板.
ContolPanel is type: FlowLayoutPanel sender is a panel that i have a click on.
如何重新排序在FlowLayoutPanel中设置的用户控件的顺序?
How do i reorder the order my Usercontrols is set in my FlowLayoutPanel?
这是我将Objekts(UserControls)添加到FlowLayoutPanel中的方式
This is how i add my Objekts(UserControls) to my FlowLayoutPanel
private void button1_Click_1(object sender, EventArgs e)
{
int count = 0;
var panel = sender as Panel;
switch (panel.Name)
{
case "TypePanel":
ContolPanel.Controls.Add(new Type().Initialize(_ConnectionStr, _connection, _brugerLNr, _klinikLNr, _speciale, _ICPC, _segmenterStrings).SetModifiedCallBack(FilterModified));
break;
}
}
我将其添加为4-5次,并且有很多不同,这只是一个. 用"+"和-"按钮重新排序Thies的最佳方法是什么?
I add this like 4-5 times and have alot of diffrent once, this this is just one. What is the best method to reorder thies with a "+" and "-" button?
我将所有控件保存在List<Controls>
中,然后使用
I thouth of saveing all the controls in a List<Controls>
And then reorder them with something like
ControlList[1] = ControlList[2]
,然后将列表中的所有控件插入到FlowLayoutPanel中. 但这似乎不适合我.有一种简单的方法可以做到这一点吗?
and then inset all the controls from the list into the FlowLayoutPanel. But this just dosnt seem to work out for me. Is there a easy way of doing this smart?
推荐答案
您可以将用户控件添加到面板中,并将用户控件的Dock
属性设置为DockStyle.Top
,然后作为一个好主意更改 z顺序,使用 Parent.SetChildIndex
使其上下移动.
You can add your user controls to a panel and set Dock
property of your user controls to DockStyle.Top
, then as a good idea change z-order of user control using Parent.SetChildIndex
to move it up or down.
为此,请将以下两种方法添加到用户控件中:
To do so, add these two methods to your user controls:
public void MoveUp()
{
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);
}
public void MoveDown()
{
if (this.Parent == null)
return;
var index = this.Parent.Controls.GetChildIndex(this);
if (index > 0)
this.Parent.Controls.SetChildIndex(this, index - 1);
}
通过覆盖用户控件中的ProcessCmdKey
,您还可以支持使用 + 向上移动和使用-键向下移动:
Also you can support move up using + and moving down using - keys, by overriding ProcessCmdKey
in your user control:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Add:
this.MoveUp();
break;
case Keys.Subtract:
this.MoveDown();
break;
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
您还可以在用户控件中添加上移Button
和下移Button
并在用户控件中处理这些按钮的Click
事件:
And also you can add a move up Button
and a move down Button
in your user control and handle Click
event of those buttons in your user control:
private void MoveUpButton_Click(object sender, EventArgs e)
{
this.MoveUp();
}
private void MoveDownButton_Click(object sender, EventArgs e)
{
this.MoveDown();
}
由于我们创建了MoveUp
和MoveDown
公共目录,因此可以上下移动用户控件:
Since we created MoveUp
and MoveDown
public, You can move a user control up and down in form:
myUserControl1.MoveUp();
这篇关于在常规面板中重新排序控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!