Set Listbox Value

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

    #1

    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

    • breitak67

      #3
      Re: Set Listbox Value


      You're gonna laugh. It's this easy:

      namespace ListBoxDemo
      {
      partial class Form1
      {
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.Componen tModel.IContain er components = null;
      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      /// <param name=\"disposin g\">true if managed resources should be
      disposed; otherwise, false.</param>
      protected override void Dispose(bool disposing)
      {
      if (disposing && (components != null))
      {
      components.Disp ose();
      }
      base.Dispose(di sposing);
      }
      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeCompo nent()
      {
      this.btnSetValu e = new System.Windows. Forms.Button();
      this.btnGetValu e = new System.Windows. Forms.Button();
      this.tbValue = new System.Windows. Forms.TextBox() ;
      this.lbMyListBo x = new System.Windows. Forms.ListBox() ;
      this.SuspendLay out();
      //
      // btnSetValue
      //
      this.btnSetValu e.Location = new System.Drawing. Point(199, 67);
      this.btnSetValu e.Name = \"btnSetValue\" ;
      this.btnSetValu e.Size = new System.Drawing. Size(75, 23);
      this.btnSetValu e.TabIndex = 8;
      this.btnSetValu e.Text = \"Set Value\";
      this.btnSetValu e.UseVisualStyl eBackColor = true;
      this.btnSetValu e.Click += new
      System.EventHan dler(this.btnSe tValue_Click);
      //
      // btnGetValue
      //
      this.btnGetValu e.Location = new System.Drawing. Point(199, 12);
      this.btnGetValu e.Name = \"btnGetValue\" ;
      this.btnGetValu e.Size = new System.Drawing. Size(75, 23);
      this.btnGetValu e.TabIndex = 7;
      this.btnGetValu e.Text = \"Get Value\";
      this.btnGetValu e.UseVisualStyl eBackColor = true;
      //
      // tbValue
      //
      this.tbValue.Lo cation = new System.Drawing. Point(190, 41);
      this.tbValue.Na me = \"tbValue\";
      this.tbValue.Si ze = new System.Drawing. Size(100, 20);
      this.tbValue.Ta bIndex = 6;
      //
      // lbMyListBox
      //
      this.lbMyListBo x.FormattingEna bled = true;
      this.lbMyListBo x.Items.AddRang e(new object[] {
      \"Alpha\",
      \"Beta\",
      \"Delta\",
      \"Gamma\"});
      this.lbMyListBo x.Location = new System.Drawing. Point(12, 12);
      this.lbMyListBo x.Name = \"lbMyListBox\" ;
      this.lbMyListBo x.Size = new System.Drawing. Size(120, 95);
      this.lbMyListBo x.TabIndex = 5;
      this.lbMyListBo x.Click += new
      System.EventHan dler(this.lbMyL istBox_Click);
      //
      // Form1
      //
      this.AutoScaleD imensions = new System.Drawing. SizeF(6F, 13F);
      this.AutoScaleM ode = System.Windows. Forms.AutoScale Mode.Font;
      this.ClientSize = new System.Drawing. Size(365, 264);
      this.Controls.A dd(this.btnSetV alue);
      this.Controls.A dd(this.btnGetV alue);
      this.Controls.A dd(this.tbValue );
      this.Controls.A dd(this.lbMyLis tBox);
      this.Name = \"Form1\";
      this.Text = \"Form1\";
      this.ResumeLayo ut(false);
      this.PerformLay out();
      }
      #endregion
      private System.Windows. Forms.Button btnSetValue;
      private System.Windows. Forms.Button btnGetValue;
      private System.Windows. Forms.TextBox tbValue;
      private System.Windows. Forms.ListBox lbMyListBox;
      }
      }

      using System;
      using System.Collecti ons.Generic;
      using System.Componen tModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows. Forms;
      namespace ListBoxDemo
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeCompo nent();
      }
      private void lbMyListBox_Cli ck(object sender, EventArgs e)
      {
      this.tbValue.Te xt = this.lbMyListBo x.SelectedItem. ToString();
      }
      private void btnSetValue_Cli ck(object sender, EventArgs e)
      {
      this.lbMyListBo x.SelectedItem = this.tbValue.Te xt;
      }
      }
      }


      --
      breitak67

      Comment

      • breitak67

        #4
        Re: Set Listbox Value


        In a web app the ListBox will be populated with ListItem objects. The
        Items property of the listbox has a "FindByTest " method that returns a
        ListItem object that you can set the .SelectedItem property to.


        --
        breitak67

        Comment

        • Jack Jackson

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

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

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