0

我刚刚完成了这段代码,想把下面的代码改成循环。每个部门都有一个他们应该单击的特定按钮,并且应该只对部门 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

4 回答 4

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;
    }
  }                   
}
于 2013-03-28T17:33:04.360 回答
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;
            }
        }
    }
于 2013-03-28T17:29:07.133 回答
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);

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

例如

Button[] buttons = new Button[] { btnDept1, btnDept2, btnDept3};
for (int i = 0; i < buttons.Length; i++)
   SetButton(i, buttons[i]);
于 2013-03-28T17:33:07.203 回答
0

这是获取所需按钮的更简单方法。i只需将值附加到“ btnDept”中ActiveForm.Controls[...]。检查null,然后去工作......

    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.
            }
        }
    }
于 2013-03-28T17:39:35.620 回答