Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. ListBox Question

ListBox Question

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    waner michaud
    wrote on last edited by
    #1

    Hi everyone, I have a listbox derived from a table with the following record: Listbox_answer_item Bleu Red Black this is from an answer table base on a particular question. since a question can have more than one asnwer, I like to number the response to a question I rather show the items in the list box in sequential manner with a numbering system. For example: Listbox_answer_item 1. Bleu (first answer) 2. Red (second answer) 3. Black (third answer) 4. etc.. this number should be a position number..which answer that enters first and so forth.. Thanks in advance!

    C A 2 Replies Last reply
    0
    • W waner michaud

      Hi everyone, I have a listbox derived from a table with the following record: Listbox_answer_item Bleu Red Black this is from an answer table base on a particular question. since a question can have more than one asnwer, I like to number the response to a question I rather show the items in the list box in sequential manner with a numbering system. For example: Listbox_answer_item 1. Bleu (first answer) 2. Red (second answer) 3. Black (third answer) 4. etc.. this number should be a position number..which answer that enters first and so forth.. Thanks in advance!

      C Offline
      C Offline
      Christopher Mars
      wrote on last edited by
      #2

      I think this is what you are asking for VB.Net

      Dim answers() As String = { "Bleu", "Red", "Black" }

      For I As Integer = 0 To answers.Length - 1
       ListBox1.Items.Add((I + 1).ToString() + ". " + answers(I))
      Next

      VB6

      Dim I As Integer
      Dim answers(2) As String

      answers(0) = "Bleu"
      answers(1) = "Red"
      answers(2) = "Black"

      For I = 0 To UBound(answers)
       List1.AddItem CStr(I + 1) + ". " + answers(I)
      Next I

      To add more answers to the listbox, just add more elements to the array.

      W 1 Reply Last reply
      0
      • C Christopher Mars

        I think this is what you are asking for VB.Net

        Dim answers() As String = { "Bleu", "Red", "Black" }

        For I As Integer = 0 To answers.Length - 1
         ListBox1.Items.Add((I + 1).ToString() + ". " + answers(I))
        Next

        VB6

        Dim I As Integer
        Dim answers(2) As String

        answers(0) = "Bleu"
        answers(1) = "Red"
        answers(2) = "Black"

        For I = 0 To UBound(answers)
         List1.AddItem CStr(I + 1) + ". " + answers(I)
        Next I

        To add more answers to the listbox, just add more elements to the array.

        W Offline
        W Offline
        waner michaud
        wrote on last edited by
        #3

        Hi, Thanks for replying. these answers are from a Question_Answer table relationship. The listbox is binding with Qestion_Answer table based on Question_PK and Answer_PK. so the answer is not hardcoded. I want to enumarate the answers on the listbox. Thanks again. PS. vb.net and sql

        1 Reply Last reply
        0
        • W waner michaud

          Hi everyone, I have a listbox derived from a table with the following record: Listbox_answer_item Bleu Red Black this is from an answer table base on a particular question. since a question can have more than one asnwer, I like to number the response to a question I rather show the items in the list box in sequential manner with a numbering system. For example: Listbox_answer_item 1. Bleu (first answer) 2. Red (second answer) 3. Black (third answer) 4. etc.. this number should be a position number..which answer that enters first and so forth.. Thanks in advance!

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          You should be able to use the ListBox Format event to solve this problem. Some rough and ready code showing the general idea

          Private data As String() = {"One", "Two", "Three", "Four"}

          Public Sub New()
          InitializeComponent()
          addhandler listBox1.format, addressof listBox1_Format
          listBox1.DataSource = data
          End Sub

          Private Sub listBox1_Format(sender As Object, e As ListControlConvertEventArgs)
          Dim list As IList = DirectCast(DirectCast(sender, ListBox).DataSource, IList)
          Dim idx As Int32 = list.IndexOf(e.ListItem)
          ' format as "1. One", "2. Two" etc
          e.Value = String.Format("{0}. {1}", idx + 1, e.ListItem)
          End Sub

          Alan.

          modified on Friday, June 17, 2011 10:37 AM

          S 1 Reply Last reply
          0
          • A Alan N

            You should be able to use the ListBox Format event to solve this problem. Some rough and ready code showing the general idea

            Private data As String() = {"One", "Two", "Three", "Four"}

            Public Sub New()
            InitializeComponent()
            addhandler listBox1.format, addressof listBox1_Format
            listBox1.DataSource = data
            End Sub

            Private Sub listBox1_Format(sender As Object, e As ListControlConvertEventArgs)
            Dim list As IList = DirectCast(DirectCast(sender, ListBox).DataSource, IList)
            Dim idx As Int32 = list.IndexOf(e.ListItem)
            ' format as "1. One", "2. Two" etc
            e.Value = String.Format("{0}. {1}", idx + 1, e.ListItem)
            End Sub

            Alan.

            modified on Friday, June 17, 2011 10:37 AM

            S Offline
            S Offline
            Simon_Whale
            wrote on last edited by
            #5

            you've got some c# in your text there

            listBox1.Format += New ListControlConvertEventHandler(listBox1_Format)

            should be

            addhandler listbox1.format, addressof listbox1_format

            Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

            A 1 Reply Last reply
            0
            • S Simon_Whale

              you've got some c# in your text there

              listBox1.Format += New ListControlConvertEventHandler(listBox1_Format)

              should be

              addhandler listbox1.format, addressof listbox1_format

              Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              Good catch, I'd put C# source through a code converter and must have copied that line in separately. Alan.

              S 1 Reply Last reply
              0
              • A Alan N

                Good catch, I'd put C# source through a code converter and must have copied that line in separately. Alan.

                S Offline
                S Offline
                Simon_Whale
                wrote on last edited by
                #7

                No the converters don't handle the event handlers properly on conversion.

                Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups