How To Do A Listbox Array VB.Net

SOLVED Discussion in 'Web Design & Coding' started by Comptech, Dec 13, 2011.

  1. Comptech

    Comptech ICT Technican

    Joined:
    Dec 11, 2011
    Messages:
    16
    Likes Received:
    14
    Location:
    UK
    If you need to ever do a listbox as a refrance and click on each item and take you to a next page the following code on a click event is what you are looking for,

    Code under clicker event ie button or radio button checkbox or whatever



    Code,

    Dim questions As Form() = New Form() {Form2, Me, Form2, Me}
    questions(0) = Form2

    questions(CheckedListBox1.SelectedIndex).Show()

    ------------------------------------------------------------------------------------------------

    Dim question as form() = new form() {form2,Me "add each for into that section"

    questions(0) = Form2 "Put the oder of the listbox you want it displayed in"


    example

    Listbox starts with 0 so the first array would be set to 0 like the above sample



    questions(CheckedListBox1.SelectedIndex).Show() "This peace of code tells the computer the display the page if that is the seledted index"


    Hope this helps

    Kind Regards

    Bradley
     
    Last edited: Dec 13, 2011
    Comptech, Dec 13, 2011
    #1
    Zeus likes this.
    1. Advertisements

  2. Comptech

    AceInfinity Somewhere...

    Joined:
    Aug 13, 2012
    Messages:
    7
    Likes Received:
    0
    Location:
    Canada
    Code:
    Dim questions As Form() = New Form() {Form2, Me, Form2, Me}
    You're just extending the Form array by 2 elements when you add Form2 twice and Me (the main form presumably) twice. Which is not needed, there's no purpose for that. :)

    Code:
    questions(0) = Form2
    Then setting the first element of this array to the element that it already represents, also not necessary.

    If I may rephrase this? :)

    "Listbox elements start at index 0, so the first element would represent index 0"

    You're not setting the array to 0, you're retriving the first element of the Form array to either get or set that element within the index at 0...

    I can't see what you're trying to do here though, some of the code you have is confusing and no offence, makes no sense.

    If you want to show the form from the array of Form's, based on the CheckedListBox SelectedIndex, then this is really all you need. I'm not sure what the purpose of having a CheckedListBox over a regular ListBox is though.

    Code:
    Dim questions As Form() = New Form() {Me, Form2}
    questions(CheckedListBox1.SelectedIndex).Show()
    However, this also means that you require at least 2 ListBox items in order for this to work properly, and no more, and one other thing, you should be checking for IF a ListBox item is selected, otherwise if this code goes into the wrong event Sub, or is called based on the wrong conditions, then having no items selected will return a value of -1.

    You can't index that within an Array. the IndexOf method returns -1 when a specific searched element is not found, but otherwise you'll have an error on your hands.

    Code:
    If CheckedListBox1.SelectedIndex <> -1 Then
    	Dim questions As Form() = New Form() {Me, Form2}
    	questions(CheckedListBox1.SelectedIndex).Show()
    End If
     
    AceInfinity, Aug 13, 2012
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.