參考資料: Form.AcceptButton 屬性 能做什麼?

想像一下如果使用者在 textBox 填好資料後,不用滑鼠,按 Enter 就能執行下一步的動作,是不是蠻方便的。

namespace AcceptBtnTest
{
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
             this.AcceptButton = button1;
         }

        private void button1_Click(object sender, EventArgs e)
         {
            
             Form frm = new Form();
             for (int i = 0; i < Convert.ToInt16(textBox2.Text); i++)
             {
                 for (int j = 0; j < Convert.ToInt16(textBox1.Text); j++)
                 {
                     Button btn = new Button();
                     frm.AcceptButton = btn;
                     frm.Controls.Add(btn);
                     btn.Left = 50 * i;
                     btn.Top  = 20 * j;
                     btn.Width = 50;
                     btn.Height = 20;
                     btn.Text = j.ToString() + ", " + i.ToString();
                     btn.Click += new EventHandler(myClick);
                 }
             }
             frm.Show();
         }
         private void myClick(object sender, EventArgs e)
         {
             MessageBox.Show(((Button)sender).Text);
         }
         private void Form1_Load(object sender, EventArgs e)
         {

        }
     }
}

 

輸入完3,4按下Enter即可以

image

產生3x4按鈕

image

按下1,1按鈕

image

sample code

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()