gpt4 book ai didi

c# - 完成特定任务后清除每件事

转载 作者:太空狗 更新时间:2023-10-30 00:31:52 24 4
gpt4 key购买 nike

我正在开发一个软件,我想在完成特定任务后清除每件事,比如清除所有字段、 ListView 等,我必须写下很多要清除的东西,就像我的新按钮的代码是:

 private void btnNew_Click(object sender, EventArgs e)
{
txtProductCode1.ReadOnly = false;
txtInvoiceNo.ReadOnly = false;
cmbCustoemerType.Enabled = false;
cmbCustomerName.Enabled = false;
button1.Enabled = true;
txtProductCode1.Text = null;
txtWageName.Text = null;
txtWageCost.Text = null;
btnFirst.Visible = false;
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
cmbProductName.Enabled = true;
txtQty.ReadOnly = false;
txtPercant.ReadOnly = false;
txtDiscount.ReadOnly = false;
cmbCustoemerType.Enabled = true;
cmbCustomerName.Enabled = true;
button5.Enabled = true;
btnDelete.Enabled = true;
dtp1.Enabled = true;
btnSave.Text = "&Save";

cmbCustoemerType.Text = null;
cmbCustomerName.Text = null;
txtInvoiceNo.Clear();
txtDiscount.Clear();
txtGrandTotal.Clear();
txtProductName.Clear();
txtSalePrice.Clear();
txtQty.Clear();
txtTotal.Clear();

txtExtraWages.Text = null;
lvExtraWages.Items.Clear();
lvTransaction.Items.Clear();
lblCount.Text = null;
lblNetTotal.Text = null;
txtPercant.Text = null;

txtInvoiceNo.Text = invoice.ToString();
}

有什么方法可以避免再次编写代码,就像我需要启用我在另一个按钮上禁用的东西,所以我必须编写它的反向代码来完成任务,这真的恼人的!还有其他方法吗?

编辑:

尝试了以下代码:

private void btnNew_Click(object sender, EventArgs e)
{
//using setboxdefault function
SetTextBoxDefaults();

cmbCustoemerType.Text = null;
cmbCustomerName.Text = null;
cmbCustoemerType.Enabled = false;
cmbCustomerName.Enabled = false;
cmbProductName.Enabled = true;
cmbCustoemerType.Enabled = true;
cmbCustomerName.Enabled = true;

button5.Enabled = true;
btnDelete.Enabled = true;
button1.Enabled = true;
btnFirst.Visible = false;
btnPrevious.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;


btnSave.Text = "&Save";

lvExtraWages.Items.Clear();
lvTransaction.Items.Clear();

lblCount.Text = null;
lblNetTotal.Text = null;

dtp1.Enabled = true;

txtInvoiceNo.Text = invoice.ToString();
}

private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
TextBox.Clear();
}
}

public IEnumerable<Control> GetControls(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControls(ctrl, type)).Concat(controls).Where(c => c.GetType() == type);
}

但它在 TextBox.Clear(); 上出错,错误是错误 4

An object reference is required for the non-static field, method, or property 'System.Windows.Forms.TextBoxBase.Clear()'    

请让我知道我错在哪里..!!

最佳答案

正如 Tim 所建议的,您可以遍历控件并相应地设置每个控件..

public IEnumerable<Control> GetControls(Control control,Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControls(ctrl,type)).Concat(controls).Where(c => c.GetType() == type);
}

private void btnNew_Click(object sender, EventArgs e)
{
SetComboBoxDefaults();
SetTextBoxDefaults();
SetButtonDefaults();
}

private void SetComboBoxDefaults()
{
var comboBoxes = GetControls(this, typeof(ComboBox));
foreach (var comboBox in comboBoxes)
{
((ComboBox)comboBox).Enabled = false;
((ComboBox)comboBox).Text = null;
}
}

private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
((TextBox)textBox).Clear();
}
}

private void SetButtonDefaults()
{
var buttons = GetControls(this, typeof(Button));
foreach (var button in buttons)
{
((Button)button).Visible = false;

if (button.Name == "btnSave") ((Button)button).Text = "&Save";
}
}

您还可以使用单独的 GetControl 方法,以便它返回特定类型的 Control,从而减少转换为派生类型的需要。

编辑更新:

您正在使用 TextBox 类型而不是 foreach 循环中的变量名。更新它以使用 textBox(小写),而不是 TextBox(pascal),见下文:

private void SetTextBoxDefaults()
{
var textBoxes = GetControls(this, typeof(TextBox));
foreach (var textBox in textBoxes)
{
((TextBox)textBox).Clear();
}
}

关于c# - 完成特定任务后清除每件事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21738154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com