如何在不闪烁和布局重叠的情况下恢复和最大化窗体?

How to restore and maximize a form without flickering and layout overlapping?

在我的 windows 表单应用程序中,我添加了一个 SplitContainer 控件。在 SplitContainer 的面板 1 中,我有一个列表框,在 SpliContainer 的面板 2 中,我有两个按钮。在 ListBox 的鼠标移动上,我想 select ListBox 项目。下面是我对 select 列表框项目的代码,

 private void ListBox1_MouseMove(object sender, MouseEventArgs e)
    {
        int i;
        this.SuspendLayout();
        for (i = 0; i < (this.listBox1.Items.Count); i++)
        {
            if (this.listBox1.GetItemRectangle(i).Contains(this.listBox1.PointToClient(MousePosition)))
            {
                this.listBox1.SelectedIndex = i;
                return;
            }
        }
        this.ResumeLayout(true);
    }

The SuspendLayout() and ResumeLayout() are called to avoid the overlapping of panels when the form is loading.

如果我恢复表单,将鼠标移到 ListBox 上并再次最大化表单,则 SplitContainer 面板 2 中的按钮无法正确加载。如果我删除 Suspend 和 ResumeLayout,恢复和最大化工作正常。我提到了这个 Whosebug query link.

ListBox 没有调整大小和停靠 属性。那么我不应该调用 SuspendLayout 和 ResumeLayout 吗?有人建议在哪里使用 SuspendLayout/ResumeLayout 在哪里不使用?

这样试试:

private void ListBox1_MouseMove(object sender, MouseEventArgs e)
{

    int newindex = ListBox1.IndexFromPoint(e.Location);
    if (newindex != index) //avoid flickering
    {
        int i;
        this.SuspendLayout();
        for (i = 0; i < (this.listBox1.Items.Count); i++)
        {
            if (this.listBox1.GetItemRectangle(i).Contains(this.listBox1.PointToClient(MousePosition)))
            {

                this.listBox1.SelectedIndex = i;
                index = newindex;
                //return; why return?
            }
        }
        this.ResumeLayout(true);
    }
}

只需将索引声明为全局变量即可。