KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

ListBox Remove Multiple Selected Items

 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
mercury
KiXforms Regular
KiXforms Regular


Joined: 10 Aug 2005
Posts: 50

PostPosted: Fri Nov 11, 2005 2:02 pm    Post subject: ListBox Remove Multiple Selected Items Reply with quote

This is a bit of a newby question..

How do I remove multiple selected items from a .Net ListBox?

The SelectedItems Property contains a list of the current Selection but not a reference to the original index of the item. Is there a way to retrieve this info or another way to bring about the same effect?

The best I can do at the moment is remove the selection if their respective indexes match. For example if the ListBox contains 10 objects and I select the top 3 then those object will go as expected. If I segment the selection (i.e item indexes 1, 5, 9) then I still remove indexes 1,2,3 as that is their index in the SelectedItems collection rather than their original position in the ListBox.
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Nov 11, 2005 2:17 pm    Post subject: Reply with quote

A newby question ? Nope! I fine observation about lacking functionality ? Yup

We will need to add two things for the next release:

ListBox.SelectedIndices

Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.

ListBox.GetSelected(index)

Returns a value indicating whether the specified item is selected.

A new build will be out later today.
Back to top
View user's profile Send private message
Lonkero
KiXforms Devotee
KiXforms Devotee


Joined: 13 Mar 2003
Posts: 1022
Location: Espoo, Finland

PostPosted: Fri Nov 11, 2005 3:54 pm    Post subject: Reply with quote

getselected?

sounds like return all selected indexes.

isSelected() sounds better in my ear. can we vote on the name?

_________________
Hammer
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Nov 11, 2005 4:00 pm    Post subject: Reply with quote

lol, I didn't pick the name ... Bill G picked the name.
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Nov 11, 2005 4:09 pm    Post subject: Reply with quote

btw - if there is one thing I've learned about .Net naming conventions, is that there is always a reason why things are named the way they are.
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Nov 11, 2005 5:36 pm    Post subject: Reply with quote

Ok, new build has been posted with changes as discussed ... there are two ways items at selected indexes can be removed now, but be careful when removing items by index. As one is "walking-through" an indexed collection, and removing things by index, sometimes things don't work-out as planned - because your walking the index - and deleting the index at the same time.

To overcome this issue, sometimes, its best to work your way from the end of list to the beginning - heres one of deleting by index:

Code:

Break On

$System = CreateObject("Kixforms.System")

$Form = $System.Form()

$ListBox = $Form.Controls.Add($System.ListBox())
$ListBox.Center
$ListBox.SelectionMode = $System.SelectionMode_MultiExtended

For $i = 1 to 10
 $= $ListBox.Items.Add("Item-$i")
Next

$DeleteButton = $Form.Controls.Add($System.Button())
$DeleteButton.Center
$DeleteButton.Top = $ListBox.Bottom
$DeleteButton.Click = "DeleteButtonClick()"
$DeleteButton.Text = "Delete"

$Form.Center
$Form.Show
While $Form.Visible
 $= Execute($Form.DoEvents)
Loop

Exit 0

Function DeleteButtonClick()

 For $i = $ListBox.SelectedIndices.Count - 1 To 0 Step -1
  $ListBox.Items.RemoveAt($ListBox.SelectedIndices.Item($i))
 Next

 $ListBox.Select

EndFunction


-Shawn
Back to top
View user's profile Send private message
mercury
KiXforms Regular
KiXforms Regular


Joined: 10 Aug 2005
Posts: 50

PostPosted: Fri Nov 11, 2005 8:13 pm    Post subject: Reply with quote

Hi Shawn,

Thanks for the swift additions!

And also for the point about removal order - Pretty sure I would have been sctratching my head about that one for a while. My ListBox has the Sorted property set. Hopefully this won't complicate things too much.
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Nov 11, 2005 8:39 pm    Post subject: Reply with quote

Dont think a sorted list box should matter. In terms of the head-scratching, I did enough of that for the both of us. If you see the need for any other properties/methods/classes please dont hesitate to post.
Back to top
View user's profile Send private message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Fri Nov 11, 2005 8:43 pm    Post subject: Reply with quote

btw - here is "the other" way of deleting selected indexes. not as efficient but looks more straight-forward:

Code:

Function DeleteButtonClick()

 For $i = $ListBox.Items.Count - 1 To 0 Step -1
  If $ListBox.GetSelected($i)
   $ListBox.Items.RemoveAt($i)
  Endif
 Next

EndFunction
Back to top
View user's profile Send private message
Lonkero
KiXforms Devotee
KiXforms Devotee


Joined: 13 Mar 2003
Posts: 1022
Location: Espoo, Finland

PostPosted: Wed Jan 04, 2006 1:26 am    Post subject: Reply with quote

and what about a third one:
Code:

function DeleteButtonClick()
$SelectedItems = $ListBox.SelectedItems
 For $i = 0 to $SelectedItems.Count - 1
   $ListBox.Items.Remove($SelectedItems.Items.Item($i))
 Next
EndFunction

_________________
Hammer
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group