C# 在c语言中如何在运行时在框架或面板上添加按钮控件#

C# 在c语言中如何在运行时在框架或面板上添加按钮控件#,c#,winforms,C#,Winforms,windows窗体应用程序 我想要设计类似pda设备的键盘。我想要所有数字键在一组中,然后我想要将所有这些按钮安排在一个框架或面板上。包含所有特殊功能键的第二个框架或面板。我想在阅读了键盘的xml文件描述后,在运行时设计这个键盘。 请指导我实现此操作。将控件添加到容器的简短版本(按钮用作示例): 棘手的部分可能不是创建控件并将它们添加到容器中,而是对它们进行排列,使其看起来更美观。另一个示例Add5按钮 int xlocation = 5; for (int i = 1; i &

windows窗体应用程序 我想要设计类似pda设备的键盘。我想要所有数字键在一组中,然后我想要将所有这些按钮安排在一个框架或面板上。包含所有特殊功能键的第二个框架或面板。我想在阅读了键盘的xml文件描述后,在运行时设计这个键盘。
请指导我实现此操作。

将控件添加到容器的简短版本(
按钮用作示例):


棘手的部分可能不是创建控件并将它们添加到容器中,而是对它们进行排列,使其看起来更美观。

另一个示例Add5按钮

    int xlocation = 5;
    for (int i = 1; i <= 5; i++)
    {
         Button newButton = new Button();
         {
             newButton.Name = string.Format("Button{0}", i);
             newButton.Text = string.Format("Button {0}",i);
             newButton.Location = new System.Drawing.Point(xlocation, 10);
             newButton.Size = new System.Drawing.Size(75, 35);
             newButton.Click += btn_msg;
             this.Controls.Add(newButton);
         }
         xlocation = xlocation + 85;
    }

你说的是webforms(asp.net)、asp.NETMVC还是winforms(桌面应用程序)?向表单中动态添加控件确实是一场布局噩梦;幸运的是,有TableLayoutPanel控件使事情(稍微)简单一些。
    int xlocation = 5;
    for (int i = 1; i <= 5; i++)
    {
         Button newButton = new Button();
         {
             newButton.Name = string.Format("Button{0}", i);
             newButton.Text = string.Format("Button {0}",i);
             newButton.Location = new System.Drawing.Point(xlocation, 10);
             newButton.Size = new System.Drawing.Size(75, 35);
             newButton.Click += btn_msg;
             this.Controls.Add(newButton);
         }
         xlocation = xlocation + 85;
    }
    public void btn_msg(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        conn.msgErr(btn.Name.ToString());
    }