如何实现';更新';作用

本文关键字:更新 作用 何实现 实现 | 更新日期: 2023-09-27 18:24:51

我在学校做了一些练习,最后为列表框写了一段代码,如下所示。现在请注意,3行代码总是在3个不同的私有空间中返回;

int numberOfItems = lstItems.Items.Count;
string y = numberOfItems.ToString();
txtAantal.Text = y;

我想把它们放在某种函数或方法中,我可以在需要这些代码的每个私有空间中简单地调用这些函数或方法,但我不知道如何做到这一点,有人能帮我吗?

namespace IList
{
public partial class frmAddItem : Form
{
    public frmAddItem()
    {
        InitializeComponent();
    }
    private void btnAdd_Click(object sender, EventArgs e)
    {
        string newItem = txtItem.Text;
        lstItems.Items.Add(newItem);
        int numberOfItems = lstItems.Items.Count;
        string y = numberOfItems.ToString();
        txtAantal.Text = y;
        txtItem.Text = "";
    }
    private void lstItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Selected = lstItems.SelectedIndex;
        Selected += 1;
        string x = Selected.ToString();
        txtSelected.Text = x;
    }
    private void btnRemoveAt_Click(object sender, EventArgs e)
    {
        int Location = int.Parse(txtRemoveAt.Text);
        Location -= 1;
        lstItems.Items.RemoveAt(Location);
        int numberOfItems = lstItems.Items.Count;
        string y = numberOfItems.ToString();
        txtAantal.Text = y;
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        lstItems.Items.Clear();
        int numberOfItems = lstItems.Items.Count;
        string y = numberOfItems.ToString();
        txtAantal.Text = y;
    }
}
}

如何实现';更新';作用

实际上,这三行代码可以变成一行代码,这样就不需要将它们提取到一个独立的方法中:

textAantal.Text = lstBox.Items.Count.ToString();

编写一个单独的私有方法,如下所示。

 private void myActionMethod(string action)
    {
      switch (action)
      {
        case "btnAdd_Click"
              string newItem = txtItem.Text;
              lstItems.Items.Add(newItem);
              int numberOfItems = lstItems.Items.Count;
              string y = numberOfItems.ToString();
              txtAantal.Text = y;
              txtItem.Text = "";
             break;
       case "lstItems_SelectedIndexChanged"
            int Selected = lstItems.SelectedIndex;
            Selected += 1;
            string x = Selected.ToString();
            txtSelected.Text = x;
            break;
      case "btnClear_Click"
            int Location = int.Parse(txtRemoveAt.Text);
            Location -= 1;
            lstItems.Items.RemoveAt(Location);
            int numberOfItems = lstItems.Items.Count;
            string y = numberOfItems.ToString();
            txtAantal.Text = y;
            break;
    case "btnRemoveAt_Click"
          int Location = int.Parse(txtRemoveAt.Text);
          Location -= 1;
          lstItems.Items.RemoveAt(Location);
          int numberOfItems = lstItems.Items.Count;
          string y = numberOfItems.ToString();
          txtAantal.Text = y;
          break;
      }
    }

在每次事件中调用您的方法

    private void btnAdd_Click(object sender, EventArgs e)
    {
      myActionMethod("btnAdd_Click");
    }
    private void lstItems_SelectedIndexChanged(object sender, EventArgs e)
    {
       this. myActionMethod("lstItems_SelectedIndexChanged");
    }
   private void btnRemoveAt_Click(object sender, EventArgs e)
    {
        this. myActionMethod("btnRemoveAt_Click");
    }
   private void btnClear_Click(object sender, EventArgs e)
   {
       this. myActionMethod("btnClear_Click");
   }

您可以将myActionMethod命名为任何名称MyUpdateMethod或其他名称。我希望这能有所帮助。