Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically select an item in a listbox?

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:

lblSelectedPrinter.Text = AppSettings.ReadSettingsVal("beltprinter");
listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.IndexOf(lblSelectedPrinter.Text);

...does not work. I saw an example using "GetItemAt" here (Programmatically selecting Items/Indexes in a ListBox) but my stripped down and archaic version of C# (.NET 1.1, C# 2) has no such critter.

UPDATE

I thought this would work:

string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
lblSelectedPrinter.Text = currentPrinter;
int currentPrinterIndex = listBoxBeltPrinters.Items.IndexOf(currentPrinter);
listBoxBeltPrinters.SelectedItem = currentPrinterIndex;

...but it, also, does not (the current printer displays in the label, but the corresponding entry/value in the listbox is not selected).

like image 798
B. Clay Shannon-B. Crow Raven Avatar asked Aug 02 '13 16:08

B. Clay Shannon-B. Crow Raven


People also ask

How do I select an item in ListBox?

To select an item in a ListBox, we can use the SetSelect method that takes an item index and a true or false value where the true value represents the item to be selected. The following code snippet sets a ListBox to allow multiple selection and selects the second and third items in the list: listBox1.

How do I select an item in a ListBox in VB net?

Using the ListBox's FindString() method you can find the index of the first item starting with the specified string. You can then use that to set the SelectedIndex property which will select the item at the specified index.

How do I know if an item is selected in ListBox?

To determine the items that are selected, you can use the Selected property of the list box. The Selected property of a list box is an array of values where each value is either True (if the item is selected) or False (if the item is not selected).

Is it possible to select multiple items from ListBox?

Use a multiple-selection list box when you want to: Enable users to select multiple items in a list. Enable users to type their own value in a list.


2 Answers

I see you've already solved this, but why not do it the tried and tested way?

  lblSelectedPrinter.Text = AppSettings.ReadSettingsVal("beltprinter");
  listBoxBeltPrinters.SelectedIndex = -1;
  if (!String.IsNullOrEmpty(lblSelectedPrinter.Text)) {
    for (int index = 0; index < listBoxBeltPrinters.Items.Count; index++) {
      string item = listBoxBeltPrinters.Items[index].ToString();
      if (lblSelectedPrinter.Text == item) {
        listBoxBeltPrinters.SelectedItem = index;
        break;
      }
    }
  }

This way, you know the SelectedIndex value is set to -1 as soon as the text changes, and if it is found in your ListBox, that item is selected.

Even better would be to write a handler when the Label control lblSelectedPrinter fires the TextChanged event.

lblSelectedPrinter.TextChanged += new EventHandler(SelectedPrinter_TextChanged);

Then, create that Event Handler like shown above:

private void SelectedPrinter_TextChanged(object sender, EventArgs e) {
  listBoxBeltPrinters.SelectedIndex = -1;
  if (!String.IsNullOrEmpty(lblSelectedPrinter.Text)) {
    for (int index = 0; index < listBoxBeltPrinters.Items.Count; index++) {
      string item = listBoxBeltPrinters.Items[index].ToString();
      if (lblSelectedPrinter.Text == item) {
        listBoxBeltPrinters.SelectedItem = index;
        break;
      }
    }
  }
}

You've already solved your problem, so this is just food for thought.

like image 180
jp2code Avatar answered Oct 21 '22 16:10

jp2code


This works:

listBoxBeltPrinters.SetSelected(listBoxBeltPrinters.FindString("beltprinter"), true);
like image 20
Tyler Pantuso Avatar answered Oct 21 '22 17:10

Tyler Pantuso