所以我們都熟悉點擊和按住鼠標按鈕,然後將鼠標移動到網格邊緣,列/行滾動和選擇增長的功能。使用鼠標滾動DataGridView
我有一個基於DataGridView的控件,由於性能問題我必須關閉MultiSelect並自己處理選擇過程,現在也禁用了click + hold滾動功能。
有關如何回寫此功能的任何建議?
我想使用一些簡單的MouseLeave事件,但我不知道如何確定它離開的位置,以及實現動態滾動速度。
所以我們都熟悉點擊和按住鼠標按鈕,然後將鼠標移動到網格邊緣,列/行滾動和選擇增長的功能。使用鼠標滾動DataGridView
我有一個基於DataGridView的控件,由於性能問題我必須關閉MultiSelect並自己處理選擇過程,現在也禁用了click + hold滾動功能。
有關如何回寫此功能的任何建議?
我想使用一些簡單的MouseLeave事件,但我不知道如何確定它離開的位置,以及實現動態滾動速度。
就在這個代碼添加到您的Form1_Load的
DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel);
而這一個是鼠標滾輪事件
void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
int scrollLines = SystemInformation.MouseWheelScrollLines;
if (e.Delta > 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex
= Math.Max(0, currentIndex - scrollLines);
}
else if (e.Delta < 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex
= currentIndex + scrollLines;
}
}
有時我得到這個System.ArgumentOutOfRangeException – Timeless
不會發生System.ArgumentOutOfRangeException如果:
void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
int scrollLines = SystemInformation.MouseWheelScrollLines;
if (e.Delta > 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines);
}
else if (e.Delta < 0)
{
if (this.DataGridView1.Rows.Count > (currentIndex + scrollLines))
this.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines;
}
}
完整答案 你需要設置焦點Datagridview
private void DataGridView1_MouseEnter(object sender, EventArgs e)
{
DataGridView1.Focus();
}
then Add Mouse wheel event in Load function
DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel);
Finally Create Mouse wheel function
void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
int scrollLines = SystemInformation.MouseWheelScrollLines;
if (e.Delta > 0)
{
this.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines);
}
else if (e.Delta < 0)
{
if (this.DataGridView1.Rows.Count > (currentIndex + scrollLines))
this.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines;
}
}
它適用於我。
你能對你的問題更具體嗎?你可以把一段代碼(如果你做了什麼)? – Priyank
我還沒有做任何事情......我希望能夠在編碼之前以合理的方式得到一些(一般)指導。 – ChandlerPelhams