协助C#循环

时间:2013-03-28 17:27:30

标签: c# loops

我刚刚完成了此代码,并希望将以下代码更改为循环。每个部门都有一个特定的按钮,他们可以点击它们,并且只对那些在1,2或3部分内的人可见。

有人可以给我一些指导。

                pchk.Visible = true;

                Int32 count = 0;
         count = chk.GetCount(1);
         // dept 1
                if (count == -1)
                {
                    btnDept1.Visible = false;
                }
                else
                {
                    btnDept1.Text = "Next dep1[" + count.ToString() + "]";
                    if (count == 0)
                        btnDept1.Enabled = false;
                }

                // dept 2
                count = chk.GetCount(2);
                if (count == -1)
                {
                    btnDept2.Visible = false;
                }
                else
                {
                    btnDept2.Text = "Next dep2 [" + count.ToString() + "]";
                    if (count == 0)
                        btnDept02.Enabled = false;
                }
                // dept 3
                count = chk.GetCount(3);
                if (count == -1)
                {
                    btnDept3.Visible = false;
                }
                else
                {
                    btnDept3.Text = "Next dept3 [" + count.ToString() + "]";
                    if (count == 0)
                        btnDept3.Enabled = false;
                }
            }

4 个答案:

答案 0 :(得分:3)

将此转换为循环的最大挑战是根据数值获取btnDept1值。一种方法是引入一个用switch语句

执行此操作的方法
Button GetButton(int id) {
  switch (id) {
    case 1: return btnDept1;
    case 2: return btnDept2;
    ...
  }
}

或者另一种方法是将它们保存在一个数组中并使用索引来访问该数组。无论哪种方式,你可以通过以下方式折叠成循环

for (int i = 0; i < TheCount; i++) {
  int id = i + 1;
  Button button = GetButton(id);
  int count = chk.GetCount(2);
  if (count == -1) { 
    button.Visible = false;
  } else { 
    button.Text = String.Format("Next dep{0} [{1}]", id, count);
    if (count == 0) {
      button.Enabled = false;
    }
  }                   
}

答案 1 :(得分:0)

    for (int i = 1; i <= 3; i++)
    {
        count = chk.GetCount(i);
        if (count == -1)
        {
            switch (i)
            {
                case 1:
                    btnDept1.Visible = false;
                    break;
                case 2:
                    btnDept2.Visible = false;
                    break;
                case 3:
                    btnDept3.Visible = false;
                    break;
            }
        }
        else
        {
            switch(i)
            {
                case 1:
                    btnDept1.Text = "Next dep1[" + count.ToString() + "]";
                    if (count == 0)
                        btnDept1.Enabled = false;
                    break;
                case 2:
                    btnDept1.Text = "Next dep1[" + count.ToString() + "]";
                    if (count == 0)
                        btnDept1.Enabled = false;
                    break;
                case 3:
                    btnDept1.Text = "Next dep1[" + count.ToString() + "]";
                    if (count == 0)
                        btnDept1.Enabled = false;
                    break;
            }
        }
    }

答案 2 :(得分:0)

尝试这样的事情:

private void SetButton(int id, Button btn)
{
    var count = chk.GetCount(id);
    if (count == -1)
    {
        btn.Visible = false;
    }
    else
    {
        btn.Text = String.Format("Next dep{0}[{1}]", id.ToString(), count.ToString());
        if (count == 0)
            btn.Enabled = false;
    }

}

用法可以是这样的:

SetButton(1, btnDept1);
SetButton(2, btnDept2);
SetButton(3, btnDept3);

当然,如果你使用一个数组/按钮列表,它会更容易......

e.g。

Button[] buttons = new Button[] { btnDept1, btnDept2, btnDept3};
for (int i = 0; i < buttons.Length; i++)
   SetButton(i, buttons[i]);

答案 3 :(得分:0)

这是一种更简单的获取所需按钮的方法。只需将i值附加到btnDept中的“ActiveForm.Controls[...]”即可。检查是否为空,然后开始工作......

    for (int i = 0; i < deptCount; i++)
    {
        Button b = ActiveForm.Controls["btnDept" + i.ToString()] as Button;

        if (b != null)
        {
            if (count == -1)
            {
                b.Visible = true;
            }
            else
            {
                // etc.
            }
        }
    }