C#对象连接动态对象中的动态标签
C# object connection with dynamic labels in dynamic objects
我创建了一个动态表单,并在其中创建了动态对象。但是出于某种原因,当我创建动态事件处理程序时,事件处理程序不会获取动态对象。也就是说,它们不会出现在 Visual Studios 的选项下拉列表中。
代码:
private void btnShop_Click(object sender, EventArgs e)
{
Form frmShop = new Form();
frmShop.Show();
Button newButton = new Button();
Button Add = new Button();
Label Meat = new Label();
Label Carrots = new Label();
Label DogFood = new Label();
Label Fish = new Label();
Label Rainbows = new Label();
frmShop.Controls.Add(Meat);
frmShop.Controls.Add(Carrots);
frmShop.Controls.Add(DogFood);
frmShop.Controls.Add(Fish);
frmShop.Controls.Add(Rainbows);
frmShop.Controls.Add(newButton);
frmShop.Controls.Add(Add);
frmShop.Size = new Size(300,300);
//HERE IS MY PROBLEM v //
frmShop.Controls.Add(lblCount)
}
变量 Rainbow 是 btnShop_Click 事件处理程序的局部变量,即使您使用相同的名称,也不能在其他方法中使用此变量。 Learn about scoping here
您需要将控件移动到全局范围或给它们命名并从表单控件集合中检索它们。
例如,将接收动态表单的变量移动到全局范围(在任何 class 方法之外但在 class 声明内)
private Form frmShop = null;
private void btnShop_Click(object sender, EventArgs e)
{
frmShow = new Form();
.....
// Set the Name property for the label
Label Rainbow = new Label { Name = "Rainbow" }
...
frmShow.Controls.Add(Rainbow);
}
protected void newButton_Click(object sender, EventArgs e)
{
...
// Try to extract the label required from the form controls collection
// using the specified name
Label aRainbowLabel = frmShow.Controls
.OfType<Label>()
.FirstOrDefault(x=>x.Name=="Rainbow");
aRainbowLabel.Text = "Rainbows: 1";
}
请注意,您正在走一条危险的道路。变量 frmShow 是全局变量,如果您再次单击同一个按钮,您将创建具有相同元素的第二个表单。如果这没问题,那么没问题,但是如果您只需要这种形式的一种,则需要在创建表单之前检查变量是否为 null,并在关闭该表单时将变量 frmShow 设置回 null。
我创建了一个动态表单,并在其中创建了动态对象。但是出于某种原因,当我创建动态事件处理程序时,事件处理程序不会获取动态对象。也就是说,它们不会出现在 Visual Studios 的选项下拉列表中。
代码:
private void btnShop_Click(object sender, EventArgs e)
{
Form frmShop = new Form();
frmShop.Show();
Button newButton = new Button();
Button Add = new Button();
Label Meat = new Label();
Label Carrots = new Label();
Label DogFood = new Label();
Label Fish = new Label();
Label Rainbows = new Label();
frmShop.Controls.Add(Meat);
frmShop.Controls.Add(Carrots);
frmShop.Controls.Add(DogFood);
frmShop.Controls.Add(Fish);
frmShop.Controls.Add(Rainbows);
frmShop.Controls.Add(newButton);
frmShop.Controls.Add(Add);
frmShop.Size = new Size(300,300);
//HERE IS MY PROBLEM v //
frmShop.Controls.Add(lblCount)
}
变量 Rainbow 是 btnShop_Click 事件处理程序的局部变量,即使您使用相同的名称,也不能在其他方法中使用此变量。 Learn about scoping here
您需要将控件移动到全局范围或给它们命名并从表单控件集合中检索它们。
例如,将接收动态表单的变量移动到全局范围(在任何 class 方法之外但在 class 声明内)
private Form frmShop = null;
private void btnShop_Click(object sender, EventArgs e)
{
frmShow = new Form();
.....
// Set the Name property for the label
Label Rainbow = new Label { Name = "Rainbow" }
...
frmShow.Controls.Add(Rainbow);
}
protected void newButton_Click(object sender, EventArgs e)
{
...
// Try to extract the label required from the form controls collection
// using the specified name
Label aRainbowLabel = frmShow.Controls
.OfType<Label>()
.FirstOrDefault(x=>x.Name=="Rainbow");
aRainbowLabel.Text = "Rainbows: 1";
}
请注意,您正在走一条危险的道路。变量 frmShow 是全局变量,如果您再次单击同一个按钮,您将创建具有相同元素的第二个表单。如果这没问题,那么没问题,但是如果您只需要这种形式的一种,则需要在创建表单之前检查变量是否为 null,并在关闭该表单时将变量 frmShow 设置回 null。