简体   繁体   English

如何使ListBox中的第一项被自动选择?

[英]How do I make that the first item in ListBox will be selected automatically?

I have a UserControl in a Form that contains a Listbox. 我在包含列表框的窗体中有一个UserControl。 I would like to automatically select the first item in the Listbox (assuming there is at least one item) but I cannot get the following code to work: 我想自动选择列表框中的第一个项目(假设至少有一个项目),但是我无法使以下代码正常工作:

private void Lightnings_Mode_Load(object sender, EventArgs e)
        {
            this.Size = new Size(416, 506);
            this.Location = new Point(23, 258);
            listBoxIndexs();
            listBoxControl1.MyListBox.SelectedIndex = 0;
            if (this.listBoxControl1.MyListBox.Items.Count > 0)
                this.listBoxControl1.MyListBox.SelectedIndex = 0;
            listBoxControl1.MyListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
            this.listBoxControl1.ItemRemoved += new EventHandler<ItemEventArgs>(listBoxControl1_ItemRemoved);
        }

This line: listBoxControl1.MyListBox.SelectedIndex = 0; 这行:listBoxControl1.MyListBox.SelectedIndex = 0; will mark the first ListBox item in blue like it is selected. 会像选中它一样将第一个ListBox项目标记为蓝色。 But it's not realy selecting the item! 但这并不是真的选择项目!

So I tried to add this: 因此,我尝试添加以下内容:

if (this.listBoxControl1.MyListBox.Items.Count > 0)
                    this.listBoxControl1.MyListBox.SelectedIndex = 0;

But it's not working either. 但这也不起作用。

This is the SelectedIndex event: 这是SelectedIndex事件:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            item = listBoxControl1.MyListBox.SelectedItem.ToString();
            this.f1.PlayLightnings();
            f1.pdftoolsmenu();
            int indx = listBoxControl1.MyListBox.SelectedIndex;
            if (listBoxControl1.Indices.Contains(indx))
            {
                if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
                {
                    pdf1.Lightnings.Add(item.ToString());
                }
            }
        }

The name of the event is not right I have to change it since it's the ListBox over the UserControl but it's the right one. 事件的名称不正确,我必须更改它,因为它是UserControl上的ListBox,但它是正确的。

When I put a breakpoint in the SelectedIndex event and I click on an item, it stops at the breakpoint. 当我在SelectedIndex事件中放置一个断点并单击一个项目时,它会在该断点处停止。 But I want it to go to the selectedIndex event automatically once I show/open the new Form with the UserControl and the ListBox. 但是,我希望使用UserControl和ListBox显示/打开新窗体后,它会自动进入selectedIndex事件。

So if I put a breakpoint in the SelectedIndex event, when I click the button in Form1 to show/open the new Form it will automatically stop in the breakpoint like I was clicking the first item. 因此,如果我在SelectedIndex事件中放置一个断点,则当我单击Form1中的按钮以显示/打开新窗体时,它会自动在断点处停止,就像我单击第一项一样。

This is in Form1 the code that show the new Form: 这是Form1中显示新表单的代码:

if (toolStripComboBox2.SelectedIndex == -1 && toolStripComboBox1.SelectedIndex == -1)
            {
            }
            else
            {
                Lightnings_Extractor.Lightnings_Mode lightningsmode1 = new Lightnings_Extractor.Lightnings_Mode(this);
                lightningsmode1.Show();
            }

Everything is working except for selecting the first item automatically. 一切正常,除了自动选择第一个项目。

You may need to swap the lines where you begin to listen for your event and where you actually broadcast it. 您可能需要在开始收听事件以及实际广播事件的地方交换线路。

Try changing: 尝试更改:

if (this.listBoxControl1.MyListBox.Items.Count > 0)
    this.listBoxControl1.MyListBox.SelectedIndex = 0;
listBoxControl1.MyListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);

To this: 对此:

listBoxControl1.MyListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
if (this.listBoxControl1.MyListBox.Items.Count > 0)
    this.listBoxControl1.MyListBox.SelectedIndex = 0;

Typically I subscribe to EventHandlers in the OnLoad section of my form, and adjust any setting in my OnShow section to help avoid this sort of thing. 通常,我在表单的OnLoad部分中订阅EventHandlers,并调整OnShow部分中的任何设置来帮助避免这种情况。

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.setselected(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.setselected(v=vs.90).aspx

instead of 代替

if (this.listBoxControl1.MyListBox.Items.Count > 0)
                    this.listBoxControl1.MyListBox.SelectedIndex = 0;

Try this: 尝试这个:

if (this.listBoxControl1.MyListBox.Items.Count > 0)
                    this.listBoxControl1.MyListBox.SetSelected(0,true);

You have to change the Property of the item itself: 您必须更改项目本身的属性:

listBoxControl1.Focus();
listBoxControl1.Items[0].Selected = true;

the first line is not really necessary, but I would include it to prevent some issues. 第一行不是真正必要的,但我将其包括在内以防止出现某些问题。 You also should handle the IndexOutOfRangeException in case you have no items in the listbox. 如果列表框中没有任何项目,则还应该处理IndexOutOfRangeException

您可以简单地尝试:使用0索引。

listBoxControl1.Items[0].Selected = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM