0

我正在为 WP8 开发一个应用程序,在我的代码中,我想为数组中的按钮分配索引。原因是我想用按钮来操作按钮(即按下一个按钮激活其他按钮)


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

}

4

2 回答 2

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]);
        }
    }
}
于 2013-07-06T11:08:16.500 回答
0
--- loop ---
Button abc = new Button();
abc.Name = loopCounter.ToString();
--- loop ---

这将帮助您分配索引,不要使用 Button 数组,它没用!

于 2013-07-06T11:06:50.900 回答