Tango-Charlie

Hi,
Here is what I am doing....
I have list box filled up with integer nos. I want to sort them, so I have a sort button below it. In the event handler of sort, I am sorting the items in the list box by taking them in the array and calling array.sort()....Now I want to assign this sorted items back to the list box....Question is how do i do that...

Well, Listbox.items does not let me assign to it since its just a get property. So I cleared the list box using Items.clear() and tried to reassign items using Itmes.AddRange() method. But the it does not accept any parameter of type Int32[]. So if I say

Int32 nos = new Int32[]..
..
//..sort nos...

Listbox1.Itmes.AddRange(nos)
It gives error.

Any idea on this...


Re: Windows Forms General How do i assign items to the list box...

paoloTheCool

Try this:

Code Block

private void Form1_Load(object sender, EventArgs e)

{

int[] nos = {1, 2, 3, 4, 5, 6, 7, 8, 9};

foreach (int integer in nos)

{

listBox1.Items.Add(integer.ToString());

listBox1.Sorted = true;

}

}

If this is not what you are trying to do, then I am very sorry. Wink

If you have any questions, please ask.

Thanks,

paoloTheCool





Re: Windows Forms General How do i assign items to the list box...

Jeris

please try the following codes:

object[] items=new object[] {1,2,3,4,5};

Listbox1.Items.AddRange(items);





Re: Windows Forms General How do i assign items to the list box...

Manju Sandhu

Hi,

Either set the listbox sort property to true. and if you dont want to do this then simply use this code.

Int32[] listItem ;

//stored sorted listBox1 item in listItem

listBox1.Items.Clear();

for(int j=0;j<listItem.Length;j++)

listBox1.Items.Add(listItem[j]);

Regards,

Manju Sandhu





Re: Windows Forms General How do i assign items to the list box...

Tango-Charlie

Hi Guys,
Thanks for your answers....
1) Basically I had an array of sorted nos (Int32[]) which I wanted to assign to the list box directly without iterating through array and adding each item saperatly. There is AddRange method but it only takes array of objects as a parameters.
So I guess as manju stated, we ve to iterate through the array to add them in the list box.

Thank You....






Re: Windows Forms General How do i assign items to the list box...

Chunsheng Tang - MSFT

Hi, Tango

Tango-Charlie wrote:
Hi Guys,
Thanks for your answers....
1) Basically I had an array of sorted nos (Int32[]) which I wanted to assign to the list box directly without iterating through array and adding each item saperatly. There is AddRange method but it only takes array of objects as a parameters.
So I guess as manju stated, we ve to iterate through the array to add them in the list box.

Thank You....


An easy way to achieve this:

Code Block

int[] arr = { 3, 4, 5, 1, 2 };

listBox1.DataSource = arr;

Or

Code Block

int[] arr = new int[] { 3, 4, 5, 1, 2 };
object[] objArr = new object[arr.Length];
arr.CopyTo(objArr, 0);
listBox1.Items.AddRange(objArr);

Best Regards

Chunsheng Tang






Re: Windows Forms General How do i assign items to the list box...

Tango-Charlie

Gatch you...This makes sense...
Thank You so much...