0

Hi I'm trying to see the elements of an array in a listbox, my array is in a class, but I dont know how to see the elements of the array in a windows form listbox. This is my code:

 NumSepaERG[0] = Convert.ToDouble(columnas[1]);
 ListBox listbox2 = new ListBox();
 listbox2.Items.Add(NumSepaERG[0]);

But I know how view the elements in the listbox.

4
  • Do you want to know how to display the new ListBox in Form? Commented Oct 10, 2012 at 15:10
  • Hi, Mithrandir yes I believe so.. Commented Oct 10, 2012 at 15:11
  • 1
    Each Form has a property named Controls which gives you access to the "list" of it's child controls. Simply add your ListBox to this collection. Commented Oct 10, 2012 at 15:12
  • this code is in a class named Import, i want to see the values of NumSepaERG in a windows form Listbox. Commented Oct 10, 2012 at 15:35

3 Answers 3

1
ListBox listBox1 = new ListBox();
// add items 
listBox1.Items.Add(NumSepaERG[0]);
// add to controls 
Controls.Add(listBox1);

if you have array as items then you can use AddRange method as:

listBox1.Items.AddRange(NumSepaERG);
Controls.Add(listBox1);

Update

create object of your class which contain array...

e.g. :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        ListBox listBox1 = new ListBox();
        MyClass obj = new MyClass();
        listBox1.DataSource = obj.NumSepaERG;
        Controls.Add(listBox1);
    }
}
public class MyClass
{
    public double[] NumSepaERG { get; set; }
    public MyClass()
    {
        NumSepaERG =new double[] {2.0, 5.6};
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

NumSepaERG is on a class named Import, and I want to see the elements of NumSepaERG on a listbox in my windows form..
Ok, but NumSepaERG contains data from a file that was selected with openfiledialog, that's why is in the Import class
1

You can try with this code

var listbox2 = new ListBox();
foreach(var item in columnas)
{
 listbox2.Items.Add(item);
}
this.Controls.Add(listbox2 );

9 Comments

You dont have to create (first line of A.Yakoub code) that listbox. You can use listbox that you already put on you form with "Form Designer".
Hi IvanL, I new in C# how do I add the listbox to FOrm.controls??
@Carl you can try with this.Controls.Add(listbox2);
YourForms.Controls.Add(listbox2)
My form is named Form1 but doesnt appear Controls, it appear only ControlCollection
|
1

ListBox.Items implements IEnumerable through ListBox.ObjectCollection so you can use a foreach to loop over the elements.

foreach (var element in listBox2.Items)
{
    MessageBox.Show(element.ToString());
}

1 Comment

Looks like I misunderstood, I thought the OP wanted to know how to view the elements in the listbox...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.