Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make this repetitive statement into a loop [closed]

This Code is too repetitive! I have to re write this code up to twenty! Can you guys help me covert this into a loop that wil just move from textbox1, to 2 to 3 and so on?

    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

This works, thanks for the help guys, the issue was the direct cast. Solved:

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
like image 924
RexPRGMER Avatar asked Nov 10 '22 11:11

RexPRGMER


1 Answers

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

...but you should also consider using the CheckedListBox control instead. That will allow your menu to be dynamically loaded (loaded from a text file or a database), and you won't need to manage multiple checkbox controls.

Here's the CheckedListBox version...

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
like image 95
SSS Avatar answered Nov 15 '22 05:11

SSS