[Gambas-user] annoying glitch

Dimitris Anogiatis dosida at ...626...
Sun Mar 8 02:57:23 CET 2015


Hey Gary,

I assume you're using the two for loops to find which item on the source
listbox is selected and then removed after the addition to the second
listbox is done.
Gambas listboxes are a bit more flexible. You can get the index of the
selected item with  lstInCar.Index. If there is no item selected on the
listbox then the index is -1
If the Listbox is empty (ie no more items in it), lstInCar.Index becomes -1
again. To get the text that the current selected item has then you can use
lstInCar.Current.Text

So with this in mind your code can become:

Public Sub btnLeft_Click()
'if the lstInCar is not empty or if an item is selected
  If lstInCar.Index > -1 Then
      'Add the selected item to lstAtHome
      lstAtHome.Add(lstInCar.Current.Text)
      'Remove the item from lstInCar that you just added to lstAtHome
      lstInCar.Remove(lstInCar.Index)
  Endif
End

if you want to do the reverse (send an item back to lstInCar) you will have
to reverse the listboxes.

Depending on what behavior you want to achieve you can add the following
two lines after the EndIf
but BEFORE the End statement (so that the next two lines are within the
Sub):

      lstAtHome.Current.Selected = False
      lstInCar.Current.Selected = False

What these two lines will do is undo the selection from both listboxes just
in case the user decides to select something on both of them.

The code will finally become:

Public Sub btnLeft_Click()
'if the lstInCar is not empty or if an item is selected
  If lstInCar.Index > -1 Then
      'Add the selected item to lstAtHome
      lstAtHome.Add(lstInCar.Current.Text)
      'Remove the item from lstInCar that you just added to lstAtHome
      lstInCar.Remove(lstInCar.Index)
  Endif
      'Deselect items from both listboxes
      lstAtHome.Current.Selected = False
      lstInCar.Current.Selected = False
End

This piece of code works only for a simple list box where you can select
only one item at a time
For listboxes that allow multiple selections at a time the code will be a
bit different (but then we can
work on that a bit later).

I hope this helps you out in understanding Gambas a bit better.
Dimitris


On Sat, Mar 7, 2015 at 10:41 AM, Gary Emms <gary.emms at ...626...> wrote:

> Hi
> I'm trying to follow a tutorial from Linux Format. I have 2 list boxes
> and I'm trying to move an entry from one to the other using:
>
> Public Sub btnLeft_Click()
>
>     Dim i As Integer
>    'transfer selected itelms to left
>    For i = 0 To lstInCar.Count - 1
>      If lstInCar[i].Selected Then
>        lstAtHome.Add(lstInCar[i].Text)
>      Endif
>    Next
>    'clear the entries from the left (backwards)
>    For i = lstInCar.Count - 1 To 0 Step - 1
>      If lstInCar[i].Selected Then
>        lstInCar.Remove(i)
>      Endif
>    Next
> End
>
> it works fine except, If, and only if, I select the first item in the
> list after it is deleted the new top item is  automatically selected. Is
> this a bug or am I doing something wrong?
> Cheers
> Gary
>
>
> ------------------------------------------------------------------------------
> Dive into the World of Parallel Programming The Go Parallel Website,
> sponsored
> by Intel and developed in partnership with Slashdot Media, is your hub for
> all
> things parallel software development, from weekly thought leadership blogs
> to
> news, videos, case studies, tutorials and more. Take a look and join the
> conversation now. http://goparallel.sourceforge.net/
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



More information about the User mailing list