2017-01-11 42 views
1

有人可以解释为什么下面的代码会在插入的元素之间留下空白区域以及如何修复它?面板AutoScrollPosition在元素之间留下空白

enter image description here

private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) 
    { 
     if (outLine.Data != null && !String.IsNullOrWhiteSpace(outLine.Data)) 
     { 
      this.lineCount++; 
      Label TestLBL = new Label(); 
      TestLBL.Text = outLine.Data.TrimStart(); 
      TestLBL.Name = this.lineCount.ToString(); 
      TestLBL.AutoSize = true; 
      TestLBL.Location = new Point(10, panel1.Controls.Count * 20); 

      BeginInvoke(new MethodInvoker(() => 
      { 
       panel1.Controls.Add(TestLBL); 
       panel1.AutoScrollPosition = new Point(10, this.lineCount * 20); 


      })); 
     } 
    } 

回答

1

既然你不使用FlowLayoutPanel的,你就必须以获得正确的位置,以补偿滚动条位置:

TestLBL.Location = new Point(10, panel1.AutoScrollPosition.Y + panel1.Controls.Count * 20); 

你或许应该把所有那个BeginInvoke块内的GUI控件创建代码。 GUI控件喜欢在GUI线程上创建。

+0

好东西。非常感谢!! –

相关问题