Connecting Tech Pros Worldwide Forums | Help | Site Map

Set Listbox Value

Derek Hart
Guest
 
Posts: n/a
#1: Oct 14 '08
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
Guest
 
Posts: n/a
#2: Oct 15 '08

re: Set Listbox Value


Derek Hart wrote:
Quote:
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(ByVal sender As Object, ByVal e As System.EventArgs) 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ThisIndex As Integer
ThisIndex = ListBox1.FindStringExact("monkey")
If ThisIndex >= 0 Then
MessageBox.Show(ListBox1.Items(ThisIndex).ToString , _
"ListBox", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
ListBox1.SelectedIndex = ThisIndex
ListBox1.Items(ThisIndex) = "donkey"
End If
End Sub


--

Teme64 @ http://windevblog.blogspot.com
breitak67
Guest
 
Posts: n/a
#3: Oct 15 '08

re: Set Listbox Value



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

namespace ListBoxDemo
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name=\"disposing\">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#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 InitializeComponent()
{
this.btnSetValue = new System.Windows.Forms.Button();
this.btnGetValue = new System.Windows.Forms.Button();
this.tbValue = new System.Windows.Forms.TextBox();
this.lbMyListBox = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// btnSetValue
//
this.btnSetValue.Location = new System.Drawing.Point(199, 67);
this.btnSetValue.Name = \"btnSetValue\";
this.btnSetValue.Size = new System.Drawing.Size(75, 23);
this.btnSetValue.TabIndex = 8;
this.btnSetValue.Text = \"Set Value\";
this.btnSetValue.UseVisualStyleBackColor = true;
this.btnSetValue.Click += new
System.EventHandler(this.btnSetValue_Click);
//
// btnGetValue
//
this.btnGetValue.Location = new System.Drawing.Point(199, 12);
this.btnGetValue.Name = \"btnGetValue\";
this.btnGetValue.Size = new System.Drawing.Size(75, 23);
this.btnGetValue.TabIndex = 7;
this.btnGetValue.Text = \"Get Value\";
this.btnGetValue.UseVisualStyleBackColor = true;
//
// tbValue
//
this.tbValue.Location = new System.Drawing.Point(190, 41);
this.tbValue.Name = \"tbValue\";
this.tbValue.Size = new System.Drawing.Size(100, 20);
this.tbValue.TabIndex = 6;
//
// lbMyListBox
//
this.lbMyListBox.FormattingEnabled = true;
this.lbMyListBox.Items.AddRange(new object[] {
\"Alpha\",
\"Beta\",
\"Delta\",
\"Gamma\"});
this.lbMyListBox.Location = new System.Drawing.Point(12, 12);
this.lbMyListBox.Name = \"lbMyListBox\";
this.lbMyListBox.Size = new System.Drawing.Size(120, 95);
this.lbMyListBox.TabIndex = 5;
this.lbMyListBox.Click += new
System.EventHandler(this.lbMyListBox_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(365, 264);
this.Controls.Add(this.btnSetValue);
this.Controls.Add(this.btnGetValue);
this.Controls.Add(this.tbValue);
this.Controls.Add(this.lbMyListBox);
this.Name = \"Form1\";
this.Text = \"Form1\";
this.ResumeLayout(false);
this.PerformLayout();
}
#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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ListBoxDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void lbMyListBox_Click(object sender, EventArgs e)
{
this.tbValue.Text = this.lbMyListBox.SelectedItem.ToString();
}
private void btnSetValue_Click(object sender, EventArgs e)
{
this.lbMyListBox.SelectedItem = this.tbValue.Text;
}
}
}


--
breitak67
breitak67
Guest
 
Posts: n/a
#4: Oct 15 '08

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
Jack Jackson
Guest
 
Posts: n/a
#5: Oct 15 '08

re: Set Listbox Value


On Tue, 14 Oct 2008 15:50:14 -0700, "Derek Hart"
<derekmhart@yahoo.comwrote:
Quote:
>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 a SelectedValue property. I don't know how it behaves if
the Listbox allows multiple items to be selected.
Cor Ligthert[MVP]
Guest
 
Posts: n/a
#6: Oct 15 '08

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@yahoo.comwrote in message
news:uiKy88kLJHA.5232@TK2MSFTNGP05.phx.gbl...
Quote:
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.
>
Jeff Johnson
Guest
 
Posts: n/a
#7: Oct 15 '08

re: Set Listbox Value


"Derek Hart" <derekmhart@yahoo.comwrote in message
news:uiKy88kLJHA.5232@TK2MSFTNGP05.phx.gbl...
Quote:
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 = someTextIGotFromMyDatabase

not work for you?


Closed Thread