none
I Can't Make My list Array Appear In My CheckBox RRS feed

  • Question

  • I am very new to visual basic and am working on a class assignment. When I try to create an array program, it seem to work. There are no errors or anything. It runs. However, after it is done I am left with a form and a empty checkbox, and I am not sure why. This even happens when I follow along with someone else's code, and when I try to create my own. I am sure I am just missing something obvious. Here is my code. I'd appreciate it if someone could look at it, or even run it and see if they see my values in the checkbox.

    PublicClassForm1

    PrivateSubBooks_SelectedIndexChanged(sender AsObject, e AsEventArgs) HandlesBooks.SelectedIndexChanged

    DimMyBooks(3) AsString

    MyBooks(0) = "The Maze Runner"

    MyBooks(1) = "The Hunger Games"

    MyBooks(2) = "Divergent"

    MyBooks(3) = "The Fault in Our Stars"

    Fori AsInteger= 0 ToMyBooks.GetUpperBound(0)

    Books.Items.Add(MyBooks(0))

    MessageBox.Show(MyBooks(0))

    Next

    EndSub

    EndClass

    Thursday, July 13, 2017 12:06 PM

All replies

  • You need to show how your form is built up:

    -what is Books which has the SelectedIndex Changed event? ComboBox, ListView?

    From the code you posted it's obvious that you are recreating the MyBooks array everytime the selection changes; this is inefficient. Just create the MyBooks as Load time.

    Also, you are adding to Books when Books SelectedIndexChanged event happen, that doesn't really make sense.

    So, explain what your program should do and we'll try to help you with its structure.

    Thursday, July 13, 2017 12:23 PM
  • Hi

    Your code is confusing. Here is an illustration of 3 ways (including your own) that may help. I use a CheckedListBox control on Form1 to list the Books - a CheckBox control only shows a Check/unchecked box not a list.

    ' Form1 with CheckedListBox named "Books"
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim MyBookList As New List(Of String)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' One way 
            With MyBookList
                .Add("The Maze Runner")
                .Add("The Hunger Games")
                .Add("Divergent")
                .Add("The Fault in Our Stars")
            End With
            Books.DataSource = MyBookList
            ' ===================================
    
            ' second way
            'With Books
            '    .Items.Add("The Maze Runner")
            '    .Items.Add("The Hunger Games")
            '    .Items.Add("Divergent")
            '    .Items.Add("The Fault in Our Stars")
            'End With
            ' ===================================
    
            ' your way
            'Dim MyBooks(3) As String
            'MyBooks(0) = "The Maze Runner"
            'MyBooks(1) = "The Hunger Games"
            'MyBooks(2) = "Divergent"
            'MyBooks(3) = "The Fault in Our Stars"
            'For i As Integer = 0 To MyBooks.GetUpperBound(0)
            '    Books.Items.Add(MyBooks(i))
            'Next
    
        End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Thursday, July 13, 2017 12:26 PM
  • Below is a Form with Checkbox's and Labels on it from this thread https://social.msdn.microsoft.com/Forums/en-US/e2743a3b-980f-4b9c-85de-641ce5e2dde9/label-changes-according-to-the-checkboxes-arrays?forum=vbgeneral in Karen Instructors post.

    I'm not certain how you, Angelxx12, are providing values to a Checkbox. A Checkbox can either be checked or unchecked although the caption or text for it can be altered. And there's a CheckedListBox available but I'm not capable of discerning your intent with the information you have provided.


    La vida loca

    Thursday, July 13, 2017 4:36 PM
  • Hi Angelxx12,

    Based on your description, you want to bind the array to checklistbox, if yes, please refer to:

    Dim MyBooks(3) As String
            MyBooks(0) = "The Maze Runner"
            MyBooks(1) = "The Hunger Games"
            MyBooks(2) = "Divergent"
            MyBooks(3) = "The Fault in Our Stars"
            For i As Integer = 0 To MyBooks.GetUpperBound(0)
                Books.Items.Add(MyBooks(i))
            Next

    The Books is the CheckListBox.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 14, 2017 7:23 AM
    Moderator
  • However, after it is done I am left with a form and a empty checkbox, and I am not sure why.

    You need to use the loop index variable as the index into the array:

    For i AsInteger= 0 To 3
      Books.Items.Add(MyBooks(i)) 
      'MessageBox.Show(MyBooks(i))
    Next
    

    However that would fix a problem of seeing four lines of your first book. If you are seeing nothing at all, then either that function isn't being called at all, or 'Books' isn't what you think it is.   See here for the procedures you need to find out what it going on:
    https://vbdotnetblog.wordpress.com/overview/debugging/

    especially for single stepping your code and using the Watch window.

    Friday, July 14, 2017 7:59 AM