Question

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

Was it helpful?

Solution

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.

OTHER TIPS

This works:

listBoxBeltPrinters.SetSelected(listBoxBeltPrinters.FindString("beltprinter"), true);
int i = AppSettings.ReadSettingsVal("beltprinter"); //Save it as an int.
listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.IndexOf(i);
lblSelectedPrinter.Text = listBoxBeltPrinters.SelectedItem.toString();

You need it to be an integer. You can use int.Parse to Convert to cast it from string to int.

listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.IndexOf(int.Parse(System.Configuration.ConfigurationSettings.AppSettings.Get("beltprinter")));
lblSelectedPrinter.Text = listBoxBeltPrinters.SelectedItem.toString();

This works:

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

This is the only code required to display, read, and write the settings val:

private void PrinterPickerForm_Load(object sender, System.EventArgs e)
{
    Type type = typeof(PrintUtils.BeltPrinterType);
    foreach (FieldInfo field in type.GetFields(BindingFlags.Static | BindingFlags.Public))
    {
        string display = field.GetValue(null).ToString();
        listBoxBeltPrinters.Items.Add(display);
    }
    string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
    lblCurrentPrinter.Text = currentPrinter;
    int currentPrinterIndex = listBoxBeltPrinters.Items.IndexOf(currentPrinter);
    listBoxBeltPrinters.SelectedIndex = currentPrinterIndex;
}

private void btnSaveSelectedVal_Click(object sender, System.EventArgs e)
{
    string sel = listBoxBeltPrinters.SelectedItem.ToString();
    if (sel != lblCurrentPrinter.Text)
    {
        AppSettings.WriteSettingsVal("beltPrinter", sel);
    }
}

can you try the following??? It takes from your code, and then uses FindString

string currentPrinter = AppSettings.ReadSettingsVal("beltprinter");
lblSelectedPrinter.Text = currentPrinter;
int index = listBoxBeltPrinters.FindString(lblSelectedPrinter.Text);
listBoxBeltPrinters.SelectedIndex = index;

Combination of listBoxObject.SetSelected() and listBoxObject.FindString() is an elegant solution. It works for me, too.

lblSelectedPrinter.Text = AppSettings.ReadSettingsVal("beltprinter");

listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.FindByText(lblSelectedPrinter.Text);

By value:

listBoxBeltPrinters.SelectedItem = listBoxBeltPrinters.Items.FindByValue(1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top