C# 为按钮C分配索引#
我正在为WP8开发一个应用程序,在我的代码中,我希望像在数组中一样为按钮分配索引。原因是我想用按钮操作按钮(即按下一个按钮激活其他按钮)C# 为按钮C分配索引#,c#,C#,我正在为WP8开发一个应用程序,在我的代码中,我希望像在数组中一样为按钮分配索引。原因是我想用按钮操作按钮(即按下一个按钮激活其他按钮) 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows.Forms; 名称空间test2 { 公共部分类Form1:Form { 公共表格1() { 初始化组件(
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间test2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
公共类LED按钮:按钮
{
公共const int LEDWidth=50;
公共建筑内部高度=30;
公共按钮()
{
BackColor=Color.Tan;//内部颜色
//BackColor=Color.FromArgb(0,64,0);
ForeColor=Color.Yellow;//轮廓
FlatStyle=FlatStyle.Popup;//按钮样式
尺寸=新尺寸(LED宽度、LED高度);
UseVisualStyleBackColor=false;
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
LED按钮[,]b=新的LED按钮[4,4];
for(int y=0;y
这将帮助您分配索引,
不要使用按钮阵列,它是无用的 如果我正确理解了你的问题,你应该依靠一系列代表。这里您对代码进行了更正,将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]);
}
}
}
namespace test2
{
公共部分类Form1:Form
{
公共代理无效按钮\u单击(对象发送者,事件参数e);
公共静态按钮点击[]点击方法=新建按钮点击[4];
公共表格1()
{
初始化组件();
}
公共类LED按钮:按钮
{
公共const int LEDWidth=50;
公共建筑内部高度=30;
公共按钮()
{
BackColor=Color.Tan;//内部颜色
//BackColor=Color.FromArgb(0,64,0);
ForeColor=Color.Yellow;//轮廓
FlatStyle=FlatStyle.Popup;//按钮样式
尺寸=新尺寸(LED宽度、LED高度);
UseVisualStyleBackColor=false;
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
clickMethods[0]=按钮电子单击1;
clickMethods[1]=按钮neric\u Click\u 2;
clickMethods[2]=按钮电子单击3;
clickMethods[3]=按钮neric\u Click\u 4;
}
私有无效按钮neric\u Click\u 1(对象发送者,事件参数e)
{
}
私有无效按钮neric\u Click\u 2(对象发送者,事件参数e)
{
}
私有无效按钮neric\u Click\u 3(对象发送者,事件参数e)
{
}
私有无效按钮neric\u Click\u 4(对象发送者,事件参数e)
{
}
私有无效按钮1\u单击\u 1(对象发送者,事件参数e)
{
LED按钮[,]b=新的LED按钮[4,4];
for(int y=0;y 如果(y)你没有解释你遇到的问题…实际上我想说的是我需要直接访问按钮,我们可以实现吗?我很困惑。是WP8
还是WinForms
?
--- loop ---
Button abc = new Button();
abc.Name = loopCounter.ToString();
--- loop ---
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]);
}
}
}