寻求帮助解决我遇到的问题
很抱歉,如果这个问题已经被问过,我找不到类似的问题。
想法是单击图片框时将图像更改为打开。
如果按住图片框超过 2 秒以打开新窗体并使图片框保持关闭状态。
但是,如果点击图片框然后保持 2 秒然后返回,我需要图片框状态保持打开状态。
这是我到目前为止尝试过的方法。
我相信要使其正常工作,我需要阻止 MouseUp 事件的发生。
有什么方法可以在 Tick 发生时停止 MouseUp?
有没有更简单/更好的方法来做到这一点?
如有任何帮助,我们将不胜感激。
private void time_HoldDownInternal_Tick(object sender, EventArgs e)
{
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
form1show.Visible = true;
}
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
time_HoldDownInternal.Enabled = true;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
//MessageBox.Show("mouse up");
time_HoldDownInternal.Enabled = false;
time_HoldDownInternal.Interval = 1000;
}
private void pb_pictureBoxTest_Click(object sender, EventArgs e)
{
if (mainMenuVariables.mousedown == true)
{
if (mainMenuVariables.pictureBox == false)
{
mainMenuVariables.pictureBox = true;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOn);
return;
}
if (mainMenuVariables.pictureBox == true)
{
mainMenuVariables.pictureBox = false;
pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOff);
return;
}
}
if (mainMenuVariables.mousedown == false)
{
//nothing
}
}
最佳答案
与其启动计时器,不如在按下鼠标时记录当前时间。然后在鼠标向上,检查它是否已经 2 秒。例如:
private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = true;
mainMenuVariables.mousedowntime = DateTime.Now;
}
private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
mainMenuVariables.mousedown = false;
var clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;
if ( clickDuration > TimeSpan.FromSeconds(2))
{
// Do 'hold' logic (e.g. open dialog, etc)
}
else
{
// Do normal click logic (e.g. toggle 'On'/'Off' image)
}
}
关于c# - 计时器、点击、鼠标按下、鼠标弹起事件不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406422/