动态按钮创建&使用 c# 将它们按预定义的顺序放置 [英] Dynamic button creation & placing them in a predefined order using c#

查看:19
本文介绍了动态按钮创建&使用 c# 将它们按预定义的顺序放置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NET 4.5 C# 创建一个 windows 窗体.我想动态创建 &添加按钮和还为它们分配点击事件,但希望它们像图像一样以特定方式动态放置.

我的问题是如何以上述方式动态放置按钮,即 4x4 格式(一行 4 个按钮,4 列,但行数不限).是否有可能以胜利形式这样做?

目前我正在尝试下面提到的代码,但不清楚如何放置如上所示的按钮.

 public Form1(){初始化组件();for (int i = 0; i <5; i++){按钮按钮 = new Button();button.Location = new Point(160, 30 * i + 10);button.Click += new EventHandler(ButtonClickCommonEvent);button.Tag = i;this.Controls.Add(button);}}void ButtonClickCommonEvent(对象发送者,EventArgs e){按钮按钮 = 发送者为按钮;如果(按钮!= null){switch ((int)button.Tag){案例0://第一个按钮被点击休息;情况1://第二个按钮被点击休息;//...}}}

请用代码建议解决方案.

解决方案

您可以使用

注意:

NET 4.5 C# to create a windows form. I want to dynamically create & add buttons & also assign them click events but want them to be dynamically placed in a particular fashion just like the image.

My question is how do I place the buttons dynamically in the above fashion i.e. 4x4 format (4 buttons in a row, 4 columns but unlimited rows). Is it possible to do so in win forms?

Presently I'm trying the below mentioned code but have no clear idea as to how I can place the buttons as shown above.

        public Form1()
    {
        InitializeComponent();

        for (int i = 0; i < 5; i++)
        {
            Button button = new Button();
            button.Location = new Point(160, 30 * i + 10);
            button.Click += new EventHandler(ButtonClickCommonEvent);
            button.Tag = i;
            this.Controls.Add(button);
        }
    }

void ButtonClickCommonEvent(object sender, EventArgs e)
  {
     Button button = sender as Button;
     if (button != null)
     {       
        switch ((int)button.Tag)
        {
           case 0:
              // First Button Clicked
              break;
           case 1:
              // Second Button Clicked
              break;
           // ...
        }
     }
  }

Please advise solution with codes.

解决方案

You can use a TableLayoutPanel and create your buttons dynamically and add them to the panel.

For example:

private void Form1_Load(object sender, EventArgs e)
{
    var rowCount = 3;
    var columnCount = 4;

    this.tableLayoutPanel1.ColumnCount = columnCount;
    this.tableLayoutPanel1.RowCount = rowCount;

    this.tableLayoutPanel1.ColumnStyles.Clear();
    this.tableLayoutPanel1.RowStyles.Clear();

    for (int i = 0; i < columnCount; i++)
    {
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100 / columnCount));
    }
    for (int i = 0; i < rowCount; i++)
    {
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100 / rowCount));
    }

    for (int i = 0; i < rowCount* columnCount; i++)
    {
        var b = new Button();
        b.Text = (i+1).ToString();
        b.Name = string.Format("b_{0}", i + 1);
        b.Click += b_Click;
        b.Dock = DockStyle.Fill;
        this.tableLayoutPanel1.Controls.Add(b);
    }
}

void b_Click(object sender, EventArgs e)
{
    var b = sender as Button;
    if (b != null)
        MessageBox.Show(string.Format("{0} Clicked", b.Text));
}

Note:

这篇关于动态按钮创建&amp;使用 c# 将它们按预定义的顺序放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆