我在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个单独的事件中进行此操作。 Resize
和SizeChanged
像这样:
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;