locked
How can i disable highlighting on selected listbox items? in a listbox control? RRS feed

  • Question

  • I am using a listbox but only displaying one item at a time (by making the listbox only big enough to show one item) ; and the user is using the scroll bar up and down buttons to change the selection.

    Like this

    I am then using the TopIndex property to get the text of the current listbox item. Using this code

    string text = lbAs.Items[lbAs.TopIndex].ToString();

    So I don't want the highlight on the item, how can I disable the highlight on the list box?

    Thank you




    Sunday, October 4, 2015 8:30 PM

Answers

  • Hi CSharpProtogeeInTraining,

    [So I don't want the highlight on the item, how can I disable the highlight on the list box?]

    You need to change the SelectionMode property to None. 

    In addition, for this kind of question, I suggest you post in Windows Forms General forum.
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=winforms

    Best Regards,
    Li Wang

    • Proposed as answer by DotNet Wang Wednesday, October 14, 2015 9:50 AM
    • Marked as answer by Youjun Tang Monday, October 19, 2015 6:57 AM
    Monday, October 5, 2015 3:08 AM

All replies

  • Hi CSharpProtogeeInTraining,

    [So I don't want the highlight on the item, how can I disable the highlight on the list box?]

    You need to change the SelectionMode property to None. 

    In addition, for this kind of question, I suggest you post in Windows Forms General forum.
    https://social.msdn.microsoft.com/Forums/en-US/home?forum=winforms

    Best Regards,
    Li Wang

    • Proposed as answer by DotNet Wang Wednesday, October 14, 2015 9:50 AM
    • Marked as answer by Youjun Tang Monday, October 19, 2015 6:57 AM
    Monday, October 5, 2015 3:08 AM
  • Greetings CSharpProtogeeInTraining.

    This is slightly off-topic, but I would like to point out that unless there are only two or three items in each ListBox, then making them so small as to only display one item at a time is a very bad idea.

    Suppose there is one item in the list for every letter of the alphabet, and the current selection is "A" and the user wants to change to "Z". He/she will have to click the little arrow twenty five times. Now imagine having to do that for umpteen ListBoxes at a time. The user will want to strangle you.

    And that's assuming that the items are in a known and easily-remembered order, like the letters of the alphabet. Suppose the items are in some other sort of order, so that the user doesn't know exactly where in the list to find the one that he/she is looking for and doesn't know whether to scroll up or down or how far. In that case strangulation would be too good for you.

    I strongly recommend considering ComboBoxes instead.

    Monday, October 5, 2015 3:35 AM
  • Try handling the SelectedIndexChanged event (you can add a common function for all of the listboxes):

    private void listBox_SelectedIndexChanged( object sender, EventArgs e )
    {
        ListBox lb = (ListBox)sender;
        if( lb.SelectedIndex >= 0 ) lb.SelectedIndex = -1;
    }

    Tuesday, October 6, 2015 5:22 AM