【问题标题】:Panel AutoScrollPosition leaves white spaces between elements面板 AutoScrollPosition 在元素之间留下空白
【发布时间】:2017-01-11 15:31:47
【问题描述】:

有人能解释一下为什么下面的代码在插入的元素之间会留下空白以及如何解决吗?

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);


            }));
        }
    }

【问题讨论】:

    标签: c# winforms autoscroll


    【解决方案1】:

    由于您没有使用 FlowLayoutPanel,因此您必须补偿滚动条位置才能获得正确的位置:

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

    您可能应该将所有 GUI 控件创建代码放在 BeginInvoke 块中。 GUI 控件喜欢在 GUI 线程上创建。

    【讨论】:

    • 好东西。非常感谢!!
    猜你喜欢
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2013-09-30
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多