我在一个表单上有 37 个复选框。我保留了每个复选框的名称,例如:p1
p2
p3
.........
p36
p37
. 所以现在在 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";
}
}
}