Find Vertical Scroll Position for CheckedListBox

G

Guest

Hi,

On my x64 Vista PC I am trying to find the index or value of the
WinForms CheckedListBox Item at the current mouse position after a
right click. First I tried using the Click event, but for
CheckedListBox it doesn't fire for a right click. The MouseDown
event fires, I can use the MouseEventArgs argument e.Y to find the
index of the Item, unless the CheckedListBox has been scrolled. I set
the VerticalScroll.Minimum=0; VerticalScroll.Maximum = Items.Count ==
110 for my current test. So far I have tried:

CheckedListBox.AutoScrollOffset.Y
Form.VerticalScroll.Value
Form.AutoScrollPosition.Y

All of these always return 0 regardless of the vertical scrollbar
position. Is there any way to find the index of a right clicked
CheckedListBox Item after vertical scrolling?

Thanks,
invegat
 
M

Morten Wennevik [C# MVP]

nospam@invegat said:
Hi,

On my x64 Vista PC I am trying to find the index or value of the
WinForms CheckedListBox Item at the current mouse position after a
right click. First I tried using the Click event, but for
CheckedListBox it doesn't fire for a right click. The MouseDown
event fires, I can use the MouseEventArgs argument e.Y to find the
index of the Item, unless the CheckedListBox has been scrolled. I set
the VerticalScroll.Minimum=0; VerticalScroll.Maximum = Items.Count ==
110 for my current test. So far I have tried:

CheckedListBox.AutoScrollOffset.Y
Form.VerticalScroll.Value
Form.AutoScrollPosition.Y

All of these always return 0 regardless of the vertical scrollbar
position. Is there any way to find the index of a right clicked
CheckedListBox Item after vertical scrolling?

Thanks,
invegat

Hi invegat,

Did you try the IndexFromPoint method?

void checkedListBox1_MouseDown(object sender, MouseEventArgs e)
{
int index = checkedListBox1.IndexFromPoint(e.Location);
}

It should take scroll position into account and return the correct index.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top