473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Selection of items in list box

maliksleo
115 100+
i have a list box now i want to select 3 values in it through VB code

and restrict the user to select onle three values please help me

Regards
Mar 20 '09 #1
10 2738
Try code
Expand|Select|Wrap|Line Numbers
  1.  <asp:listbox id="list1" runat="server" selectionmode="multiple">
  2.  
  3.       <asp:listitem>Madrid</asp:listitem>
  4.  
  5.       <asp:listitem>Oslo</asp:listitem>
  6.  
  7.       <asp:listitem>Lisbon</asp:listitem>
  8.  
  9.     </asp:listbox>
VB Code

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
  2. System.EventArgs) Handles Button1.Click
  3.     Dim i As Integer
  4.     TextBox1.Text = "You Selected" & " "
  5.     For i = 0 To ListBox1.Items.Count - 1
  6.         If ListBox1.Items(i).Selected Then
  7.             TextBox1.Text = TextBox1.Text & ListBox1.Items(i).Text & " "
  8.         End If
  9.     Next
  10. End Sub
Mar 20 '09 #2
Frinavale
9,735 Expert Mod 8TB
Could you please elaborate on what you mean when you say:
@maliksleo
Do you only want 3 values to appear in the ListBox?
How are you adding items to your ListBox?

-Frinny
Mar 20 '09 #3
tlhintoq
3,525 Expert 2GB
I think he's saying he has a listbox with multiple selection, of many items. But wants to restrict it to no more than 3.

Maliksleo: If that sounds right use the SelectedIndicesChanged event, and compare to the number of items already selected.
Mar 20 '09 #4
maliksleo
115 100+
thanks for your consideration

tlhintoq is right i want to restrict the user to select not more than three values

but how can i compare the mentioned items?
Mar 30 '09 #5
Frinavale
9,735 Expert Mod 8TB
You need to implement a method that handles the ListBox's SelectedIndexChanged event. Do your comparison in this method.
Mar 30 '09 #6
maliksleo
115 100+
i got the point thanks alot for your help
thanks
maliksleo
Mar 31 '09 #7
maliksleo
115 100+
now one more thing how i can select different values in listbox through vb code?

values are coming from database like

china,USA,UK etc

now how i can show these values selected in list box?

Thanks in advance
Mar 31 '09 #8
maliksleo
115 100+
Expand|Select|Wrap|Line Numbers
  1. Dim lang As Array, i
  2.         lang = Split(language, ",")
  3.         Response.Write(lang(1))
  4.         For Each i In lang
  5.             Dim j As Integer
  6.             For j = 0 To lblangknown.Items.Count - 1
  7.                 If i = lblangknown.Items(j).Text Then
  8.                     lblangknown.Items(j).Selected = True
  9.                 End If
  10.             Next
  11.         Next
i have problem with this code i m not getting any selected value plz help
Mar 31 '09 #9
maliksleo
115 100+
i found the solution below
Expand|Select|Wrap|Line Numbers
  1. lblangknown.DataBind()
  2.         Dim lang As Array, i
  3.         lang = Split(language, ",")
  4.         For Each i In lang
only u need to bind your data first and then execute the code which i previously posted

thanks
maliksleo
Mar 31 '09 #10
NitinSawant
270 100+
instead of sending the values to server and then manipulating it, you should use client side javascript.
Apr 4 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Felix Natter | last post by:
hi, I have a php-Script which gets passed a Boat-ID and this is used to mark an element in a <select> as the default: <select name="boote" multiple="multiple" size="5"> <option...
2
by: David | last post by:
Hi, Is this possible & How ? We manufacture products. Each product has a list of items which can be packed at our production facility. A Picking List. Certain customers may require certain...
2
by: bienwell | last post by:
Hi all, I've got a problem with getting the data values from multiple selections in the list box. I could only get the value and the index of the first item from the selection, but not the...
10
by: Adam Clauss | last post by:
I have a page containing a list box. This list may contain duplicate items - in which the ORDER is important. ex: a b b a is significant as compared to: b
1
by: Brad Allison | last post by:
This is a newbie question. I have a combo box in where the end user will select an item (an obedience class - yes for dogs, not for developers) and then assign an obedience judge to that class. ...
0
by: Robin Tucker | last post by:
Hi there, I have a list box (just happens to be owner draw). When I select multiple items using the CTRL key, the items I have selected become highlighted (obviously). Now, when I click on one...
0
by: Robin Tucker | last post by:
Apologies for the report, but this problem is doing my head in! : I have a list box (just happens to be owner draw). When I select multiple items using the CTRL key, the items I have selected...
13
by: Bruza | last post by:
I need to implement a "random selection" algorithm which takes a list of as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of...
5
Claus Mygind
by: Claus Mygind | last post by:
I have a list box and want to limit the user to selecting a max of 5 items from the list. I've put in a counter which warns the user that more than 5 items have been selected, however I cannot...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.