469,926 Members | 2,124 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

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.
Oct 14 '08 #1
6 3385
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(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
Oct 15 '08 #2

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
Oct 15 '08 #3

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
Oct 15 '08 #4
On Tue, 14 Oct 2008 15:50:14 -0700, "Derek Hart"
<de********@yahoo.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
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.
Oct 15 '08 #5
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" <de********@yahoo.comwrote in message
news:ui**************@TK2MSFTNGP05.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.
Oct 15 '08 #6
"Derek Hart" <de********@yahoo.comwrote in message
news:ui**************@TK2MSFTNGP05.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 = someTextIGotFromMyDatabase

not work for you?
Oct 15 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by gdbjohnson-AT-yahoo-dot-ca-nospamplz | last post: by
4 posts views Thread by collie | last post: by
5 posts views Thread by Matthew Wells | last post: by
2 posts views Thread by rn5a | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.