Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make that the first item in ListBox will be selected automatically?
    primarykey
    data
    text
    <p>I have a UserControl in a Form that contains a Listbox. 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:</p> <pre><code>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 &gt; 0) this.listBoxControl1.MyListBox.SelectedIndex = 0; listBoxControl1.MyListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged); this.listBoxControl1.ItemRemoved += new EventHandler&lt;ItemEventArgs&gt;(listBoxControl1_ItemRemoved); } </code></pre> <p>This line: listBoxControl1.MyListBox.SelectedIndex = 0; will mark the first ListBox item in blue like it is selected. But it's not realy selecting the item!</p> <p>So I tried to add this:</p> <pre><code>if (this.listBoxControl1.MyListBox.Items.Count &gt; 0) this.listBoxControl1.MyListBox.SelectedIndex = 0; </code></pre> <p>But it's not working either.</p> <p>This is the SelectedIndex event:</p> <pre><code>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 &amp;&amp; !pdf1.Lightnings.Contains(item.ToString())) { pdf1.Lightnings.Add(item.ToString()); } } } </code></pre> <p>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.</p> <p>When I put a breakpoint in the SelectedIndex event and I click on an item, it stops at the breakpoint. But I want it to go to the selectedIndex event automatically once I show/open the new Form with the UserControl and the ListBox. </p> <p>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.</p> <p>This is in Form1 the code that show the new Form:</p> <pre><code>if (toolStripComboBox2.SelectedIndex == -1 &amp;&amp; toolStripComboBox1.SelectedIndex == -1) { } else { Lightnings_Extractor.Lightnings_Mode lightningsmode1 = new Lightnings_Extractor.Lightnings_Mode(this); lightningsmode1.Show(); } </code></pre> <p>Everything is working except for selecting the first item automatically.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload