我有这段代码:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (listBox1.SelectedItem != null)
{
item = listBox1.SelectedItem.ToString();
}
}
然后在我执行的设计器中listBox1所在的Form的Load事件中:
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()是:
private void listBoxIndexs()
{
for (int i = 0; i < Form1.test.Count; i++)
{
listBox1.Items.Add(Form1.test[i]);
}
}
在Load事件中,我做了:
this.listBox1.SelectedIndex = 0;
因此,当我对此表单进行显示时,我会在选择索引0时看到listBox。 问题是,当我使用我的键向上和向下箭头在项目之间移动时,我看到只有沿着selectedIndex向上移动的项目周围的框架始终保持为0。
我该如何解决?
我尝试在this.listBox1.SelectedIndex = 0;
listBox1.Select();
之后添加,但它没有帮助。
我正在使用visual studio c#2010 pro .net 4 Client Profile。
答案 0 :(得分:3)
确保ListBox.SelectionMode
设置为One
。
如果设置为MultiSimple
或MultiExtended
,则只有在用户使用空格键或鼠标按钮选择项目时才会进行选择。这些模式中的箭头键只会移动选择框。