簡體   English   中英

如何使ListBox中的第一項被自動選擇?

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

我在包含列表框的窗體中有一個UserControl。 我想自動選擇列表框中的第一個項目(假設至少有一個項目),但是我無法使以下代碼正常工作:

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

一切正常,除了自動選擇第一個項目。

您可能需要在開始收聽事件以及實際廣播事件的地方交換線路。

嘗試更改:

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部分中的任何設置來幫助避免這種情況。

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

您必須更改項目本身的屬性:

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

第一行不是真正必要的,但我將其包括在內以防止出現某些問題。 如果列表框中沒有任何項目,則還應該處理IndexOutOfRangeException

您可以簡單地嘗試:使用0索引。

listBoxControl1.Items[0].Selected = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM