如何在 C# 中移动 TrackBar 以响应鼠标事件?
这可能是一个 n00b 查询。我需要根据鼠标按下事件更改轨迹栏值。我的实现如下:
private void MoveTrackBarToMouseClickLocation(TrackBar a_tBar, int a_mouseX)
{
// Jump to the clicked location
double dblValue;
dblValue = ((double)a_mouseX / (double)a_tBar.Width) * (a_tBar.Maximum - a_tBar.Minimum);
a_tBar.Value = Convert.ToInt32(dblValue);
}
该部分工作正常。当按下鼠标按钮时,我无法使滚动正常工作。例如,如果我单击轨迹栏,然后按下鼠标说出值 50,我希望能够在鼠标按下时向右或向左滚动(从值 = 50 开始)。
我希望我已经把我的小问题说清楚了。 任何帮助表示赞赏。 谢谢
This is probably a n00b query. I have a need where I want to change the trackbar value based on a mouse down event. This I achieved as follows:
private void MoveTrackBarToMouseClickLocation(TrackBar a_tBar, int a_mouseX)
{
// Jump to the clicked location
double dblValue;
dblValue = ((double)a_mouseX / (double)a_tBar.Width) * (a_tBar.Maximum - a_tBar.Minimum);
a_tBar.Value = Convert.ToInt32(dblValue);
}
That part works fine. I am having trouble getting the scroll working while the mouse button is pressed. e.g. If I click on the trackbar and it takes me to say value 50 with the mouse down, I want to be able to scroll right or left (from value=50) while that mouse is down.
I hope I have made my small issue clear.
Any help is appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 中执行代码
MouseMove
事件,以及MouseDown
事件。当鼠标移动而其中之一时,会发生此事件按钮被按住。相比之下,您当前处理的
MouseDown
事件仅在每次按下鼠标按钮时引发一次。这就是为什么当用户移动鼠标时TrackBar
不移动,但在第一次按下按钮时正常工作的原因。您没有显示连接事件处理程序和/或调用
MoveTrackBarToMouseClickLocation
函数的代码,因此这是我能得到的最具体的内容。但如果您已经成功连接了MouseDown
事件,那么这应该是一个简单的修复。You need to execute your code in the
MouseMove
event, as well as theMouseDown
event.This event occurs when the mouse is moved while one of the buttons is held down. In contrast, the
MouseDown
event that you currently handle only gets raised once each time the mouse button is pressed down. That's why theTrackBar
is not moving when the user moves the mouse, but is working properly the first time the button is pressed.You didn't show the code where you wired up the event handlers and/or call the
MoveTrackBarToMouseClickLocation
function, so that's as specific as I can get. But if you managed to wire up theMouseDown
event already, this should be a simple fix.