【问题标题】:How do I make that the first item in ListBox will be selected automatically?如何自动选择 ListBox 中的第一项?
【发布时间】:2013-01-12 16:25:28
【问题描述】:

我在一个包含列表框的表单中有一个用户控件。我想自动选择列表框中的第一个项目(假设至少有一个项目),但我无法让以下代码工作:

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);
        }

这一行:listBoxControl1.MyListBox.SelectedIndex = 0;将第一个 ListBox 项目标记为蓝色,就像它被选中一样。但这并不是真正选择项目!

所以我尝试添加这个:

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

但它也不起作用。

这是 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());
                }
            }
        }

事件的名称不正确,我必须更改它,因为它是 UserControl 上的 ListBox,但它是正确的。

当我在 SelectedIndex 事件中设置断点并单击一个项目时,它会在断点处停止。但是,一旦我使用 UserControl 和 ListBox 显示/打开新表单,我希望它自动转到 selectedIndex 事件。

因此,如果我在 SelectedIndex 事件中设置断点,当我单击 Form1 中的按钮以显示/打开新表单时,它将自动停止在断点处,就像我单击第一项一样。

这是在 Form1 中显示新表单的代码:

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

除了自动选择第一项外,一切正常。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    您可能需要交换您开始收听事件的位置和实际广播它的位置。

    尝试改变:

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

    到这里:

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

    通常我在表单的 OnLoad 部分订阅 EventHandlers,并调整 OnShow 部分中的任何设置以帮助避免此类事情。

    【讨论】:

    • +1 我只是在写它(当然,如果这整个东西是指 WinForms 列表框)
    【解决方案2】:

    您必须更改项目本身的属性:

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

    第一行并不是真正必要的,但我会包括它以防止出现一些问题。 如果列表框中没有项目,您还应该处理IndexOutOfRangeException

    【讨论】:

      【解决方案3】:

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

      而不是

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

      试试这个:

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

      【讨论】:

      • ScruffyDuck 不,它没有进入 SelectedIndex 事件。
      • Duck answer 和 Josh W 的组合奏效了。在行之间交换并使用 SetSelected:listBoxControl1.MyListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged); if (this.listBoxControl1.MyListBox.Items.Count > 0) this.listBoxControl1.MyListBox.SetSelected(0, true);
      【解决方案4】:

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

      listBoxControl1.Items[0].Selected = true;
      

      【讨论】:

      • listBoxControl1 没有属性 Items。 listBoxControl1 是一个用户控件。 listBox 是:listBoxControl1.MyListBox 所以我做了:listBoxControl1.MyListBox.Items[0].Selected = true;但没有 Selected 属性。 [0] 之后。没有选择。 MyListBox 是 UserControl 上的 listBox1(listBoxControl1)
      • @JhonatanBirdy 我很困惑,因为 paedow 的代码和我的一样!你是说我的不可接受......嗯......
      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多