简体   繁体   中英

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 . 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. please guide me to implement this.

Short version of adding a control to a container ( Button used as example):

   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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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