![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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. |
My System Specs![]() |
| | #2 (permalink) |
| Vista Business x64 | Re: Set Listbox Value You're gonna laugh. It's this easy: namespace ListBoxDemo { partialclassForm1 { ///<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> protectedoverridevoid 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> privatevoid 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(newobject[] { "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 { publicpartialclassForm1 : Form { public Form1() { InitializeComponent(); } privatevoid lbMyListBox_Click(object sender, EventArgs e) { this.tbValue.Text = this.lbMyListBox.SelectedItem.ToString(); } privatevoid btnSetValue_Click(object sender, EventArgs e) { this.lbMyListBox.SelectedItem = this.tbValue.Text; } } } |
My System Specs![]() |
| | #3 (permalink) |
| Vista Business x64 | 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. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Set Listbox Value On Tue, 14 Oct 2008 15:50:14 -0700, "Derek Hart" <derekmhart@xxxxxx> 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. > the Listbox allows multiple items to be selected. |
My System Specs![]() |
| | #5 (permalink) |
| | 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@xxxxxx> wrote in message news:uiKy88kLJHA.5232@xxxxxx 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. > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Set Listbox Value "Derek Hart" <derekmhart@xxxxxx> wrote in message news:uiKy88kLJHA.5232@xxxxxx 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. myListBox.Text = someTextIGotFromMyDatabase not work for you? |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| ListBox vs. ListView | .NET General | |||
| Easy to get handle from listbox. How to get listbox from handle? | .NET General | |||
| Listbox | PowerShell | |||