您好我是C#的初学者,我对此代码有疑问 在最后一个btnClear_Click事件处理程序中,我需要删除我创建的动态分配的按钮数组元素。所以基本上我有一个主表单,我在那里调用对话框表单,用户可以在其中选择一个数字,当你输入一个数字时,在主表单中你会得到一些你之前输入的按钮(如果你输入的话) 4你将从0-3获得4个按钮。有人可以帮助我,因为我无法在任何地方找到答案:( 谢谢
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Button[] btnNums;
private int digits, prevNum, lastNum;
public Form1()
{
InitializeComponent();
}
private void btnSetBase_Click(object sender, EventArgs e)
{
frmDialog frmDialogInput = new frmDialog();
DialogResult dResult =
frmDialogInput.ShowDialog(this);
if (dResult == DialogResult.OK)
{
digits = frmDialogInput.Base;
lblOutDigits.Text = digits.ToString();
lastNum = int.Parse(lblOutDigits.Text);
CreateBaseButtons(prevNum, lastNum);
prevNum = lastNum;
}
}
private void CreateBaseButtons(int prev, int number)
{
for (int i = 0; i < prev; i++)
btnNums[i].Dispose();
int x = 34, y = 70;
btnNums = new Button[number];
for(int j = 0; j < number; j++)
{
btnNums[j] = new Button();
btnNums[j].Location = new System.Drawing.Point(x, y);
btnNums[j].Name = "btn" + j;
btnNums[j].Size = new System.Drawing.Size(45, 23);
btnNums[j].Text = j.ToString();
btnNums[j].UseVisualStyleBackColor = true;
btnNums[j].Click += new System.EventHandler(btnNums_Click);
this.Controls.Add(btnNums[j]);
x += btnNums[j].Size.Width;
}
}
private void btnNums_Click(object sender, EventArgs e)
{
}
private void btnClear_Click(object sender, EventArgs e)
{
lblOutDigits.Text = " ";
}
}
}
答案 0 :(得分:0)
是的,您可以在btnClear_Click事件处理程序中执行此操作。只需添加条件以检查btnNums是否为null以避免错误。
if( btnNums!=null)
{ for (int i = 0; i < btnNums.Count; i++)
btnNums[i].Dispose();
}
答案 1 :(得分:0)
我希望将代码设计更改为使用List<Button>
来维护当前的按钮列表并简化模块级变量的使用。
尝试编写如下代码:
public partial class Form1 : Form
{
private List<Button> _buttons = new List<Button>();
public Form1()
{
InitializeComponent();
}
private void btnSetBase_Click(object sender, EventArgs e)
{
frmDialog frmDialogInput = new frmDialog();
DialogResult dResult = frmDialogInput.ShowDialog(this);
if (dResult == DialogResult.OK)
{
var digits = frmDialogInput.Base;
lblOutDigits.Text = digits.ToString();
CreateBaseButtons(digits);
}
}
private void ClearBaseButtons()
{
_buttons.ForEach(b =>
{
this.Controls.Remove(b);
b.Click -= btnNums_Click;
b.Dispose();
});
_buttons.Clear();
}
private void CreateBaseButtons(int number)
{
this.ClearBaseButtons();
int x = 34, y = 70;
for (int j = 0; j < number; j++)
{
var button = new Button()
{
Location = new System.Drawing.Point(x, y),
Name = "btn" + j,
Size = new System.Drawing.Size(45, 23),
Text = j.ToString(),
UseVisualStyleBackColor = true,
};
button.Click += btnNums_Click;
this.Controls.Add(button);
_buttons.Add(button);
x += button.Size.Width;
}
}
private void btnNums_Click(object sender, EventArgs e)
{
}
private void btnClear_Click(object sender, EventArgs e)
{
lblOutDigits.Text = " ";
this.ClearBaseButtons();
}
}
这应该可以解决您的问题,并通过删除事件处理程序并从表单中明确删除按钮来添加更好的清理。
答案 2 :(得分:0)
只需将此代码写入您的方法:
private void btnClear_Click(object sender, EventArgs e)
{
lblOutDigits.Text = " ";
this.btnNums = new Button[btnNums]; // or this.btnNums = new Button[1];
}