Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 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
 
Old October 4th, 2007, 11:52 AM
Authorized User
 
Join Date: Sep 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default unselecting items in a listbox

I've created a listbox that loads districts using a dataAdapter on page load. This works fine. I've also added an event for this listbox (selectedIndexChange). The following is the code behind when this event is fired.

  Dim x As Integer
      Dim distsel As Integer = 0
      Dim dstval As String
      For x = 0 To lstDistrict.Items.Count - 1
         If lstDistrict.Items(x).Selected = True Then
            dstval = dstval + Convert.ToString(lstDistrict.Items
               (x).Value)
            distsel = distsel + 1
         End If
      Next


So when a user selects a district from the listbox, some action takes place and the selected item is highlighted within the listbox. Is there an event that I can run that will unselect an item in a listbox that has already been selected? Obviously when I try to select an item from the listbox that has already been selected the (selectedIndexChange) event doesn't fire. Can I use the databinding event to see what has been selected/not selected?

Any ideas or leads would be greatly appreciated.


 
Old October 4th, 2007, 12:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

unfortunately you will have to do the same procedure you are doing but changing the selected value to false...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old October 4th, 2007, 12:28 PM
Authorized User
 
Join Date: Sep 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I understand that I can us the same techniques to make an item selected or not selected by changing the selected properties equal to false, but the problem I'm having is that there is no event fired when I select an item from the listbox that has already been selected. So I'm not able to set the selected item properties equal to false. Is there a different event to do this?

 
Old October 4th, 2007, 12:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Nop.. you have to use the same event.. you want to deselect something that is selected?? why not use a listcheckedbox for that???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old October 4th, 2007, 04:51 PM
Authorized User
 
Join Date: Sep 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The requirements call for a listbox, however you are probably right on the lines of using a CheckboxList (if this is not what you are referring to..my apologies and please explain because I don't see any option for a listcheckedbox in my IDE). Can this be done using a listbox? What I'm trying to understand is after a selection is made in a listbox and the selectedIndexChange event is fired, how can you call the same event (selectedIndexChange) that was just used to select an item, to deselect that same item when there is no selectedIndexChange occurring? Do you have an example that you can post, based on my original code.

 
Old October 5th, 2007, 07:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

you are right and I'm Wrong.. The control I was referring is the one you are saying (Checkboxlist). And also the event doesn't fire when you deselect.. Maybe you can do the trick using another events??? maybe a mouseclickevent?? (but you will need to find where the user click, and that could be really tricky...)

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old October 5th, 2007, 02:04 PM
Authorized User
 
Join Date: Sep 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The only "RIGHT" is that you are willing to help guys like me...thank you. You've actually given me some other alternatives to look at. I will research and give solution...hopefully soon.:D

 
Old October 5th, 2007, 02:24 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmmmm. If I understand you correctly you only want the user to be able to select a value from the listbox only once, is that correct? If this is correct you have a couple of options:

I might rewrite your original code something like:

Code:
Dim item as New ListItem
Dim dstval As String = ""
Dim distsel as Integer = 0

For Each item in lstDistrict.Items
  If item.Selected Then
     dstval += item.Value
     lstDistrict.Items.Remove(item)
  End If
Next
This will allow you to keep a distinct list of items the User has NOT selected. Alternatively you could put another listbox on your form and just move an item from your source list box to a temporary list box.

Code:
Dim item as New ListItem
Dim dstval As String = ""
Dim distsel as Integer = 0

For Each item in lstDistrict.Items
  If item.Selected Then
     dstval += item.Value
     lstDistrictNew.Items.Add(item)
     lstDistrict.Items.Remove(item)
  End If
Next
and you would do the reverse if you wanted to move an item back to the source list box:

Code:
...
     lstDistrict.Items.Add(item)
     lstDistrictNew.Items.Remove(item)
I didn't test any of this but it should work for you without much problem.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old October 8th, 2007, 02:46 PM
Authorized User
 
Join Date: Sep 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thank you for your quick response...

I'm not sure if I've been absolutely clear about what I'm trying to do, if not, I apologize.

I have a listbox that can except multiple values. When a user selects a item in the listbox it is highlighted and the selectedindexchange event is fired. Within this event I have code that is building a string of value(s) (based on user selection) that i use later in the application (see code below)

       Dim x As Integer
       Dim distsel As Integer = 0
       Dim dstval As String
       For x = 0 To lstDistrict.Items.Count - 1
          If lstDistrict.Items(x).Selected = True Then
             dstval = dstval + Convert.ToString(lstDistrict.Items(x).Value)
             distsel = distsel + 1
          End If
       Next

So dstval will contain the corresponding values from what was selected in the listbox. The values in dstval could contain the following data: '021025027', three sets of values that the user has selected. My stored procedure will manipulate and return the correct data based on these selections. All of this works fine. The problem occurs when a users wants to deselect an item that was just selected; the selectedindexchange does not fire and the item that the user has tried to deselect is still highlighted, so it appears to still be selected. So basically, I'm trying to develop code that will handle instances when a user selects and deselects an item in the listbox. An even more simple explanation is if a user selects an item in the listbox it is highlighted but when a user selects the same item that was just selected, no event fires and the item is still selected (highlighted). I would like to write code to deselect that item.

Any help on this matter would be greatly appreciated.


 
Old October 8th, 2007, 03:01 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmm. Sounds like you are going to have to deal with the MouseEvents of the ListBox.

Here is an example:
http://www.vb-helper.com/howto_click...ist_clear.html

And a list of all the MouseEvents for a ListBox can be found here:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.listbox_members(VS.71).aspx

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Beginning Visual C# 2008
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========





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
Saving Listbox Items antonducabre VB.NET 2002/2003 Basics 1 July 16th, 2003 12:46 PM





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