473,473 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Unselecting items in a Listbox (HOW?)

Hi.

Is there a way to "unselect" items in a listbox, programmatically?
Here's what I want to do (and I've been trying to to this while
debugging
the form, but I've been unsuccessful) ...

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

Let's say the user selects optionbutton 1 and enables the first listbox.
Then they select a few items in the first listbox.

Then they decide (before hitting the command button to generate the
query - which is the purpose of the form) that they really want to use
the 2nd list box. So, they hit the
other option button, which will disable listbox 1 and enable listbox 2.
However, the items that were selected in listbox 1 are still
highlighted. I want to get those highlighted items unhighlighted
(unselected).

Any ideas?
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #1
10 21798
SueB wrote in message <OI**************@news.uswest.net> :
Hi.

Is there a way to "unselect" items in a listbox, programmatically?
Here's what I want to do (and I've been trying to to this while
debugging
the form, but I've been unsuccessful) ...

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

Let's say the user selects optionbutton 1 and enables the first listbox.
Then they select a few items in the first listbox.

Then they decide (before hitting the command button to generate the
query - which is the purpose of the form) that they really want to use
the 2nd list box. So, they hit the
other option button, which will disable listbox 1 and enable listbox 2.
However, the items that were selected in listbox 1 are still
highlighted. I want to get those highlighted items unhighlighted
(unselected).

Any ideas?
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***


For multiselect listboxes, I believe one way is to reassigning the
rowsource

me!lstList.rowsource = me!lstList.rowsource

Else I think you could loop the list, and set each item

for l = 0 to me!lstList.listcount-1
me!lstList.selected(l) = false
next l

--
Roy-Vidar

Nov 13 '05 #2

Sue -

Try this (modified appropriately to fit your code):
Private Sub btnUseList2_Click()
Dim lngLoop As Long

lst1.Enabled = False
For lngLoop = 0 To lst1.ItemsSelected.Count - 1
lst1.Selected(lst1.ItemsSelected.Item(lngLoop)) = False
Next lngLoop
lst2.Enabled = True
End Sub

This code is run when the button to enable the second list box is clicked; it
does the following:
1) Disables the first list box
2) Cycles through the items in the first list box, de-selecting any that are
selected
3) Enables the second list box

Hope this helps!

Peter

SueB wrote:
Hi.

Is there a way to "unselect" items in a listbox, programmatically?
Here's what I want to do (and I've been trying to to this while
debugging
the form, but I've been unsuccessful) ...

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

Let's say the user selects optionbutton 1 and enables the first listbox.
Then they select a few items in the first listbox.

Then they decide (before hitting the command button to generate the
query - which is the purpose of the form) that they really want to use
the 2nd list box. So, they hit the
other option button, which will disable listbox 1 and enable listbox 2.
However, the items that were selected in listbox 1 are still
highlighted. I want to get those highlighted items unhighlighted
(unselected).

Any ideas?

Regards,
SueB

Nov 13 '05 #3
Roy,

Thank you for the quick reply. I'll give both methods a try and see
which works best for me.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #4
Maybe it is better if your code does take the option button into
account. What if I think again, and revert to listbox 1? I find all my
selections gone.

That said, you have to set the Selected(row) to False, for every row
that is selected.

dim item as variant
for each item in yourlistbox.itemsselected
yourlistbox.selected(item) = false
next

SueB wrote:
Hi.

Is there a way to "unselect" items in a listbox, programmatically?
Here's what I want to do (and I've been trying to to this while
debugging
the form, but I've been unsuccessful) ...

I have a form with two listboxes.

The form, also, has two option buttons in an options group: 1 button for
the first
listbox and 1 button for the otgher listbox. The idea is to select an
optionbutton to enable the associated listbox. This is working just
fine. However ...

Let's say the user selects optionbutton 1 and enables the first listbox.
Then they select a few items in the first listbox.

Then they decide (before hitting the command button to generate the
query - which is the purpose of the form) that they really want to use
the 2nd list box. So, they hit the
other option button, which will disable listbox 1 and enable listbox 2.
However, the items that were selected in listbox 1 are still
highlighted. I want to get those highlighted items unhighlighted
(unselected).

Any ideas?
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***


--
Bas Cost Budde, Holland
http://www.heuveltop.nl/BasCB/msac_index.html
For human replies, replace the queue with a tea

Nov 13 '05 #5
I'm sending out a global THANK YOU to all who replied. Wow. There
certainly look there are few different ways to approach this. I'll go
check out all the options and see what fits. Thanks again.
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #6
Just in case anybody else looks at this post...I discovered a slight
flaw with what was suggested above. When you loop through the selected
items in the list and use the itemsselected.count as the upper boundary
of the loop without saving that value initially and using the saved
value the count reduces with every loop (as you iterate through the
items). Also, the index value of the selected items changes if you loop
from 0 up to the total selected count. So ... here is what I used to
get around the problem (I loop from the last item back up to the first
item)...

Public Sub clrSelectedDates()

Dim ix As Long
Dim lngCount As Long

'Save the selected count
lngCount = Me!lstProcesseddt.ItemsSelected.Count - 1

'Loop from the end to the beginning of the selected
For ix = lngCount To 0 Step -1

Me!lstProcesseddt.Selected(Me!lstProcesseddt.Items Selected.Item(ix)) =
False
Next ix

End Sub

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #7
Susan Bricker <sl*****@verizon.net> wrote:
: Just in case anybody else looks at this post...I discovered a slight
: flaw with what was suggested above. When you loop through the selected
: items in the list and use the itemsselected.count as the upper boundary
: of the loop without saving that value initially and using the saved
: value the count reduces with every loop (as you iterate through the
: items). Also, the index value of the selected items changes if you loop
: from 0 up to the total selected count. So ... here is what I used to
: get around the problem (I loop from the last item back up to the first
: item)...

Would looping with a 'for each' statement, where you never
need to specify the array index avoid this problem?
--thelma
: Regards,
: SueB

Nov 13 '05 #8
No, If you loop through a collection removing items from teh collection you
should always loop downwards as Susan has discovered.

Terry Kreft
Microsoft Access MVP
"Thelma Lubkin" <th****@alpha2.csd.uwm.edu> wrote in message
news:dc**********@uwm.edu...
Susan Bricker <sl*****@verizon.net> wrote:
: Just in case anybody else looks at this post...I discovered a slight
: flaw with what was suggested above. When you loop through the selected
: items in the list and use the itemsselected.count as the upper boundary
: of the loop without saving that value initially and using the saved
: value the count reduces with every loop (as you iterate through the
: items). Also, the index value of the selected items changes if you loop
: from 0 up to the total selected count. So ... here is what I used to
: get around the problem (I loop from the last item back up to the first
: item)...

Would looping with a 'for each' statement, where you never
need to specify the array index avoid this problem?
--thelma
: Regards,
: SueB

Nov 13 '05 #9
Or,

with yourcollection
do while .count>0
.remove 0
loop
end with

Terry Kreft wrote:
No, If you loop through a collection removing items from teh collection you
should always loop downwards as Susan has discovered.


Nov 13 '05 #10

Susan -

Whoops! Sorry, I missed that. My bad.

Definitely, looping backwards is the right way to do it. I do not think,
however, you have to store the initial count in a variable.

I believe Access evaluates the loop expression only once, so even though the
count is changing, the loop statement is set in its path. You can prove this
with the following code:
lngCount = 1
For lngLoop = lngCount To 5
lngCount = 5
Debug.Print lngLoop
Next lngLoop

The loop will still have 5 iterations. Even though lngCount gets changed
inside the loop, Access has already evaluated the expression and begun the
loop. So, your code could be rewritten as follows:
Public Sub clrSelectedDates()
Dim ix As Long

'Loop from the end to the beginning of the selected
For ix = (Me!lstProcesseddt.ItemsSelected.Count - 1) To 0 Step -1
Me!lstProcesseddt.Selected(Me!lstProcesseddt.Items Selected.Item(ix))
= False
Next ix
End Sub

Note that if you were using a DO WHILE or DO...UNTIL loop, the expression
would get evaluated every time, but FOR...NEXT loops are only evaluated once.

Peter Schroeder

Susan Bricker wrote:
Just in case anybody else looks at this post...I discovered a slight
flaw with what was suggested above. When you loop through the selected
items in the list and use the itemsselected.count as the upper boundary
of the loop without saving that value initially and using the saved
value the count reduces with every loop (as you iterate through the
items). Also, the index value of the selected items changes if you loop
from 0 up to the total selected count. So ... here is what I used to
get around the problem (I loop from the last item back up to the first
item)...

Public Sub clrSelectedDates()

Dim ix As Long
Dim lngCount As Long

'Save the selected count
lngCount = Me!lstProcesseddt.ItemsSelected.Count - 1

'Loop from the end to the beginning of the selected
For ix = lngCount To 0 Step -1

Me!lstProcesseddt.Selected(Me!lstProcesseddt.Item sSelected.Item(ix)) =
False
Next ix

End Sub

Regards,
SueB

--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/For...ccess/200507/1
Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc | last post by:
Hi all, I can't figure this out. I want the ability to delete an item in a listbox and leave the window area blank, like it is when the listbox is first created and nothing selected. Right...
3
by: darren | last post by:
How, using asp.net (vb.net), can I remove multiple selected items from a listbox. Everything I try does not seem to work. Thanks in advance.
1
by: Jason P Opdycke [MSFT] | last post by:
Hello All - I have 2 list boxes. Items in Left list box populated from a DB. I remove an item from the left box and add an item to the right box to allow user selection. When that item is...
5
by: Joe Fallon | last post by:
I have a list box with 7 text values in it. I have a pair of buttons to Move Up or Move Down the selected item one position. What is the simplest way to code the buttons so the item moves one...
3
by: manning_news | last post by:
Using 2003. How do I tell if a selected item on a multi-select listbox is now unselected? If a user unselects an item, I want to prompt them that they've unselected it. No prompt if they're...
4
by: teo | last post by:
I have a LISTBOX how to insert VBTab or Blank spaces ? in Example Listbox1.Items.Add ("Hallo" & vbTab & "GoodMorning") or Listbox1.Items.Add ("Hallo" & " " & "GoodMorning") ...
6
by: jesse07 | last post by:
I have 6 seperate listbox and I want the user to select 1 value from 1 of the 6 boxes. The problem is after the user clicks the listbox1 then listbox2, listbox1 still says highlighted. I want to...
5
by: =?Utf-8?B?QmVu?= | last post by:
How would you add an item with its DisplayMember and ValueMember without binding to a datatable or dataview like: listbox.DisplayMember = "name"; listbox.ValueMember = "recid"; listbox.DataSource...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.