【问题标题】:C# Windows Forms: How the Control is added to the Form under InitializeComponent() without Controls.AddC# Windows 窗体:如何在 InitializeComponent() 下将控件添加到窗体而不使用 Controls.Add
【发布时间】:2011-12-18 20:25:01
【问题描述】:

C# Windows 窗体:我正在使用 Visual Studio 创建一个测试应用程序,我在窗体上拖动了一个按钮并创建了一个 button_click 事件,当我调试我的应用程序时,我发现 “按钮”显示在表单上,​​即使我没有使用“this.Controls.Add(button)”将我的按钮添加到表单中。 我还检查了“filename.Designer.cs”下的 InitializeComponent() 方法,还缺少“this.Controls.Add(button)”行。

我的疑问是:在调试时如何将Control(button)自动添加到表单中,而不需要“this.Controls.Add(button)”指令。

谢谢。

【问题讨论】:

  • 你确定你看的够仔细吗?我很确定在InitializeComponents中调用Controls.Add,否则它不会工作......
  • 它是否被添加到表单内的另一个容器中? (例如面板)
  • 这是有线的,我在使用 Visual Studio 2010 和 2008 时也遇到过一种情况,并且将时钟向后更改为 Runn 的应用程序但没有更新,即使我编写了代码它也不会更新它。
  • 对不起,我这边出了点问题,忽略这个帖子。请求版主删除此帖。谢谢

标签: c# winforms visual-studio controls add


【解决方案1】:

它确实添加了它,它在“Windows 窗体设计器生成的代码”区域中。

#region Windows Form Designer generated code
// Removed to slim...
private void InitializeComponent()
{
    // Removed to slim...
    // 
    // Form1
    // 
    // Removed to slim...
    this.Controls.Add(this.button1);
    // Removed to slim...
}

#endregion

【讨论】:

  • 缺少的控件,我想由于某种原因,代码被删除,将它们放入通过属性 Form.Controls 访问的表单的 ControlCollection。
  • @VkTt 如果删除了将控件添加到窗体的行,则控件将不会出现在窗体上。
  • 对不起,我这边出了点问题,忽略这个帖子。
猜你喜欢
  • 2013-12-20
  • 1970-01-01
  • 2016-08-15
  • 2010-11-05
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
相关资源
最近更新 更多