Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
| Search | Today's Posts | Mark Forums Read
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 23rd, 2009, 06:11 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default deselecting items in listbox after certain other item choosen.


I have list box on a windows form in windows application app in VB.NET, the list box contains 9 items, the items are populated from a database.
The values selected on the listbox are then used to built up a SQL statement which is then sent to create a report.
My problem is, I want the list box to deselect all other options when one particular one is selected,

Item 1
Item 2
Item 3
Item 4

So Items 2,3 4 can all be selected together, but when Item 1 is selected I want all others to be deselect in the SelectedIndexChanged event.
Any ideas?
__________________
Thank You
  #2 (permalink)  
Old February 23rd, 2009, 07:37 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Sure! Every time you have a selectedindexchanged event raised, Loop through all the collection, and deselect the items you don't want...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #3 (permalink)  
Old March 19th, 2009, 09:11 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

Ok, still have this issue… here is my code:
' i= 0, once this is selected I want all the others to be deselected, but I also want the user to be able to reselect the others aswell without having to manually unselect 0, there are 9 items in the list. I want the users to be able to select multiples only for 1-9, but once zero selected all the others are unselected.



If lstSt.Items.Count >= 0 Then

For i = 0 To lstStore.Items.Count - 1
If lstStore.GetSelected(i) = True And i <> 0 Then

Else

End If

Next i

End If
End if

ANy Ideas?
__________________
Thank You
  #4 (permalink)  
Old March 19th, 2009, 09:41 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

ok, so what's the problem?
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #5 (permalink)  
Old March 19th, 2009, 09:45 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

Ok, so basically when I selected item 0 I want all other to be unselected, but I also want the user to be able to reselt the other other items in case they change their mind.
Item 0 = is basically and group of shops
Items 1-9 = individual

You see what im trying to do, I want the user to selected either Group of shops – v- individual shops
__________________
Thank You
  #6 (permalink)  
Old March 19th, 2009, 10:26 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Yes, but you realized that the user interface is confusing?? checkbox mean to be not exclusive each other (so, item 0 shouldn't be a check box!).
Beside the UI problem, I still don't understand where is your problem?? if the item 0 is checked, deselect all the rest, if the user checks any of the rest, deselect 0.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #7 (permalink)  
Old March 19th, 2009, 10:29 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

Hey

Im using a list box control not a check box.
And yes how you described how is should work is perfect,( if the item 0 is checked, deselect all the rest, if the user checks any of the rest, deselect 0.) im looking for the code to do it. This would be a great help.
__________________
Thank You
  #8 (permalink)  
Old March 19th, 2009, 10:36 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Doesn't every item of the list has a property checked?

like

List.listitem.item(X).checked??

The code you wrote before was a great approach, what was the problem with it?
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #9 (permalink)  
Old March 19th, 2009, 11:14 AM
Authorized User
 
Join Date: Nov 2007
Location: , , Ireland.
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

Ok here is my code below:

situation: when I select any of the items 1-9, its fine, when I selected item 0 all the others are unselected.. this is fine.
PROBLEM: once this happens and item zero is the only one selected, I try to selected an item in 1-9 but it will not allow me until manually unselected item zero.
What I want to happen is that once I reclick item 1 say, that this will allow me and will unselected item zero at same time.
Does this make sense
So basically half of it is working
If lstStore.Items.Count >= 0 Then

For i = 0 To lstStore.Items.Count - 1

If lstStore.GetSelected(i) = TrueAnd i <> 0 Then


Me.lstStore.SetSelected(0, False)
ElseIf lstStore.GetSelected(i) = TrueAnd i = 0 Then
Me.lstStore.ClearSelected()
Me.lstStore.SelectedIndex = 0

EndIf

Next i
__________________
Thank You
  #10 (permalink)  
Old March 19th, 2009, 11:23 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

And now we have a real problem :)

Ok, you are rigth, because you are always looking if the first item is checked.

So a solution could be have and internal boolean variable that work something like this:

At first it false. When you checked 0, convert it to true and deselect all.
when you enter the procedure again, check if the value is true and that 0 item is selected, if that happens, this is a case when the user pressed another checkbox.
if the value is true but item 0 is unchecked, then put value to false and don't do anything else..

Also some notes.

a For that does something different for the first item is useless. Better start the for at 1, and do the work for the first item before the for.
Also
if something = true then...
is the same as
if something then
because something is a boolean by itself ;)
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
The Following User Says Thank You to gbianchi For This Useful Post:
DavidM (April 6th, 2009)




Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting all listbox items Lucy_H85 C# 2005 2 December 1st, 2009 05:36 AM
Moving Items up and down in a listbox Brendan Bartley Access 15 September 18th, 2007 06:42 AM
UNSelect Items in ListBox mmcdonal Access VBA 7 June 21st, 2007 08:09 AM
Unhighlight Listbox Items selevanm Access VBA 1 March 7th, 2007 01:39 PM
One item in Listbox venterjo General .NET 2 February 1st, 2005 05:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.