简体   繁体   English

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

[英]How add button control on frame or panel at runtime in c#

windows form application I want desine keypad like pda device .I want all numeric keys in one group therfore I want to arrange all these button on one frame or panel . Windows窗体应用程序我想要像pda设备这样的desine键盘。我希望将所有数字键归为一组,然后再将所有这些按钮排列在一个框架或面板上。 second frame or panel which contain all the special function keys . 第二个框架或面板,包含所有特殊功能键。 I want design this keyboard at runtime after reading xml file description of keypad. 阅读键盘的xml文件描述后,我想在运行时设计此键盘。 please guide me to implement this. 请指导我实施此操作。

Short version of adding a control to a container ( Button used as example): 向容器添加控件的简短版本(以Button为例):

   Button myButton = new Button();
   myButton.Text = "some text";
   // attach event handler for Click event 
   // (assuming ButtonClickHandler is an existing method in the class)
   myButton.Click += ButtonClickHandler;
   myPanel.Controls.Add(myButton);
   // additional code for setting location and such for myButton

The tricky part will probably not be to create the controls and add them to the container, but to arrange them so that it looks good. 棘手的部分可能不是创建控件并将它们添加到容器中,而是对其进行排列以使其看起来不错。

another example add 5 button 另一个示例添加5按钮

    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;
    }

button click Event 按钮单击事件

    public void btn_msg(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        conn.msgErr(btn.Name.ToString());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM