Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改面板中按钮的位置?_C#_Windows_Winforms_Button_Panel - Fatal编程技术网

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()
事件是如何在这里起作用的。千万不要在绘画事件中做任何会强制绘画的事情!结果是一个无限循环!