C# 如何更改面板中按钮的位置?
我试图根据需要在面板中执行一些操作,并将现有添加按钮的位置更改为新位置C# 如何更改面板中按钮的位置?,c#,windows,winforms,button,panel,C#,Windows,Winforms,Button,Panel,我试图根据需要在面板中执行一些操作,并将现有添加按钮的位置更改为新位置 public class MyPanel : Panel { protected override OnPaint() { // Added the controls. // Done some operations which needs the location change. // Changed the location of the butto
public class MyPanel : Panel
{
protected override OnPaint()
{
// Added the controls.
// Done some operations which needs the location change.
// Changed the location of the buttons.
}
}
但控件在面板中不可见
仅当位置未更改时,控制才会正确显示在面板中
是否有人请告诉我,为什么在动态更改位置时面板中不显示控件?将此添加到您的项目中
Control board = panel1;
Button But = new Button()
{
Size = new Size(size, size),
Location = new Point(x, y),
....
};
board.Controls.Add(But);
能否在
OnPaint
方法中添加代码?为什么尝试在OnPaint
方法中添加控件?显示执行这些操作的代码而不是注释。并且,如前所述,解释一下OnPaint()
事件是如何在这里起作用的。千万不要在绘画事件中做任何会强制绘画的事情!结果是一个无限循环!