【问题标题】:Loop through checkbox dynamically to check it is true or not动态循环检查复选框以检查它是否为真
【发布时间】:2013-04-13 16:35:46
【问题描述】:

我在一个表单上有 37 个复选框。我保留了每个复选框的名称,例如:p1p2p3.........p36p37。所以现在在 VB.NET 中,我使用通过点字符串循环所有控件来控制转换并获取复选框的值。

以下是我使用的 VB.NET 代码,它可以工作。

    Dim Start As Integer = 0
        Dim StrAdd As String = ""
        For i As Integer = 1 To 37
            Const chk As String = "p"
            Dim chkBox As CheckBox = CType(per_box.Controls(chk & i), CheckBox)
            If chkBox.Checked = True Then
                If Start = 0 Then
                    StrAdd = StrAdd + "1"
                    Start = 1
                Else
                    StrAdd = StrAdd + ":1"
                End If
            Else
                If Start = 0 Then
                    StrAdd = StrAdd + "0"
                    Start = 1
                Else
                    StrAdd = StrAdd + ":0"
                End If
            End If
        Next

但是,在创建 C# 应用程序时,我尝试做同样的事情,但它不起作用。谁能帮忙。

这是我的 C# 代码:

        string chk = "p";
        int start = 1;
        string str = "";
        for (int i = 1; i <= 37; i++)
        {
            chk = chk + i;
            CheckBox chkBox = (CheckBox)tab_patients.Controls[chk + i];
            if (chkBox.Checked == true)
            {
                if (start == 1)
                {
                    str = str + "1";
                    start = 0;
                }
                else
                {
                    str = str + ":1";
                }
            }
            else
            {
                if (start == 1)
                {
                    str = str + "0";
                    start = 0;
                }
                else
                {
                    str = str + ":0";
                }
            }
        }

【问题讨论】:

    标签: c# .net vb.net winforms checkbox


    【解决方案1】:

    删除这一行

    chk = chk + i;
    

    因为当你这样做时,你的chk 在第一次迭代时变成chk1 然后通过Controls 数组访问控件,如下所示:

    CheckBox chkBox = (CheckBox)tab_patients.Controls[chk + i];
    

    你实际上是这样做的:

    CheckBox chkBox = (CheckBox)tab_patients.Controls[chk11];
    

    幸运的是,您有 chk11,所以它可能适用于第一次迭代。

    现在看看下一次迭代会发生什么,

    对于下一次迭代,chk,当前值为chk1,将是chk+i,即chk12 再次在 .Controls 中添加一个 i,因此 chk 值变为 chk122

    CheckBox chkBox = (CheckBox)tab_patients.Controls[chk122];
    

    这没什么,因此不起作用。等等

    您不应该修改 chk 值。只需在Controls 中使用它,但不要为其赋值。

    还有 在 VB 中,您正在这样做:

    If chkBox.Checked = True Then
                    If Start = 0 Then
                        StrAdd = StrAdd + "1"
                        Start = 1
                    Else
                        StrAdd = StrAdd + ":1"
                    End If
    

    而在 C# 中你正在这样做:

      if (chkBox.Checked == true)
                {
                    if (start == 1)
                    {
                        str = str + "1";
                        start = 0;
                    }
                    else
                    {
                        str = str + ":1";
                    }
                }
    

    这两种逻辑略有不同。您在 Vb 中比较 0,如果为真,则附加 1,但在 C# 中,您在比较 1,如果为真,则附加 1

    【讨论】:

    • 谢谢 :) 一个小错误,我被困了将近 5 个小时 :P 很高兴来到 Stackoverflow
    • 这发生在我们最好的人身上 :)
    【解决方案2】:

    这是一种糟糕的编程习惯。为什么不将所有复选框放在一个面板中,然后循环通过面板的控件(在本例中为复选框)?

    Dim str as string=""
    :For i as integer =0 to 36
    :Select case panel1.controls(i).checked
    :Case true
    :Str=str & "1:"
    :Case false
    :Str=str & "0:" 
    :End select
    :Next i
    :Str=microsoft.visualbasic.right(str,str.length-1)'remove the colon at last
    

    '这样可以避免许多不需要的变量,例如 start 和 chk '常量和 if 语句。转换时不会有任何困难 '以上代码转c#。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 2021-09-29
      • 2014-03-02
      • 2019-02-22
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      相关资源
      最近更新 更多