【发布时间】:2024-01-22 06:50:01
【问题描述】:
我找不到将按钮添加到布局的任何方法。
我正在尝试将子(按钮)添加到布局中,但找不到任何方法。
源代码:
using System;
using System.Windows.Forms;
namespace WinForms
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Button button = new Button {Height = 100, Width = 100, Text = "Test"};
}
}
}
【问题讨论】:
-
你应该在
Form1的构造函数中这样做:Button button = new Button { ... }; this.Controls.Add(button);。如果出于某种原因,您想在Main()中执行此操作(我认为没有充分的理由),只需将表单实例分配给变量并将按钮添加到其Controls集合中。Form1 frm = new Form1(); frm.Controls.Add(button); Application.Run(frm);.
标签: c# winforms user-interface desktop-application desktop