I have this code:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (listBox1.SelectedItem != null)
{
item = listBox1.SelectedItem.ToString();
}
}
Then in the Load event of the Form where the listBox1 is on in the designer I did:
private void NewForm_Load(object sender, EventArgs e)
{
this.Size = new Size(416, 506);
this.Location = new Point(23, 258);
listBoxIndexs();
this.listBox1.SelectedIndex = 0;
}
listBoxIndexs() is:
private void listBoxIndexs()
{
for (int i = 0; i < Form1.test.Count; i++)
{
listBox1.Items.Add(Form1.test[i]);
}
}
In the Load event, I did:
this.listBox1.SelectedIndex = 0;
So when I make Show to this Form i see the listBox when index 0 is already selected.
The problem is when im using my keys arrows up and down to move between the items i see only the Frame around the items moving up down the selectedIndex is all the time keep on 0.
How can i fix it?
I tried to add after the this.listBox1.SelectedIndex = 0; also listBox1.Select(); but it didn’t help.
I’m using visual studio c# 2010 pro .net 4 Client Profile.
Make sure your
ListBox.SelectionModeis set toOne.If it is set to
MultiSimpleorMultiExtended, selections will only occur when the user uses the spacebar or the mouse button to select the item. The arrow keys in these modes just moves the selection marquee.