Reputation: 13
For example i have these values stored in the listbox.
int pid = (int)reader["pid"];
string FirstName = (string)reader["Fname"];
string LastName = (string)reader["Lname"];
string gender = (string)reader["gender"];
string address = (string)reader["address"];
string email = (string)reader["email"];
string item = string.Format("{0} - {1} - {2} -{3} - {4} - {5}" pid,FirstName, LastName,gender,address,email);
this.listBox1.Items.Add(item);
How do i pass these items to another class method ? Thanks
Upvotes: 1
Views: 3743
Reputation: 53
You can pass it like that:
public void YourMethod(ListBox.ObjectCollection items)
using:
YourMethod(listBox1.Items);
UPD: I created a windows form with empty listbox and 1 button. Clicking on button fills the listbox. Form1 class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
var itemGenerator = new ItemGenerator();
itemGenerator.AddItems(listBox1);
}
}
ItemGenerator class:
public class ItemGenerator
{
private readonly string[] items = {"item1", "item2", "item3"};
public void AddItems(ListBox listBox)
{
foreach (var item in items)
{
listBox.Items.Add(item);
}
}
}
ItemGenerator has method AddItems
which has ListBox in parameter. This method is called in Form1 class, so you can directly give a ListBox to processing.
Upvotes: 1