Reputation: 302
I have code that frequently updates the contents of my DataGridView, by clearing the previous contents, and then re-filling the entire DataGridView with new rows of data. But if I click and hold on the scroll bar, I would like for it to NOT update the DataGridView while I am scrolling.
1: How do I detect that I am currently holding down the mouse button in a scroll operation?
What I tested so far:
The DataGridView.MouseDown event does NOT fire if I click on the scroll bar. It only fires if I click on some content within the DataGridView, but it ignores clicks on the actual scroll bar itself. Curiously though, the MouseEnter event DOES fire when I hover the scroll bar. But I don't see how I can tell whether I am scrolling or not, just by detecting a hover. I need to detect the MouseDown event on the scroll bar.
Upvotes: 1
Views: 566
Reputation: 302
Here's the code:
private void Form1_Load(object sender, EventArgs e)
{
PropertyInfo property = dataGridView1.GetType().GetProperty(
"VerticalScrollBar", BindingFlags.NonPublic | BindingFlags.Instance);
if (property != null)
{
ScrollBar scrollbar = (ScrollBar)property.GetValue(dataGridView1, null);
scrollbar.MouseCaptureChanged += Scrollbar_MouseCaptureChanged;
scrollbar.MouseLeave += Scrollbar_MouseLeave;
}
}
static bool MouseIsDown = false;
private void Scrollbar_MouseLeave(object sender, EventArgs e)
{
MouseIsDown = false;
}
private void Scrollbar_MouseCaptureChanged(object sender, EventArgs e)
{
MouseIsDown = !MouseIsDown;
}
Then, simply check if MouseIsDown == true, and that'll tell you if the mouse is currently held down on the scroll bar.
Upvotes: 1