Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to loop throught Listbox

I use this code, but its not working.
Any idea?
For Each item As String In lstmnth.SelectedItem.Value
       MessageBox.Show(item.ToString())
Next
Posted
Updated 27-Jan-11 23:46pm
v4

First of all, you should be looping through lstmnth.Items and not lstmnth.SelectedItem.Value as it has only one value.

Second, MessageBox does not work in ASP.NET, its as Windows Form thingie.
 
Share this answer
 
v2
Comments
soni uma 28-Jan-11 5:52am    
It is vb.net codding. And u can get MessageBox in vb.net
Pravin Patil, Mumbai 28-Jan-11 5:57am    
Hi Soni, The question has been tagged under ASP.NET and it is not possible to have messagebox in ASP.NET no matter which language it is using C#.Net or VB.Net.
Sandeep Mewara 28-Jan-11 5:59am    
Yet, it's a Winforms thing and not a ASP.NET application. Surely wrongly tagged question.
Pravin Patil, Mumbai 28-Jan-11 10:07am    
yeah....it should have been tagged under VB.NET.
Pravin Patil, Mumbai 28-Jan-11 5:57am    
Very good explanation manas.
Well, here is C# Snippet for you. Hope you can convert it into VB.NET

C#
foreach (ListItem s in listControl.Items)
     {
         Response.Write(s.Value);
     }

Hope this will help you !
 
Share this answer
 
Comments
Abhijit Jana 28-Jan-11 6:03am    
Sorry. Floks.. I have answered the same things. Page was open for me a long time after that I have answered !!
Sergey Alexandrovich Kryukov 28-Jan-11 9:45am    
Nevertheless, a 5. I'm not sure writing response here is needed, does not matter -- just for example.
--SA
Thanks For replay

problem is solved

For i As Integer = 0 To lstmnth.Items.Count - 1
           If lstmnth.Items(i).Selected Then
              MessageBox.Show(lstmnth.Items(i).Value.ToString())
           End If
Next
 
Share this answer
 
Comments
Pravin Patil, Mumbai 28-Jan-11 10:11am    
Congrats.........
Do following way
Dim CountyItems As ListItem
For Each CountyItems In LstCounty.Items
      MessageBox.Show(CountyItems.Value.ToString())
Next
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jan-11 9:44am    
Why are you still showing this message box?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900