【问题标题】:How to change the location of a Button in a Panel?如何更改面板中按钮的位置?
【发布时间】:2019-01-19 19:02:04
【问题描述】:

我正在尝试根据我的需要在面板中执行一些操作,并将现有添加按钮的位置更改为新位置。

public class MyPanel : Panel
{   
   protected override OnPaint()
   {  
       // Added the controls.


       // Done some operations which needs the location change.


       // Changed the location of the buttons.

   }

 }

但控件在面板中不可见。

仅当位置未更改时,控件才会正确显示在面板中。

有人请告诉我,为什么动态更改位置时控件不显示在面板中?

【问题讨论】:

  • 可以在OnPaint方法中添加代码吗?
  • 为什么要在OnPaint方法中添加控件?
  • 显示执行这些操作的代码,而不是 cmets。并且,如前所述,解释OnPaint() 事件如何在这里发挥作用。
  • 永远不要在 Paint 事件中做任何会强制绘画的事情!结果是无限循环!

标签: c# windows winforms button panel


【解决方案1】:

将此添加到您的项目中

Control board = panel1;

Button But = new Button()
{
     Size = new Size(size, size),
     Location = new Point(x, y),
     ....
};

board.Controls.Add(But);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    相关资源
    最近更新 更多