none
How to enable only one selected ListBox at a time RRS feed

  • Question

  • I have two ListBoxes: "OnlineProjects" and "Projects" I want the user to be able to select an item from one of them, but not both. when the user clicks on a one ListBox and the an item from the other listbox is selected, the currently selected item will be unselected, and the item the users clicks on will be selected instead.

    Is it possible?

    Thank you in advance!


    • Edited by avivgood Monday, November 18, 2019 4:30 PM
    Monday, November 18, 2019 4:25 PM

Answers

  • Hi avivgood,

    You can clear a ListBox’s selection by setting “SelectedIndex = -1”.

    Here is my code you can refer to.

    private void OnlineProjects_SelectedIndexChanged(object sender, EventArgs e) { if (OnlineProjects.SelectedIndex > -1) { Projects.SelectedIndex = -1; } } private void Projects_SelectedIndexChanged(object sender, EventArgs e) { if (Projects.SelectedIndex > -1) { OnlineProjects.SelectedIndex = -1; }

    }

    Best Regards,

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 19, 2019 1:47 AM

All replies

  • Hi avivgood,

    When (OnlineProject.Text = "") or (OnlineProjects.SelectedIndex = -1), make "Projects" enabled.
    When (OnlineProject.Text <> "") and (OnlineProjects.SelectedIndex <> -1), make "Projects" blank.

    Regards,

    Ashidacchi -- http://hokusosha.com

    Monday, November 18, 2019 11:27 PM
  • Hi avivgood,

    You can clear a ListBox’s selection by setting “SelectedIndex = -1”.

    Here is my code you can refer to.

    private void OnlineProjects_SelectedIndexChanged(object sender, EventArgs e) { if (OnlineProjects.SelectedIndex > -1) { Projects.SelectedIndex = -1; } } private void Projects_SelectedIndexChanged(object sender, EventArgs e) { if (Projects.SelectedIndex > -1) { OnlineProjects.SelectedIndex = -1; }

    }

    Best Regards,

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 19, 2019 1:47 AM