Text in Listboxes

I am very new to VB programming (taking it as a HS course), and I'm having some trouble with listboxes. I have a form made up for a 'restaurant order' program (the criteria came from the course... :p ) which is composed of radio buttons, check boxes, two command buttons, and a list box. Buttons are no problem (unless I've done something wrong that I don't realize), but I don't think I'm handling the list box correctly. I have it coded so that the selected buttons/boxes send their text to the box's 'text' property (to produce an 'order' listed in the box) when a certain button is pressed, but it does not work. Nothing will appear in the list box--I even tried simply writing a line of code that assigned a string literal to the box's text via the code window (cutting out the complication of working with the radio buttons), but even this failed. The list box does not appear to have a text property at all when I view its properties via the design window, so I have suspicions that I'm addressing the wrong property when attempting to add text. I did notice the "items" property in there, but I don't know how I would use it with the buttons. What am I doing wrong here, and how are you supposed to get text displayed in a list box? Thanks a lot! Code Below:

Public Class Form1

Private Sub btnOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrder.Click

'determine which Bread radio button is marked true
If (rbB1.Checked = True) Then
ListBox1.Text = rbB1.Text 'insert the appropriate text
ElseIf (rbB2.Checked = True) Then
ListBox1.Text = rbB2.Text
ElseIf (rbB3.Checked = True) Then
ListBox1.Text = rbB3.Text
End If

'determine which Cheese radio button is marked true
If (rbCh2.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbCh2.Text
ElseIf (rbCh3.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbCh3.Text
End If

'determine which Meat radio button is marked true
If (rbM1.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM1.Text
ElseIf (rbM2.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM2.Text
ElseIf (rbM3.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM3.Text
ElseIf (rbM4.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM4.Text
ElseIf (rbM5.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM5.Text
ElseIf (rbM6.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM6.Text
ElseIf (rbM7.Checked = True) Then 'randomly select one of the other six meats,
Randomize() 'using a randomized number which corresponds to arranged cases
Dim iSelect As Integer = Int(6 * Rnd() + 1)
Select Case iSelect
Case 1
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM1.Text
Case 2
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM2.Text
Case 3
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM3.Text
Case 4
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM4.Text
Case 5
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM5.Text
Case 6
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & rbM6.Text
End Select
End If

'Check each Condiments box to see if it is checked
If (cbC1.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & cbC1.Text
ElseIf (cbC2.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & cbC2.Text
ElseIf (cbC3.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & cbC3.Text
ElseIf (cbC4.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & cbC4.Text
ElseIf (cbC5.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & cbC5.Text
ElseIf (cbC6.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & cbC6.Text
ElseIf (cbC7.Checked = True) Then
ListBox1.Text = ListBox1.Text & vbNewLine.ToString() & cbC7.Text
End If
End Sub
End Class

Also, is there some more efficient way of determining what button from within a group box is selected? If/Then statements seem a bit cumbersome. Thanks again!

Comments

  • Solved my own problem--thanks anyway. It ends up, the list box wasn't necessary to satisfy the assignment, so I substituted a text box instead (which was far more suited to the task). Just for the record, though, is it possible to assign values to a list box from within the code, or does it have to be pre-defined, before the program is run?
  • : Solved my own problem--thanks anyway. It ends up, the list box
    : wasn't necessary to satisfy the assignment, so I substituted a text
    : box instead (which was far more suited to the task). Just for the
    : record, though, is it possible to assign values to a list box from
    : within the code, or does it have to be pre-defined, before the
    : program is run?
    :

    listboxname.additem 'string here'

    list1.additem command1.caption
  • Thanks for the tip!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories