all groups > vj# > february 2004 >
You're in the

vj#

group:

listbox control


listbox control Ryan Schoolman
2/16/2004 7:51:45 PM
vj#: I'm a noobie
When you populate the items collection visually in a listbox control is
there a way to get the text from all of the items using a method or do you
have to loop through the entire collection to get the whole text? Can you
send me an example?


--
Ryan Schoolman - Programmer & Application Architect
ryan@pclegends.com

PC Legends
http://www.pclegends.com

[w] 715.839.6855
[c] 715.379.0878
[h] 715.855.9003

RE: listbox control skadu.vjcr NO[at]SPAM online.microsoft.com
2/17/2004 8:23:50 AM
Hi Ryan

There is currently no direct API to get the text from all of the items in
one go. You will need to loop through the entire collection to get the
whole text.

You might want to refer to the following link for a detailed documentation
on ListBox APIs
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsLi
stBoxMethodsTopic.asp

Thanks for your feedback.

Satish [MSFT]
--------------------
[quoted text, click to view]
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.
phx.gbl
[quoted text, click to view]


---------------------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
Re: listbox control Lars-Inge Tønnessen
2/17/2004 11:15:06 PM

You have to loop.

Bellow you will find a small app with two listboxes and a button. One with
items and one empty. When you push the button all items in listbox1 will be
copied into listbox2.




import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;
/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button1;

/**
* Required designer variable.
*/
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}

/**
* Clean up any resources being used.
*/
protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}

#region Windows Form Designer generated code
/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/

private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();

//
// listBox1
//
this.listBox1.get_Items().AddRange(new Object[]
{"one", "two", "three", "four"});
this.listBox1.set_Location(new System.Drawing.Point(8, 16));
this.listBox1.set_Name("listBox1");
this.listBox1.set_Size(new System.Drawing.Size(120, 95));
this.listBox1.set_TabIndex(0);

//
// listBox2
//
this.listBox2.set_Location(new System.Drawing.Point(232, 16));
this.listBox2.set_Name("listBox2");
this.listBox2.set_Size(new System.Drawing.Size(120, 95));
this.listBox2.set_TabIndex(1);

//
// button1
//
this.button1.set_Location(new System.Drawing.Point(144, 48));
this.button1.set_Name("button1");
this.button1.set_TabIndex(2);
this.button1.set_Text("button1");
this.button1.add_Click( new
System.EventHandler(this.button1_Click) );

//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(360, 117));
this.get_Controls().AddRange(new System.Windows.Forms.Control[]
{this.button1,this.listBox2,this.listBox1});
this.set_Name("Form1");
this.set_Text("Form1");
this.ResumeLayout(false);
}
#endregion


/**
* The main entry point for the application.
*/
/** @attribute System.STAThread() */
public static void main(String[] args)
{
Application.Run(new Form1());
}


// ___---==== This is where it happens ====---____
private void button1_Click (Object sender, System.EventArgs e)
{
// Return the number of items in the listbox.
int length = this.listBox1.get_Items().get_Count();

// Loop through listbox 1 and copy the items into listbox 2
for ( int counter = 0; counter < length; counter++ )
this.listBox2.get_Items().Add(
this.listBox1.get_Items().get_Item(counter) );
}

// ___---==================================---____

}


Did this answer your question?

--
Regards,
Lars-Inge Tønnessen
http://emailme.larsinge.com
http://www.larsinge.com

AddThis Social Bookmark Button