Karl__Stevens

This statement :

for (int i = 0; i <= 255; i++)

{

listBox1.Items.Insert(0, String.Format(" {0:X} ", i));

}

Fills the ListBox with values of i in reverse order:

I expected the first item to be overwritten and finally be FF.

Who is right Thanks



Re: Windows Forms General Cannot get ListBox.Items.Insert to work in C# Express

Bob zhu - MSFT

Hi :

you insert into the listbox would not replace the old item,

but add new one at the specified index

but you need set it to another one directly like:

public class MyItem : object

{

public string name;

public override string ToString()

{

return name;

}

}

in form load you add follow:

this.listBox1.Items.Add("");
for (int i = 0; i <= 255; i++)
{
MyItem item = new MyItem();
item.name = String.Format(" {0:X} ", i);
this.listBox1.Items[0] = item;
}

or you can remove and add like follow:

for (int i = 0; i <= 255; i++)
{
this.listBox1.Items.Clear();
this.listBox1.Items.Add(String.Format(" {0:X} ", i));
}






Re: Windows Forms General Cannot get ListBox.Items.Insert to work in C# Express

Karl__Stevens

Thankyou. I used insert thinking it was "replace"

I could not find "Mark as correct answer" using the Alert link, Sorry.