Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I prevent last item of the ListBox get selected when empty area is clicked
    text
    copied!<p>I have a list box which is not filled with items all the way down, and has some empty area after last item. When I click that empty area the last item is automatically get's selected. And that selection happens before <code>MouseDown</code> event. And I want to prevet it from happening.</p> <p>I can keep current selected index (only one item can be selected) in a variable in <code>SelectedIndexChanged</code>, and reset it in <code>MouseDown</code>, but between <code>MouseDown</code> and <code>MouseUp</code> last item is selected - and it doesn't look good.</p> <p>How can I prevent last item from being selected when empty area is clicked?</p> <hr> <p><em>P.S. This is owner-drawn ListBox, but I'm not sure it has anything to do with this issue.</em></p> <pre><code>private void listBox_DrawItem(object sender, DrawItemEventArgs e) { if ((e.State &amp; DrawItemState.Selected) == DrawItemState.Selected) { e.Graphics.FillRectangle(Brushes.LightSteelBlue, e.Bounds); } else { e.Graphics.FillRectangle(Brushes.White, e.Bounds); } if (_commands.Count &gt; 0) { KeyValuePair&lt;string, string&gt; cmd = (KeyValuePair&lt;string, string&gt;)_commands[e.Index]; // FIRST ROW e.Graphics.DrawString(cmd.Key, _cmdNameFont, Brushes.Black, e.Bounds.X, e.Bounds.Y + _cellPadding); // SECOND ROW e.Graphics.DrawString(cmd.Value, _cmdCommandFont, Brushes.Black, e.Bounds.X + 5, e.Bounds.Y + _cmdNameFont.Height + _cellPadding); } e.DrawFocusRectangle(); } private void listBox_MeasureItem(object sender, MeasureItemEventArgs e) { e.ItemHeight = _cmdNameFont.Height + _cmdCommandFont.Height + _cellPadding * 2 ; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload