首页 / 问答 / C# Winforms桌面应用程序中如何简化for循坏嵌套循环的写法?

C# Winforms桌面应用程序中如何简化for循坏嵌套循环的写法?

C# WINFORM 1.6K 次浏览
0
string qxsz1 = "";
string qxsz2 = "";
string qxsz3 = "";
string qxsz4 = "";
for (int j = 0; j < CheckBoxList1.Items.Count; j++)
{
    if (CheckBoxList1.Items[j].Selected == true)
        qxsz1 += CheckBoxList1.Items[j].Value + ",";
}

for (int j = 0; j < CheckBoxList2.Items.Count; j++)
{
    if (CheckBoxList2.Items[j].Selected == true)
        qxsz2 += CheckBoxList2.Items[j].Value + ",";
}

for (int j = 0; j < CheckBoxList3.Items.Count; j++)
{
    if (CheckBoxList3.Items[j].Selected == true)
        qxsz3 += CheckBoxList3.Items[j].Value + ",";
}

for (int j = 0; j < CheckBoxList4.Items.Count; j++)
{
    if (CheckBoxList4.Items[j].Selected == true)
        qxsz4 += CheckBoxList4.Items[j].Value + ",";
}

string qx = qxsz1 + qxsz2 + qxsz3 + qxsz4 + qxsz5 + qxsz6 + qxsz7;

获取多个单选框选择的值 ,如何用一个循坏得到 qx ?

回复 [×]
提交评论
请输入评论内容

1 个回答

  • 0

    写了一个只用LINQ的示例,CheckedListBox个数是动态的,先看运行效果:

    设计器代码(Form2.Designer.cs):

    后端代码(Form2.cs)

    Rector的个人主页

    Rector

    2022-02-24 回答

    我来回答