Skip to main content

Problem with using ListBoxs to select data

Hello, 
I am having a problem with using listboxs to select some data for use by the user. It only happens with one specific listbox eve4 n though the code is exactly the same. 
I have a dialog with 4 ListBoxs. ListBox1 holds the initial data and as the user makes selections the selected field goes to ListBox2. (Very similar to how the code works in several of Brian's Snippets). This works perfectly for ListBox3 to ListBox4. The problem I have is with ListBox2 and when the user pushs the button to remove an item. The code acts as though item -1 in the array has been selected everytime. 
Any help that can be provided would be greatly appreciated. I've attached the full code. The problem area code is below: 
If SSMM1 = 1 And SS1.listbox1 <> -1 Then 
chosensg = SS1.listbox1
chosentxt = listbox1$(chosensg)
 
z = 0
 
Do Until listbox2$(z) = ""
z = z + 1
Loop
 
listbox2$(z) = chosentxt
listbox1$(chosensg) = ""
End If
 
If SSMM1 = 2 And SS1.listbox2 <> -1 Then
chosensg = SS1.listbox2
chosentxt = listbox2$(chosensg)
 
  z = 0
 
Do Until listbox1$(z) = ""
z = z + 1
Loop
 
  listbox1$(z) = chosentxt
listbox2$(chosensg) = ""
End If
 
Call cleanup_listbox1
Call cleanup_listbox2
 
 
 

Brian Element Mon, 10/17/2016 - 07:36

Hi Chris, any chance you can attach your entire script so I can see how the dialogs are set-up?

Thanks

Brian

chdarc Mon, 10/17/2016 - 20:27

See attachment. 
I think I've fixed it. Though I've noticed that whenever the user selects to remove an item it puts this at the botton of the ListBox and is not in numerical order. I've tried using the sortarray function you wrote and this works for putting them in order, but then when you select a new item to include it doesn't move the selected item but instead another. Hopefully that explanation makes sense. 
The dialog also seems to blink each time I press one of the buttons to select or unselect an item. 
Any help you can provided would be much appreciated! 

Brian Element Tue, 10/18/2016 - 08:01

Hi Chris,

Well explaining the blinking is fairly easy.  What is happending is you have the loop that continually calls the dialog until it meets the requirements.  What is actually happpening is that you call the dialog, the person makes a selection and then the dialog gets destroyed, it goes back to your loop, the loop validates the information and recreates the dialog if necessary to get more information.  There is a way to get around this but you have to move lots of your code into the DisplayIt(ControlID$, Action%, SuppValue%) function.  In the DisplayIt function you can add a line of code that forces the dialog to stay open until you decide to close it.

DisplayIt = 0 forces the dialog to stay open and DisplayIt = 1 will let the dialog close.

For the ListBox all my code is in the related DisplayIt function.  It also needs some global variables to remember what the user has selected from the ListBox and I don't see those variables in your script, so that is probalby why you are not getting the selection you expected.

Hope this helps explain some of your questions.