Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Count selected items from listbox?

Author  Topic 

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-06-14 : 17:13:43
Anyone know what syntax to count the number of selected items from a listbox?

This is what I have so far but it counts all items from the listbox and what I want is just to count whatever is selected.


Dim D As Integer
For D = 0 To lstTerritory.Items.Count - 1
strFilter += " and territory = '" & territoryFilter & "'"
Next

jhermiz

3564 Posts

Posted - 2005-06-14 : 17:27:32
If Me.lstTerritory.Items(D).Selected = True Then
...do it
else
dont do it
end if



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-06-14 : 17:54:44
Thanks for the response, but what about this part?


For D = 0 To lstTerritory.Items.Count - 1


How would I set that so that it counts the selected items? For example it counts 15 items from the listbox, but i select 3. I want it to only loop 3 times and not 15 times. At the moment I can't seem to get it to count what is being selected.

Probably need another loop to go in and count how many items are selected?

Any ideas?
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-14 : 18:07:39
Cant do that you can only loop and check if the item is selected or not, if it is do something else do something else.
There is no selected count property..so just write the complete loop:

For i = 0 to lstBox.Items.Count-1
if lstBox.Items(i).Selected = true then
'do something
else
'do something else
end if
next i



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-06-15 : 09:40:32
Oh I didn't know that. But yes that works. Thanks.

With the syntax, doesn't it suppose to increment to the next selected item within the listbox?

For example:

Apple
Banana
Pears
Strawberry


If i select Banana and Strawberry, shouldn't it loop and then increment from banana to strawberry? At the moment it just increment with the first on the list giving me "Banana" twice. Probably I need a syntax to have it move down the listbox?



Dim D As Integer
For D = 0 To Me.dlTerritory.Items.Count - 1
'strFilter += " and territory = '" & territoryFilter & "'"
If Me.dlTerritory.Items(D).Selected = True Then
strFilter += " and territory = '" & territoryFilter & "'"
End If
Next D



Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-15 : 10:51:05
Use the SelectedItems() property to return the count and/or all items that are selected in a listbox.

It helps quite a bit to hit F1 now and then to read the documentation.


- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-15 : 11:49:02
The D in the code does move the item down. Its an index in the listbox..not sure what you are having problems with, I dragged and dropped a list box...put that code in, clicked a button...and bam it response.writed all the entries that were selected.



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-06-15 : 14:39:17
That's what I thought too, but it does not to want to increment to the next selected item. I will play around with it some more and see what's going on.
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-15 : 14:48:57
ok, if you are desperate post back and ill whip up the example and post it on our web server and allow u to see it with the code.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-15 : 15:27:39


You could try something like crazy like using the SelectedItems() collection, since that's what it is there for ... though I do recognize that it might seem too simple, so like many programmers you might think it can't possibly be a good way to do it .

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-15 : 15:52:48
quote:
Originally posted by jsmith8858



You could try something like crazy like using the SelectedItems() collection, since that's what it is there for ... though I do recognize that it might seem too simple, so like many programmers you might think it can't possibly be a good way to do it .

- Jeff



Chris take your time and do not worry about posts like this...if you search the forum you will see a lot of posts by jeff that belittle others...as I stated if you are having trouble just ask, that is why we are here.

Jeff,

It would be too simple to recognize that the SelectedItems collection does not exist for asp.net, it works for windows applications such as winforms in C# or VB.net but not in asp.net, but apparently we are too stupid to realize that...



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-06-15 : 16:00:04
You're absolutely right, I didn't realize he was using ASP.NET -- 100% my fault.

Jon -- you're right, I'm not too helpful around here, I mostly belittle people and rarely make an actual effort to answer questions and help people out. I should be more diplomatic and sensitive to others and more tactful like you are.

I'll have to work on it.

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-15 : 16:02:19
Chris I have created a demo for you zipped here:

http://www.jakrauseinc.com/jhermiz/WebApplication1222.zip

Basically the code is this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Me.ListBox1.Items.Add("blah1")
Me.ListBox1.Items.Add("blah2")
Me.ListBox1.Items.Add("blah3")
Response.Write(Me.ListBox1.Items.Count)
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To Me.ListBox1.Items.Count - 1
If Me.ListBox1.Items(i).Selected = True Then
Response.Write(Me.ListBox1.Items(i).Text)
Else
Response.Write(Me.ListBox1.Items(i).Text & " was not selected")
End If
Next
End Sub





Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-06-15 : 16:03:02
quote:
Originally posted by jsmith8858

You're absolutely right, I didn't realize he was using ASP.NET -- 100% my fault.

Jon -- you're right, I'm not too helpful around here, I mostly belittle people and rarely make an actual effort to answer questions and help people out. I should be more diplomatic and sensitive to others and more tactful like you are.

I'll have to work on it.

- Jeff



Is there an image where I can roll my eyes ?




Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

chriskhan2000
Aged Yak Warrior

544 Posts

Posted - 2005-06-22 : 13:01:15
Thanks for understanding. It helps when someone with more knowledge can show you in a couple of minutes than to pounder for days trying to figure what to do.

I had benefited a lot from this forum and appreciate all the helps you all gave. I know that sometimes I asked too much questions or not giving enough info, so please bare with me. Coming from a PC Technician and Network Admin to a programmer is a totally new ball game for me. I used to code in COBOL/Oracle so that's why I decided to pick up programming again.



Chris
Go to Top of Page
   

- Advertisement -