如何以正确的顺序(递增顺序)遍历groupbox中包含的复选框 [英] How to iterate checkboxes held in groupbox in correct order (increasing order)

查看:118
本文介绍了如何以正确的顺序(递增顺序)遍历groupbox中包含的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码(PART OF CODE)

SqlCommand cmd =新的SqlCommand("SELECT * FROM SEATING",cs);
SqlDataReader reader = cmd.ExecuteReader();
foreach(groupBox1.Controls中的var c)
{

reader.Read();
CheckBox temp =(CheckBox)c;
int座位= reader.GetInt32(0);
int状态= reader.GetInt32(2);

if(状态== 1&&座位== Convert.ToInt32(temp.AccessibleName))
{
temp.Checked = true;
temp.Enabled = false;
}


}

这样就可以了,即如果复选框的状态为1,则将其标记为已选中,然后将其禁用,但是禁用的顺序是相反的(即,从头到尾)
问题是因为foreach循环从末尾开始进行迭代
您能帮我从头开始进行迭代吗?

我尝试过的事情:

好吧,我读了一些有关LINQ的文章,但不理解它. microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol(v=vs.110).aspx">Control.GetNextControl方法(控件,布尔值)(System.Windows.Forms) [ SqlDataReader reader = cmd.ExecuteReader();
foreach (var c in groupBox1.Controls)
{

reader.Read();
CheckBox temp = (CheckBox)c;
int seat = reader.GetInt32(0);
int status = reader.GetInt32(2);

if (status == 1 && seat == Convert.ToInt32(temp.AccessibleName))
{
temp.Checked = true;
temp.Enabled = false;
}


}

this does the work, i.e marks the checkboxes as checked if its status is 1, and disables them, but the order in which they get disabled is reverse( i.e from end to first)
the problem is because the foreach loop , it start iterating from the end
can u help me make it iterate from start?

What I have tried:

Well i read about something called LINQ but didnt understood it

If you need to iterate in tab order then use the Control.GetNextControl Method (Control, Boolean) (System.Windows.Forms)[^].


An alternative to setting the tab order might be something like this:
1) you fetch the Checkboxes out of the groupBox controls collection and sort them e.g. by name

      List<CheckBox> listCheckboxesOrderedByName = 
        groupBox1.Controls
          .Cast<CheckBox>()
          .OrderBy(c => c.Name)
          .ToList();


foreach (var c in listCheckboxesOrderedByName )
{
  // 2) now this loop runs through the checkboxes 
  // that have been sorted by name

}


这篇关于如何以正确的顺序(递增顺序)遍历groupbox中包含的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆