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