DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Populating Data Based On Multiselection ListBox Reference Need Help

Solved!
Go to solution

Hello again,

 

The issue I'm having is selecting from a multiselection listbox in order to populate charts with the data selected. My code is as follows:

 

Sub loadData_EventClick(ByRef This)

   Dim j

   Dim k

   k = 0

   If ListBox.MultiSelection.Count = 0 Then Exit Sub

      For j = 1 To ListBox.Items.Count

         If ListBox.MultiSelection(j).Count = True Then

            k = k + 1

         End If

      Next

   TextBox.Text = k

End Sub

 

The code works flawlessly up until the second If statement. My current Channel Group List contains 16 channels and with the second If statement commented out the textbox output is equal to 16 (i.e. k = 16), which is what I would expect. Im just using the k = k + 1 for testing. Eventually that will be replaced with functioning code specific to what I need to do.

 

So the issue I am having is getting the code to recognize which values are selected and referencing the index for those selections in an array. Im not certain on how to do this.

 

Any help is much appreciated.

 

Thanks,

 

~Nathan

 

Jpeg attached shows the current GUI and the output of k with the second if statement commented out

 

0 Kudos
Message 1 of 4
(4,260 Views)
Solution
Accepted by topic author Nate102

Hey Nathan -

 

ListBox.MultiSelection() is a function that returns an array of the [multiple] selected elements in the ListBox.

ListBox.MultiSelection.Count is a property that returns the number of items in the aforementioned array. 

 

Therefore, the logic in your second IF statement which says If ListBox.MultiSelection(j).Count = True doesn't really make sense because:

  1. Indexing the array returned from MultiSelection (by using j) now refers to a single selected item in the list (which doesn't have its own "Count" property anymore)
  2. If you don't index the array, assuming you'd selected 4 items in your list, your statement would still effectively be like saying If 4 = True which isn't what you're getting at. 

Make sense?

 

Here's a modified version of your code that hopefully makes it a little bit more obvious how these items relate to one another.  Give it a run; obviously 75% of the code is extranneous for what you're ultimately working toward, so make sure you eventually delete what you won't need.

 

Sub loadData_EventClick(ByRef This) 'Created Event Handler
  Dim j
  If ListBox.MultiSelection.Count = 0 Then Exit Sub
  TextBox.Text = "Total ListBox Items: " & ListBox.Items.Count & vbCrLf
  TextBox.Text = TextBox.Text & "Total Selected Items: " & ListBox.MultiSelection.Count & vbCrLf
  TextBox.Text = TextBox.Text & "Selected Items:" & vbCrLf
  For j = 1 to ListBox.MultiSelection.Count
    TextBox.Text = TextBox.Text & ListBox.MultiSelection(j).Text & ", Index " & ListBox.MultiSelection(j).Index & vbCrLf
  Next
End Sub

 You're making great progress; it will get rapidly easier quickly and we're here to help - don't hesitate.

Derrick S.
Product Manager
NI DIAdem
National Instruments
Message 2 of 4
(4,251 Views)

<Removing duplicate post>

Derrick S.
Product Manager
NI DIAdem
National Instruments
0 Kudos
Message 3 of 4
(4,250 Views)

Once again Derrick, thank you!

 

You cleared up several things for me there.

 

~Nathan

0 Kudos
Message 4 of 4
(4,229 Views)