2013-07-06 57 views
0

我正在爲WP8應用程序工作,在我的代碼中,我想將索引分配給像數組中的按鈕。原因是我要操作的按鈕按鈕(即,在按下一個按鈕來激活其他按鈕)將索引分配給按鈕C#


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace test2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     public class LEDButton : Button 
     { 
      public const int LEDWidth = 50; 
      public const int LEDHeight = 30; 
      public LEDButton() 
      { 
       BackColor = Color.Tan;//inner color 
       //BackColor = Color.FromArgb(0, 64, 0); 
       ForeColor = Color.Yellow;//outline 
       FlatStyle = FlatStyle.Popup;//Button style 
       Size = new Size(LEDWidth, LEDHeight); 
       UseVisualStyleBackColor = false; 
      } 
     } 
     private void button1_Click(object sender, EventArgs e) 
     { 
      LEDButton[,] b = new LEDButton[4, 4]; 
      for (int y = 0; y < b.GetUpperBound(0); y++) 
      { 
       for (int x = 0; x < b.GetUpperBound(1); x++) 
       { 
        b[y, x] = new LEDButton() 
        { 
         //put button properties here 
         Name = "button" + y.ToString() + x.ToString(),//String.Format("Button{0}{1}", y, x), 
         TabIndex = 10 * y + x, 
         Text = y.ToString() + x.ToString(), 
         Location = new Point(LEDButton.LEDWidth * x + 20, LEDButton.LEDHeight * y + 20) 
        }; 
        // b[y, x].Click += button_Click; 
       } 
      } 
      // add buttons to controls 
      for (int y = 0; y < b.GetUpperBound(0); y++) 
       for (int x = 0; x < b.GetUpperBound(1); x++) 
        this.Controls.Add(b[y, x]); 
     } 
    } 

} 

+3

你沒有解釋你所遇到的問題... – Charleh

+0

其實我想說,我需要直接訪問按鈕,我們可以實現它嗎? –

+1

我很困惑。是「WP8」還是「WinForms」? – Liel

回答

0
--- loop --- 
Button abc = new Button(); 
abc.Name = loopCounter.ToString(); 
--- loop --- 

這將幫助你分配指標, 不要使用Button的數組,它是無用的!

1

如果我已經正確理解你的問題,你應該依賴一組代表。在這裏,你有你的代碼中動態地分配給4個不同的方法,以4個不同的按鈕的修正:

namespace test2 
{ 
    public partial class Form1 : Form 
    { 
     public delegate void button_click(object sender, EventArgs e); 
     public static button_click[] clickMethods = new button_click[4]; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     public class LEDButton : Button 
     { 
      public const int LEDWidth = 50; 
      public const int LEDHeight = 30; 
      public LEDButton() 
      { 
       BackColor = Color.Tan;//inner color 
       //BackColor = Color.FromArgb(0, 64, 0); 
       ForeColor = Color.Yellow;//outline 
       FlatStyle = FlatStyle.Popup;//Button style 
       Size = new Size(LEDWidth, LEDHeight); 
       UseVisualStyleBackColor = false; 
      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      clickMethods[0] = buttonGeneric_Click_1; 
      clickMethods[1] = buttonGeneric_Click_2; 
      clickMethods[2] = buttonGeneric_Click_3; 
      clickMethods[3] = buttonGeneric_Click_4; 
     } 
     private void buttonGeneric_Click_1(object sender, EventArgs e) 
     { 

     } 
     private void buttonGeneric_Click_2(object sender, EventArgs e) 
     { 

     } 
     private void buttonGeneric_Click_3(object sender, EventArgs e) 
     { 

     } 
     private void buttonGeneric_Click_4(object sender, EventArgs e) 
     { 

     } 
     private void button1_Click_1(object sender, EventArgs e) 
     { 
      LEDButton[,] b = new LEDButton[4, 4]; 
      for (int y = 0; y < b.GetUpperBound(0); y++) 
      { 
       for (int x = 0; x < b.GetUpperBound(1); x++) 
       { 
        b[y, x] = new LEDButton() 
        { 
         //put button properties here 
         Name = "button" + y.ToString() + x.ToString(),//String.Format("Button{0}{1}", y, x), 
         TabIndex = 10 * y + x, 
         Text = y.ToString() + x.ToString(), 
         Location = new Point(LEDButton.LEDWidth * x + 20, LEDButton.LEDHeight * y + 20) 
        }; 

        if (y <= 3) 
        { 
         b[y, x].Click += new System.EventHandler(clickMethods[y]); 
        } 
       } 
      } 
      // add buttons to controls 
      for (int y = 0; y < b.GetUpperBound(0); y++) 
       for (int x = 0; x < b.GetUpperBound(1); x++) 
        this.Controls.Add(b[y, x]); 
     } 
    } 
} 
相關問題