winform中的面板表现错误 [英] Panel in winform behaving wrongly

查看:109
本文介绍了winform中的面板表现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在winforms中有一个面板,在方法调用期间会在其中加载面板。在方法调用中,我编写了以下代码:



I am having a panel in winforms which loads panels in it during a method call. In the method call i have written following code:

//to get number of panel present in main panel so that new panel position can be set
 int counT = panel1.Controls.Count;

 Panel p = new Panel();
 p.Location = new Point(3, 3 + (counT * 197));
 p.Size = new Size(280, 150);

 //To add panel to parent panel
 panel1.Controls.Add(p);





每次调用方法时,它都会在主面板中加载一个面板。如果我没有滚动条,一切正常。一旦我滚动滚动条向下,然后我调用方法,面板之间的距离增加。



根据逻辑写入,两个面板之间的距离应为197沿Y轴的像素,但它增加了更多。



我设置了AutoScroll == true



任何帮助!!!



Every time I call the method it will load a panel in the main panel. Everything works fine if I didn't scroll bar. Once I scroll the Scroll bar to down and after that i call the method, the distance between panels increases.

As per logic written the distance between two panel should be 197 pixel along Y axis, but it is increasing by more.

I have set AutoScroll==true

Any help !!!

推荐答案

private void AddPanel(Panel target)
{
    int nControls = target.Controls.Count;

    Panel p = new Panel
    {
        Margin = new Padding(3),
        Anchor = AnchorStyles.Left | AnchorStyles.Top,
        Location = new Point(0, (nControls * 152)),
        Size = new Size(280, 150),
        BackColor = Color.AliceBlue,
        BorderStyle = BorderStyle.Fixed3D
    };

    target.Controls.Add(p);
}

如果你想让Panels刷新,那么设置他们的Dock属性;例如:



If you want the Panels flush, then set their Dock property; example:

private void AddDockedPanel(Panel target)
{
    int nControls = target.Controls.Count;

    Panel p = new Panel
    {
        Dock = DockStyle.Top,
        Size = new Size(280, 150),
        BackColor = Color.AliceBlue,
        BorderStyle = BorderStyle.Fixed3D
    };

    // verify you are getting added Panels
    // in the vertical order you expect
    // TextBox tb = new TextBox();
    // tb.Text = nControls.ToString();
    // p.Controls.Add(tb);

    target.Controls.Add(p);

    // fix the z-order so last Panel added is at the bottom
    p.BringToFront();
}


尝试设置 Control.Anchor Property [ ^ ]


这篇关于winform中的面板表现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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