-1

I am making a Windows Forms application with algorithms for school and I want to add some nice functionality to display that the algorithm is working well. One of those things is that when the user selects an item in one listbox, the items that are part of that one item get automatically selected in another listbox. This is done by the application.

I would like it if the user could not select another item in the listbox that is automatically monitored, but enabled = false sets the color to gray which makes the text invisible when an item is automatically selected.

Is there any other way to achieve this?

Pim_vh
  • 143
  • 1
  • 14
  • 1
    You can change the color when it gets disenabled. Check this: https://stackoverflow.com/questions/15773669/change-disabled-listbox-background-to-gray – MyBug18 Mar 31 '20 at 08:33
  • Name your two list box, add some pictures for your question would help to explain. – Louis Go Mar 31 '20 at 08:37
  • For example, I am not sure the "listbox" your second paragraph is the "one listbox" or "another listbox". Naming would help a lot in clarifying your goal. – Louis Go Mar 31 '20 at 08:44
  • you can try with custom Listbox control class, like [here](https://stackoverflow.com/questions/2438168/winforms-listbox-with-readonly-disabled-items). The idea is to ignore certain windows events using overridden window proc handler when ReadOnly mode is set ON – oleksa Mar 31 '20 at 08:56
  • @MyBug18 changing color won't help to scroll the disabled list box control. You have to ensure that selected item is visible to the user additionally – oleksa Mar 31 '20 at 09:07
  • If you want to select an item by using code, you can refer to [ListBox.SetSelected Method](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.setselected?view=netframework-4.8) – Jack J Jun Apr 01 '20 at 03:04

1 Answers1

0

What you could do is

  • When the program selects an entry in the second list, set a flag
  • When an entry in the list is selected, read that flag
  • If the flag is not set, unselect the item
  • Unset the flag

Code wise, this equates to something like the following (please note that I would not write code like this in a real-world szenario, but to get the gist of it, it should suffice)

private bool _valueIsSetProgrammatically = false;

private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
    this._valueIsSetProgrammatically = true;
    this.listBox2.SelectedItem = this.listBox1.SelectedItem;
}

private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
    if (!this._valueIsSetProgrammatically)
    {
        this.listBox2.SelectedItem = null;
    }

    this._valueIsSetProgrammatically = false;
}

Please note that this snippet unselects the second listbox. If you'd like to retain the selected item, you could change the second method to

private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
    if (!this._valueIsSetProgrammatically)
    {
        this.listBox2.SelectedItem = this.listBox1.SelectedItem;
    }

    this._valueIsSetProgrammatically = false;
}

(Technically the flag is not needed in this case, you could simply set the SelectedItem of listBox2 to the SelectedItem of listBox1.)

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57