我在表單上有37個複選框。我保留了每個複選框名稱,如:p1
p2
p3
.........
p36
p37
。所以現在在VB.NET中,我使用doint字符串循環所有控件來控制投射並獲取複選框的值。動態循環複選框以檢查它是否爲真
以下是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";
}
}
}
謝謝:)一個小錯誤,我被卡住了足足5小時:P大是在這裏#1 – 2013-04-20 10:15:00
它發生在我們最好的:) – 2013-04-20 10:15:32