title: How can I programmatically select an item in a listbox?
body: <p>I have a listbox displaying items from an enum. I want to select/highlight the current value (read from a database) when the listbox displays/the form opens. This code, though:</p>
  
  <pre><code>lblSelectedPrinter.Text = AppSettings.ReadSettingsVal("beltprinter");
  listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.IndexOf(lblSelectedPrinter.Text);
  </code></pre>
  
  <p>...does not work. I saw an example using "GetItemAt" here (<a href="https://stackoverflow.com/questions/831296/programmatically-selecting-items-indexes-in-a-listbox">Programmatically selecting Items/Indexes in a ListBox</a>) but my stripped down and archaic version of C# (.NET 1.1, C# 2) has no such critter.</p>
  
  <h2>UPDATE</h2>
  
  <p>I thought this would work:</p>
  
  <pre><code>string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
  lblSelectedPrinter.Text = currentPrinter;
  int currentPrinterIndex = listBoxBeltPrinters.Items.IndexOf(currentPrinter);
  listBoxBeltPrinters.SelectedItem = currentPrinterIndex;
  </code></pre>
  
  <p>...but it, also, does not (the current printer displays in the label, but the corresponding entry/value in the listbox is not selected).</p>