I made two buttons which controls scrolling on a DataGrid OnClick. I'll like to execute the code managing the scroll when users stay press on it.
我制作了两个按钮来控制DataGrid OnClick上的滚动。我想在用户按下它时执行管理滚动的代码。
I tried on MouseDown() but the code is execute only one time.
我试过MouseDown(),但代码只执行一次。
Need help.
3 个解决方案
#1
- When you get a mouse down event, set a timer to start calling a "scroll" callback function every 200ms or so (random guess on the time).
- In the timer callback, scroll by one "notch" (however much you make it.)
- When you get a mouse up event, stop the timer.
当你得到一个鼠标按下事件时,设置一个计时器开始每200ms左右开始调用一次“滚动”回调函数(随机猜测时间)。
在计时器回调中,滚动一个“缺口”(无论你做多少)。
当您获得鼠标按下事件时,请停止计时器。
#2
If you don't want to use the timer, you can always spawn a thread when needed. You only have to be careful to use Invoke() mechanism when using the UI controls, which are on the other thread.
如果您不想使用计时器,则可以在需要时始终生成线程。在使用另一个线程上的UI控件时,您只需要小心使用Invoke()机制。
Code:
private bool mouseDown = false;
private void buttonScrollUp_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
new Thread(() => {
while (mouseDown)
{
Invoke(new MethodInvoker(() => [DO_THE_SCROLLING_HERE));
Thread.Sleep([SET_AUTOREPEAT_TIMEOUT_HERE);
}
})
.Start();
}
private void buttonScrollUp_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
Code snippet above of course lacks some sanity and error cheks.
上面的代码片段当然缺乏一些理智和错误。
LP, Dejan
#3
Main idea is to implement timer, for example every 100ms, and do your logic in tick event. Algorithm can look like:
主要思想是实现计时器,例如每100毫秒,并在tick事件中执行你的逻辑。算法可能如下所示:
- Capture mouse and start timer in MouseDown event
- In MouseMove detect is cursor still over button, if no set flag
- In timer tick check flag is mouse over button, and do your scroll logic
- Release mouse capture and stop timer in MouseUp event
在MouseDown事件中捕获鼠标并启动计时器
在MouseMove中,如果没有设置标志,则检测光标仍然在按钮上
在计时器中,勾选检查标志是鼠标悬停按钮,并执行滚动逻辑
在MouseUp事件中释放鼠标捕获和停止计时器
#1
- When you get a mouse down event, set a timer to start calling a "scroll" callback function every 200ms or so (random guess on the time).
- In the timer callback, scroll by one "notch" (however much you make it.)
- When you get a mouse up event, stop the timer.
当你得到一个鼠标按下事件时,设置一个计时器开始每200ms左右开始调用一次“滚动”回调函数(随机猜测时间)。
在计时器回调中,滚动一个“缺口”(无论你做多少)。
当您获得鼠标按下事件时,请停止计时器。
#2
If you don't want to use the timer, you can always spawn a thread when needed. You only have to be careful to use Invoke() mechanism when using the UI controls, which are on the other thread.
如果您不想使用计时器,则可以在需要时始终生成线程。在使用另一个线程上的UI控件时,您只需要小心使用Invoke()机制。
Code:
private bool mouseDown = false;
private void buttonScrollUp_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
new Thread(() => {
while (mouseDown)
{
Invoke(new MethodInvoker(() => [DO_THE_SCROLLING_HERE));
Thread.Sleep([SET_AUTOREPEAT_TIMEOUT_HERE);
}
})
.Start();
}
private void buttonScrollUp_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
Code snippet above of course lacks some sanity and error cheks.
上面的代码片段当然缺乏一些理智和错误。
LP, Dejan
#3
Main idea is to implement timer, for example every 100ms, and do your logic in tick event. Algorithm can look like:
主要思想是实现计时器,例如每100毫秒,并在tick事件中执行你的逻辑。算法可能如下所示:
- Capture mouse and start timer in MouseDown event
- In MouseMove detect is cursor still over button, if no set flag
- In timer tick check flag is mouse over button, and do your scroll logic
- Release mouse capture and stop timer in MouseUp event
在MouseDown事件中捕获鼠标并启动计时器
在MouseMove中,如果没有设置标志,则检测光标仍然在按钮上
在计时器中,勾选检查标志是鼠标悬停按钮,并执行滚动逻辑
在MouseUp事件中释放鼠标捕获和停止计时器