此代码过于重复!我必须重新编写这个代码到二十个!你们可以帮助我把它转变成一个循环,只需从textbox1移动到2到3等等吗?
If CheckBox1.Checked = True Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(frmLunchBoxMenu.LineNumber.ToString + ".) An order of " + CheckBox1.Text & Environment.NewLine)
End If
If CheckBox2.Checked = True Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(frmLunchBoxMenu.LineNumber.ToString + ".) An order of " + CheckBox2.Text & Environment.NewLine)
End If If CheckBox1.Checked = True Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(frmLunchBoxMenu.LineNumber.ToString + ".) An order of " + CheckBox1.Text & Environment.NewLine)
End If
If CheckBox2.Checked = True Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(frmLunchBoxMenu.LineNumber.ToString + ".) An order of " + CheckBox2.Text & Environment.NewLine)
End If
If CheckBox3.Checked = True Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(frmLunchBoxMenu.LineNumber.ToString + ".) An order of " + CheckBox3.Text & Environment.NewLine)
End If
If CheckBox4.Checked = True Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(frmLunchBoxMenu.LineNumber.ToString + ".) An order of " + CheckBox4.Text & Environment.NewLine)
End If
If CheckBox5.Checked = True Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(frmLunchBoxMenu.LineNumber.ToString + ".) An order of " + CheckBox5.Text & Environment.NewLine)
End If
这是有效的,感谢帮助人员,问题是直接演员。解决:
Public Class frmBeverages
Dim Range As Integer = 0
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
While Range < 14
Range = Range + 1
Dim chk As CheckBox =
DirectCast(Me.Controls("Checkbox" & CStr(Range)), CheckBox)
If chk.CheckState = CheckState.Checked Then
frmLunchBoxMenu.LineNumber = frmLunchBoxMenu.LineNumber + 1
frmLunchBoxMenu.txtOrder.AppendText(CStr(frmLunchBoxMenu.LineNumber) & ".) An order of " & chk.Text & Environment.NewLine)
End If
End While
Close()
End Sub
End Class
答案 0 :(得分:1)
Dim strOutput As String = ""
Dim intLineNumber As Integer = 0
For Each ctl As Control In Me.Controls
If TypeOf ctl Is CheckBox Then
Dim chk As CheckBox = DirectCast(ctl, CheckBox)
If chk.Checked Then
intLineNumber += 1
strOutput &= intLineNumber.ToString + ".) An order of " + ctl.Text & vbCrLf
End If
End If
Next ctl
txtOrder.Text = strOutput
...但你也应该考虑使用CheckedListBox控件。这将允许您的菜单动态加载(从文本文件或数据库加载),您不需要管理多个复选框控件。
这是CheckedListBox版本......
Public Class frmLunchBoxMenu
'Add a CheckedListBox, a Button, and a TextBox named txtOrder to the form
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckedListBox1.Items.Clear()
CheckedListBox1.Items.Add("Hamburger")
CheckedListBox1.Items.Add("Pizza")
CheckedListBox1.Items.Add("Cola")
CheckedListBox1.Items.Add("Chips")
CheckedListBox1.Items.Add("Hot dog")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strOutput As String = ""
Dim intLineNumber As Integer = 0
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
If CheckedListBox1.GetItemChecked(i) Then
intLineNumber += 1
strOutput &= intLineNumber.ToString + ".) An order of " + CheckedListBox1.Items(i).ToString & vbCrLf
End If
Next i
txtOrder.Text = strOutput
End Sub
End Class
答案 1 :(得分:0)
这可能是一种方法:
Dim Range As Integer() =
Enumerable.Range(1, 20).ToArray
For Each n As Integer In Range
Dim chk As CheckBox =
DirectCast(Me.Controls("Checkbox" & CStr(n)), CheckBox)
' Specify above the right ControlCollection if needed.
If chk.Checked Then
With frmLunchBoxMenu
.LineNumber += 1
.txtOrder.AppendText(CStr(frmLunchBoxMenu.LineNumber) &
".) An order of " & chk.Text &
Environment.NewLine)
End With
End If
Next n