Selection of items in list box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maliksleo
    New Member
    • Feb 2009
    • 115

    Selection of items in list box

    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
  • bhupinder
    New Member
    • Feb 2009
    • 32

    #2
    Try code
    Code:
     <asp:listbox id="list1" runat="server" selectionmode="multiple">
    
          <asp:listitem>Madrid</asp:listitem>
    
          <asp:listitem>Oslo</asp:listitem>
    
          <asp:listitem>Lisbon</asp:listitem>
    
        </asp:listbox>
    VB Code

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
    System.EventArgs) Handles Button1.Click
        Dim i As Integer
        TextBox1.Text = "You Selected" & " "
        For i = 0 To ListBox1.Items.Count - 1
            If ListBox1.Items(i).Selected Then
                TextBox1.Text = TextBox1.Text & ListBox1.Items(i).Text & " "
            End If
        Next
    End Sub
    Last edited by Frinavale; Mar 20 '09, 03:44 PM. Reason: Added [code] tags. Please post code in [code] [/code] tags.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Could you please elaborate on what you mean when you say:
      Originally posted by maliksleo
      i want to select 3 values in it through VB code
      Do you only want 3 values to appear in the ListBox?
      How are you adding items to your ListBox?

      -Frinny

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        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 SelectedIndices Changed event, and compare to the number of items already selected.

        Comment

        • maliksleo
          New Member
          • Feb 2009
          • 115

          #5
          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?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            You need to implement a method that handles the ListBox's SelectedIndexCh anged event. Do your comparison in this method.

            Comment

            • maliksleo
              New Member
              • Feb 2009
              • 115

              #7
              i got the point thanks alot for your help
              thanks
              maliksleo

              Comment

              • maliksleo
                New Member
                • Feb 2009
                • 115

                #8
                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

                Comment

                • maliksleo
                  New Member
                  • Feb 2009
                  • 115

                  #9
                  Code:
                  Dim lang As Array, i
                          lang = Split(language, ",")
                          Response.Write(lang(1))
                          For Each i In lang
                              Dim j As Integer
                              For j = 0 To lblangknown.Items.Count - 1
                                  If i = lblangknown.Items(j).Text Then
                                      lblangknown.Items(j).Selected = True
                                  End If
                              Next
                          Next
                  i have problem with this code i m not getting any selected value plz help
                  Last edited by Frinavale; Mar 31 '09, 01:07 PM. Reason: Added [code] tags. Please post code in [code] [/code] tags

                  Comment

                  • maliksleo
                    New Member
                    • Feb 2009
                    • 115

                    #10
                    Solved

                    i found the solution below
                    Code:
                    lblangknown.DataBind()
                            Dim lang As Array, i
                            lang = Split(language, ",")
                            For Each i In lang
                    only u need to bind your data first and then execute the code which i previously posted

                    thanks
                    maliksleo
                    Last edited by Frinavale; Mar 31 '09, 01:07 PM. Reason: Added [code] tags. Please post code in [code] [/code] tags.

                    Comment

                    • NitinSawant
                      Contributor
                      • Oct 2007
                      • 271

                      #11
                      instead of sending the values to server and then manipulating it, you should use client side javascript.

                      Comment

                      Working...