开发者

Clear ListBox Selection when empty area is clicked

At least one item always remain selected in ListBox. I want that when user clicks empty area of ListBox, selection shou开发者_如何学运维ld be cleared. How to do this?

I am trying to replicate Opera Notes as a part of my application. First i was using a binded DataGridView now i am using a binded ListBox on left pane


Handle the ListBox.MouseDown event.

Call ListBox.IndexFromPoint, passing the Location property from the MouseDown event's MouseEventArgs parameter.

This should return the index of the item that was clicked, or ListBox.NoMatches if the click was on an empty area.

If the return value is ListBox.NoMatches, set the ListBox.SelectedIndex property to -1 to clear the selection.


Mr. Avalanchis has answered the question already. I am just adding the code necessary to follow the steps what he has suggested. Hope the explicit code will help.

private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        Point pt = new Point(e.X, e.Y);
        int index = listBox1.IndexFromPoint(pt);

        if (index <= -1)
        {
            listBox1.SelectedItems.Clear();
        }


    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜