Set Listbox Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Hart

    Set Listbox Value

    How can I set the listbox value and retrieve a listbox value with just text?
    I have filled the listbox with items in the items collection property (very
    simple, just one column of text items).

    I will have the text from a database and I know it is in the list. How can I
    set it and retrieve it?

    Do I need to loop the listbox to find matching text and set that
    selectedindex to true.

    Any sample code would be appreciated to get the text and set the text.


  • Teme64

    #2
    Re: Set Listbox Value

    Derek Hart wrote:
    How can I set the listbox value and retrieve a listbox value with just text?
    I have filled the listbox with items in the items collection property (very
    simple, just one column of text items).
    >
    I will have the text from a database and I know it is in the list. How can I
    set it and retrieve it?
    >
    Do I need to loop the listbox to find matching text and set that
    selectedindex to true.
    >
    Any sample code would be appreciated to get the text and set the text.
    >
    >
    ListBox has FindString and FindStringExact methods.
    Here's a sample to get started:

    Private Sub Button1_Click(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button1.Click
    ListBox1.Items. Clear()
    ListBox1.Items. Add("Banana")
    ListBox1.Items. Add("is a")
    ListBox1.Items. Add("fruit")
    ListBox1.Items. Add("but")
    ListBox1.Items. Add("monkey")
    ListBox1.Items. Add("is an")
    ListBox1.Items. Add("animal")
    End Sub

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
    Dim ThisIndex As Integer
    ThisIndex = ListBox1.FindSt ringExact("monk ey")
    If ThisIndex >= 0 Then
    MessageBox.Show (ListBox1.Items (ThisIndex).ToS tring, _
    "ListBox", _
    MessageBoxButto ns.OK, _
    MessageBoxIcon. Information)
    ListBox1.Select edIndex = ThisIndex
    ListBox1.Items( ThisIndex) = "donkey"
    End If
    End Sub


    --

    Teme64 @ http://windevblog.blogspot.com

    Comment

    • Jack Jackson

      #3
      Re: Set Listbox Value

      On Tue, 14 Oct 2008 15:50:14 -0700, "Derek Hart"
      <derekmhart@yah oo.comwrote:
      >How can I set the listbox value and retrieve a listbox value with just text?
      >I have filled the listbox with items in the items collection property (very
      >simple, just one column of text items).
      >
      >I will have the text from a database and I know it is in the list. How can I
      >set it and retrieve it?
      >
      >Do I need to loop the listbox to find matching text and set that
      >selectedinde x to true.
      >
      >Any sample code would be appreciated to get the text and set the text.
      >
      Listbox has a SelectedValue property. I don't know how it behaves if
      the Listbox allows multiple items to be selected.

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: Set Listbox Value

        Derek,

        In the same way as a Combobox

        Create a collection by instance a datatable which is the most simple to use
        with a listbox

        Set that as the DataSource
        Set one column to the displaymember
        Set another column to the valuemember

        You are ready

        Cor

        "Derek Hart" <derekmhart@yah oo.comwrote in message
        news:uiKy88kLJH A.5232@TK2MSFTN GP05.phx.gbl...
        How can I set the listbox value and retrieve a listbox value with just
        text? I have filled the listbox with items in the items collection
        property (very simple, just one column of text items).
        >
        I will have the text from a database and I know it is in the list. How can
        I set it and retrieve it?
        >
        Do I need to loop the listbox to find matching text and set that
        selectedindex to true.
        >
        Any sample code would be appreciated to get the text and set the text.
        >

        Comment

        • Jeff Johnson

          #5
          Re: Set Listbox Value

          "Derek Hart" <derekmhart@yah oo.comwrote in message
          news:uiKy88kLJH A.5232@TK2MSFTN GP05.phx.gbl...
          How can I set the listbox value and retrieve a listbox value with just
          text? I have filled the listbox with items in the items collection
          property (very simple, just one column of text items).
          >
          I will have the text from a database and I know it is in the list. How can
          I set it and retrieve it?
          >
          Do I need to loop the listbox to find matching text and set that
          selectedindex to true.
          >
          Any sample code would be appreciated to get the text and set the text.
          Did

          myListBox.Text = someTextIGotFro mMyDatabase

          not work for you?


          Comment

          Working...