早上好, 我在c#中做了一个mdi项目, 从我的perent形式打电话给孩子:
(form1 parent)
private void formularioToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frmPrueba = new Form2();
frmPrueba.MdiParent = this;
frmPrueba.Text = "Form " + Convert.ToString(x = x + 1);
frmPrueba.Show();
}
我的问题是,当我创建子窗体的多个实例然后更改窗体之间的位置时,我总是会在执行以下操作时看到子窗体的相同顺序:
(form2孩子)
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x <= MdiParent.MdiChildren.Count() - 1; x++) {
MessageBox.Show(MdiParent.MdiChildren[x].Text);
}
}
如何根据当前的订单查看子表单? 对不起我的英文,谢谢大家
答案 0 :(得分:0)
将属性Index
添加到Form2
public int Index {get; set;}
然后在更改子项的位置时,将index属性设置为它的新位置。
终于
private void button1_Click(object sender, EventArgs e)
{
var children = MdiParent.MdiChildren.OrderBy(c => c.Index).ToList();
for (int x = 0; x < children.Count; x++) {
MessageBox.Show(children[x].Text);
}
}
答案 1 :(得分:0)
答案 2 :(得分:0)
我发现这个解决方案在我的目的上运作良好,但我读到EnumChildWindows更好???非常感谢!!!
dict_gen = lambda n, x: {i + 1:range(i * (x + 1), (i + 1) * (x + 1)) for i in xrange(n)}
>>>dict_gen(3, 27)
答案 3 :(得分:0)
有趣的是,我无法在不诉诸User32.dll
extern调用来自codeproject上的这篇文章:https://www.codeproject.com/Messages/4486612/Re-Mdi-Child-Window-Z-Order.aspx
代码是:
private void ShowWindowOrder_Click(object sender, EventArgs e)
{
var children = this.MdiChildren.ToList();
var sb = new StringBuilder();
foreach (var child in this.GetChildrenSortedByZOrder())
{
sb.AppendLine(child.Text);
}
MessageBox.Show(sb.ToString());
}
private delegate bool EnumChildProc(IntPtr hwnd, IntPtr lParam);
[DllImport("User32.dll")]
private static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);
private IEnumerable<Form> GetChildrenSortedByZOrder()
{
List<IntPtr> handles = new List<IntPtr>();
if (IsHandleCreated)
{
EnumChildWindows(Handle,
(hWnd, lparam) =>
{
handles.Add(hWnd);
return true;
}, IntPtr.Zero);
}
List<Form> children = new List<Form>(handles.Count);
foreach (IntPtr handle in handles)
{
Form form = FromHandle(handle) as Form;
if (form != null)
children.Add(form);
}
return children;
}