Winform 中的面板行为错误
Panel in winform behaving wrongly
我在 Winforms 中有一个面板,它在方法调用期间加载其中的面板。
在方法调用中,我编写了以下代码:
//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);
每次我调用该方法时,它都会在主面板中加载一个面板。如果我不滚动滚动条,一切正常。一旦我向下滚动滚动条并调用该方法,面板之间的距离就会增加。
根据所写的逻辑,两个面板之间的距离沿 Y 轴应为 197 像素,但它正在增加更多。
我设置了AutoScroll=true
任何帮助!!!
这是我直到现在才知道的非常奇怪的行为(而且我在 WF 方面有很多经验)。可以看到执行上面的代码时父面板是什么时候滚动的。我在想子控件位置是相对于 ClientRectangle
,但事实证明它们正在占 DisplayRectangle
。
很快,而不是这个
p.Location = new Point(3, 3 + (counT * 197));
使用这个
var parentRect = panel1.DisplayRectangle;
p.Location = new Point(parentRect.X + 3, parentRect.Y + 3 + (counT * 197));
Panel.AutoScrollPosition
影响所有子控件的 Location
属性。滚动以这种方式工作。所以你应该记住,例如你可以存储当前滚动位置,将位置移动到 (0,0),添加新控件,然后恢复滚动位置
//to get number of panel present in main panel so that new panel position can be set
int counT = panel1.Controls.Count;
var pos = this.panel1.AutoScrollPosition; // Whe are storing scroll position
this.panel1.AutoScrollPosition = new Point(Size.Empty);
Panel p = new Panel();
p.Location = new Point(3, 3 + (counT * 197));
p.Size = new Size(280, 150);
p.BorderStyle = BorderStyle.FixedSingle;
//To add panel to parent panel
panel1.Controls.Add(p);
this.panel1.AutoScrollPosition = new Point(Math.Abs(pos.X), Math.Abs(pos.Y)); // We are restoring scroll position
我在 Winforms 中有一个面板,它在方法调用期间加载其中的面板。 在方法调用中,我编写了以下代码:
//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);
每次我调用该方法时,它都会在主面板中加载一个面板。如果我不滚动滚动条,一切正常。一旦我向下滚动滚动条并调用该方法,面板之间的距离就会增加。
根据所写的逻辑,两个面板之间的距离沿 Y 轴应为 197 像素,但它正在增加更多。
我设置了AutoScroll=true
任何帮助!!!
这是我直到现在才知道的非常奇怪的行为(而且我在 WF 方面有很多经验)。可以看到执行上面的代码时父面板是什么时候滚动的。我在想子控件位置是相对于 ClientRectangle
,但事实证明它们正在占 DisplayRectangle
。
很快,而不是这个
p.Location = new Point(3, 3 + (counT * 197));
使用这个
var parentRect = panel1.DisplayRectangle;
p.Location = new Point(parentRect.X + 3, parentRect.Y + 3 + (counT * 197));
Panel.AutoScrollPosition
影响所有子控件的 Location
属性。滚动以这种方式工作。所以你应该记住,例如你可以存储当前滚动位置,将位置移动到 (0,0),添加新控件,然后恢复滚动位置
//to get number of panel present in main panel so that new panel position can be set
int counT = panel1.Controls.Count;
var pos = this.panel1.AutoScrollPosition; // Whe are storing scroll position
this.panel1.AutoScrollPosition = new Point(Size.Empty);
Panel p = new Panel();
p.Location = new Point(3, 3 + (counT * 197));
p.Size = new Size(280, 150);
p.BorderStyle = BorderStyle.FixedSingle;
//To add panel to parent panel
panel1.Controls.Add(p);
this.panel1.AutoScrollPosition = new Point(Math.Abs(pos.X), Math.Abs(pos.Y)); // We are restoring scroll position