C# 如何使用标记和类知道按钮的数组位置?
我使用WinForm在单击时制作一个扩展板,基本上我需要的是获取数组行和列,这样我就可以发送到一个名为place的类,该类将接受按下的按钮,并使其右、左、下和上按钮可见,我如何做到这一点?。将附上一个结果示例。 请-不要建议标签和课程以外的其他想法,因为我在一位老师的指导下工作,要求非常严格,请注意,让事情尽可能简单,我是一名初学者,从事编码工作不到一年。 谢谢你! 点击功能:C# 如何使用标记和类知道按钮的数组位置?,c#,C#,我使用WinForm在单击时制作一个扩展板,基本上我需要的是获取数组行和列,这样我就可以发送到一个名为place的类,该类将接受按下的按钮,并使其右、左、下和上按钮可见,我如何做到这一点?。将附上一个结果示例。 请-不要建议标签和课程以外的其他想法,因为我在一位老师的指导下工作,要求非常严格,请注意,让事情尽可能简单,我是一名初学者,从事编码工作不到一年。 谢谢你! 点击功能: private void qqq_Click(object sender, EventArgs e) //W
private void qqq_Click(object sender, EventArgs e) //When Click
{
Place pl=(Place)( ((Button)sender).Tag());
地点类别:
class Place
{
private int row;
private int col;
public Place(int row, int col)
{
this.row = row;
this.col = col;
}
private int GetR()
{
this.row = row;
}
private int GetC();
{
this.col = col;
}
private int SetR()
{
return row;
}
你可以这样做:
{
...
btn.Click += (sender, ev) => {
var b = Button as sender;
if (b != null)
{
var tag = b.Tag; // assuming tag is keeping row/col in form R_C
var dims = tag.Split('_');
var row = int.Parse(dims[0]);
var col = int.Parse(dims[1]);
// get the buttons around, if any and make visible
SetButtonVisible($"{row - 1}_{col})";
SetButtonVisible($"{row}_{col - 1}");
SetButtonVisible($"{row}_{col + 1}");
SetButtonVisible($"{row + 1}_{col + 1}");
}
};
}
private void SetButtonVisible(string tag)
{
var button = form.Controls
.OfType<Button>()
.SingleOrDefault( c => c.Tag == tag );
if ( button != null)
{
button.Visible = true;
}
}
你的问题是什么?请发一些代码。向我们展示您在创建按钮时可以指定的内容,它是对象类型的,因此您可以指定除表单设计器中的字符串以外的任何内容。你们有多远?你们的问题是什么?你们有问题如何移动按钮吗?标签是一个属性,而不是方法。在它之后移除。对我来说有点复杂。。我不知道var是什么。var应该出现在C教程的第一页。在尝试Coe框架之前,首先应该学习C的基本知识。