多次控制调整大小事件触发

时间:2018-09-25 16:22:17

标签: c# .net winforms events resize

我在Resize上设置了FlowLayoutPanel事件,这是其中的代码:

private void OnFlowPanelResize(object sender, EventArgs e)
{
    if (FlowLayoutPanelSize != Size.Empty && FlowLayoutPanelSize != FlowLayoutPanel.Size)
    {
        foreach (Control c in FlowLayoutPanel.Controls)
        {
            double w = c.Width + c.Margin.Left + c.Margin.Right - 1;
            double procentualno = w / FlowLayoutPanelSize.Width * 100;

            //This line is problem
            c.Width = Convert.ToInt32(((decimal)FlowLayoutPanel.Width * (decimal)procentualno));
            int a = c.Width;
        }
    }
    FlowLayoutPanelSize = FlowLayoutPanel.Size;
}

如您所见,我评论了有问题的行。问题在于,当断点输入void并首先通过if事件(仅针对安全代码)时,它将进入foreach循环,并且在进入注释行之前一直正常。当到达那里时,它不会继续,而是返回到void的开头并传递一次,然后正常循环。

问题在于,回到空白状态会拉出一些异常值。

我尝试在2个单独的事件中进行此操作。 ResizeSizeChanged像这样:

private void OnFlowPanelResize(object sender, EventArgs e)
{
    if (FlowLayoutPanelSize != Size.Empty && FlowLayoutPanelSize != FlowLayoutPanel.Size)
    {

    }
    FlowLayoutPanelSize = FlowLayoutPanel.Size;
}
private void OnFlowPanelSizeChanged(object sender, EventArgs e)
{
    foreach (Control c in FlowLayoutPanel.Controls)
    {
        double w = c.Width + c.Margin.Left + c.Margin.Right - 1;
        double procentualno = w / FlowLayoutPanelSize.Width * 100;

        c.Width = Convert.ToInt32(((decimal)FlowLayoutPanel.Width * (decimal)procentualno));
        int a = c.Width;
    }
}

但结果还是一样。

如果您要复制我遇到的事件的代码,这里是我拥有的FlowLayoutPanel设置:

this.FlowLayoutPanel.Location = new System.Drawing.Point(4, 34);
this.FlowLayoutPanel.Size = new System.Drawing.Size(this.Width - this.ElementProperties.Width - 12, this.Height - 38);
this.FlowLayoutPanel.BackColor = System.Drawing.Color.White;
this.FlowLayoutPanel.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
this.FlowLayoutPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.FlowLayoutPanel.HorizontalScroll.Maximum = 0;
this.FlowLayoutPanel.AutoScroll = false;
this.FlowLayoutPanel.VerticalScroll.Visible = false;
this.FlowLayoutPanel.AutoScroll = true;
this.FlowLayoutPanel.Resize += OnFlowPanelResize;
this.FlowLayoutPanel.SizeChanged += OnFlowPanelSizeChanged;

0 个答案:

没有答案