列表框事件触发异常 [英] Listbox events firing strangely

查看:58
本文介绍了列表框事件触发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.我基本上是想告诉用户何时单击了列表框中的某些内容,按住按钮并离开了列表框.这是我正在做的事情的一个愚蠢的版本:

I'm confused. I am basically trying to tell when the user has clicked something in the listbox, held the button, and left the listbox. Here is a somewhat dumbed down version of what I am doing:

private bool itemHeld;
    private void listOriginal_MouseDown(object sender, MouseEventArgs e)
    {
        itemHeld = true;
    }

    private void listOriginal_MouseUp(object sender, MouseEventArgs e)
    {
        itemHeld = false;
    }

    private void listOriginal_MouseLeave(object sender, EventArgs e)
    {
        if (itemHeld)
            MessageBox.Show("OHH YEAH");
    }

在我看来,当您按下鼠标按钮时,它应该将itemHeld设置为true,仅在将其抬起时才将其设置为false,如果值为true,则显示ohh是.如果我中断了鼠标按下事件以检查该值,则为true,并且如果我从那里继续继续,则会显示该消息.如果我不休息,它什么也不做.这里还有其他工作吗?

To me that seems like it should turn itemHeld true when you press the mousebutton, turn it false only if you lift it, and display ohh yeah if the value is true. If I break on the mouse down event to check the value, it is true and if I continue from there it displays the message. If I do not break, it does nothing. Is there something else at work here?

简短说明:很难解释我实际上要完成的工作,但是想像一下几乎就像将文件从窗口中拖出一样.我只需要能够识别用户何时在列表框内单击,然后在有意义的情况下将其拖出列表框

Brief description: It would be difficult to explain what I am really trying to accomplish but imagine something almost like dragging a file off of a window. I need to simply be able to recognize when the user clicks inside of the listbox and then drags out of the listbox if that makes sense

推荐答案

这是什么?

private void listBox1_MouseMove(object sender, MouseEventArgs e)
{

    if (e.X > listBox1.Width - 1 || e.Y > listBox1.Height - 1 || e.X < 0 || e.Y < 0) 
    {
        Console.WriteLine("drag out");
    }
    else
        Console.WriteLine("mouse move {0}/{1}", e.X, e.Y);
}

它使用了这样一个事实,即在释放鼠标按钮之前不保留控件...但是要注意,拖出部分会发生多次,因此您可能希望第一次设置一个标志...并在鼠标向上或离开时清除该标志

it uses the fact that the Control is not left before the mousebutton is released ... but be aware that the drag out part will occur more than once so you probably will want to have a flag set the first time ... and have that flag cleared on mouse up or leave

这篇关于列表框事件触发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆