具有自动滚动的面板重绘 [英] Panel with auto scroll redraw

查看:100
本文介绍了具有自动滚动的面板重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,可以在其中以编程方式添加一个面板;

I have form that programmatically add a panel in it;

对于程序中的每个任务,我添加一个标签并 面板上的进度条,其中Y位置增加了30像素.

For each task coming in to my program, I add a label and progress bar to panel which have 30 pixel Y added to previous Y position.

但是当面板向下滚动时,有时想要向下滚动时, 位置成倍增加,但不在其确切位置.

But when panel get scrolls sometimes when want to scroll down, positions multiplied and not in their exact position.

记住,我检查了Y位置并将其写入控制台,发现Y位置可以,但是面板无法正确显示

Remember, I checked and written Y positions to console and saw that Y position is ok, but panel does not show it correctly

我认为问题在于面板绘制方法, 但不知道如何解决.

I think problem is for panel draw method, but don't know how to fix it.

问题是任务26应该排在25之后,但位置不正确,尽管控制台显示位置正确.

Problem is that task 26 should come just after 25 but not in correct position, despite of console I've seen position is correct.

添加控件的代码:

 private static void AddTaskControls(int taskId)
        {
            Label lbl = new Label();
            lbl = new Label();
            lbl.Name = "lbl" + taskId.ToString();
            lbl.Text = "Task " + taskId.ToString() + ":";
            lbl.Width = 57;
            lbl.Height = 13;
            lbl.Location = new Point(0, 25 + (taskId) * 30);

            ProgressBar pr = new ProgressBar();
            pr = new ProgressBar();
            pr.Name = "pr" + taskId.ToString();
            pr.Width = 180;
            pr.Height = 23;
            pr.Location = new Point(50, 20 + (taskId) * 30);

            Label lbl2 = new Label();
            lbl2.Name = "lbl" + taskId.ToString() + "List";
            lbl2.Text = "Starting " + taskId.ToString() + "";
            lbl2.Width = 200;
            lbl2.Height = 13;
            lbl2.Location = new Point(230, 25 + (taskId) * 30);
            //Console.WriteLine(lbl2.Location.Y + "taskid:"+taskId);


            if (panel1.InvokeRequired)
            {
                AddTaskControllsCallback t = new AddTaskControllsCallback(AddTaskControls);
                panel1.BeginInvoke(t, new object[] { taskId });
            }
            else
            {
                panel1.Controls.Add(lbl);
                panel1.Controls.Add(pr);
                panel1.Controls.Add(lbl2);               

                pr.BringToFront();                
            }
        }

推荐答案

这与线程无关.添加控件时,当前滚动位置用于重新计算 有效 Location.奇怪的?是的.

This has nothing to do with threading. When adding controls the current scroll position is used to re-calculate the effective Location. Weird? Yes..

所以原因可能是您在添加新控件时Panel 向下滚动.我看不到发生了什么,但是这里有一个很小的测试来证明问题所在:

So the reason probably is that your Panel scrolls down while you are adding new controls. I don't see where it happens but here is a tiny test to demonstrate the problem:

很熟悉吧?

我强制执行代码滚动,但是在您的情况下,原因应该类似,而修复方法也应如此.

I enforce the scrolling in the code but in your case the reason should be similar, as should be the fix..

private void button20_Click(object sender, EventArgs e)
{
    // lets add a few buttons..
    for (int i = 0; i < 20; i++)
    {
        Button btn = new Button();
        btn.Top = i * 25;
        btn.Parent = panel2;
    }
    // ..now let's enforce a scoll amount of 111 pixels:
    panel2.AutoScrollPosition =  new Point(panel2.AutoScrollPosition.X, 
                                            -panel2.AutoScrollPosition.Y + 111);

    // ..and add a few more buttons..
    for (int i = 20; i < 40; i++)
    {
        Button btn = new Button();
        btn.Top = i * 25;    // not good enough!
        // btn.Top = i * 25 + panel2.AutoScrollPosition.Y; // this will fix the problem!
        btn.Parent = panel2;
    }
}

因此,您的代码需要像这样设置Locations:

So your code needs to set the Locations like this:

lbl.Location = new Point(0, 25 + (taskId) * 30 + panel1.AutoScrollPosition.Y);
..
pr.Location = new Point(50, 20 + (taskId) * 30 + panel1.AutoScrollPosition.Y));
..
lbl2.Location = new Point(230, 25 + (taskId) * 30 + panel1.AutoScrollPosition.Y));

(或者您了解滚动如何发生并防止滚动.)

(Or you find out how the scrolling happens and prevent it.)

这篇关于具有自动滚动的面板重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆