通过双击访问列表框项 [英] Access listbox item by double-click

查看:67
本文介绍了通过双击访问列表框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这是一个痛苦的简单问题.

我有一个包含3个条目的列表框:





我有一个字符串x.

当用户双击"Cat"时,我希望触发设置x ="Cat"的方法/事件处理程序.如果他们双击"Dog",我希望x ="Dog".

令人惊讶的是,我在执行此操作时遇到了很多麻烦.只需一个快速的代码示例.

解决方案

处理列表框的DoubleClick事件:

 私有 字符串 x;
私有 无效 myListBox_DoubleClick(对象发​​件人,EventArgs e)
    {
    x = myListBox.SelectedItem.ToString();
    } 


我将在一个更广泛"的水平上回答您的问题(一次教育者,永远是一名教育者?),而不仅仅是如何您将获得一个特定的DoubleClick EventHandler,并连接"到一个特定的ListBox.

首先,您的目标是什么:

1.如果用户仅单击ListBox中的一个条目,但是单击更改了该ListBox的当前SelectedIndex属性,会发生什么:通过单击均值"上下文中的任何内容,该选择的潜在变化是否会发生?您的解决方案?

请注意,有时....取决于外部变量,系统设置,鼠标类型,单击速度,鼠标按钮的磨损程度等,可能会遇到麻烦获得双击事件.

尝试以下WinForm实验:放置一个ListBox(此处称为"listBox1"),并在其中使用与您现在正在使用的相同的三个项("Dog Cat Fish"),并添加一个大尺寸的TextBox,并将"Multi-Line"设置为"true" ,并设置为显示一个垂直滚动条,名为"tbEventReport".

将此代码添加到包含"listBox1 ListBox"和"tbEventReport TextBox"的表单中:

  private  私有 无效 ReportEvent(字符串 eventName)
    {
        tbEventReport.Text + =
            " 
            + eventId
            + " 
            + eventName
            + " 
            + " 
            // 请注意,此处不需要使用'ToString()... 
            + listBox1.SelectedIndex
            + Environment.NewLine
            + " 
            + Environment.NewLine;

        eventId ++;
    }

    私有 无效 listBox1_MouseClick(对象发​​件人,MouseEventArgs e)
    {
        ReportEvent(" );
    }

    私有 无效 listBox1_MouseDoubleClick(对象发​​件人,MouseEventArgs e)
    {
        ReportEvent(" );
    }

    私有 无效 listBox1_Click(对象发​​件人,EventArgs e)
    {
        ReportEvent(" );
    }

    私有 无效 listBox1_DoubleClick(对象发​​件人,EventArgs e)
    {
        ReportEvent(" );
    }

    私有 无效 listBox1_SelectedIndexChanged(对象发​​件人,EventArgs e)
    {
        ReportEvent(" );
    }
} 

在设计时做正确的事情,将上面定义的事件处理程序连接"到"listBox1".

然后运行该应用程序,并尝试通过多种方式单击和双击:注意如何单击已选择的相同项目仍会激发SelectedIndexChanged EventHandler.

相信典型的鼠标动作生成事件的顺序和行为会扎根" ,我相信,这将为您配备一个心理工具包",您可以在将来使用它来设计多种类型的解决方案策略. /blockquote>

Hopefully this is a painfully simple question.

I have a listbox with 3 entries:

Dog
Cat
Fish

I have a string x.

When the user double-clicks on "Cat", I want a method/event handler to fire which sets x = "Cat". If they double-click on "Dog", I want x = "Dog".

Amazingly, I''m having a lot of trouble doing this. Just need a quick code sample.

解决方案

Handle the DoubleClick event for the listbox:

private string x;
private void myListBox_DoubleClick(object sender, EventArgs e)
    {
    x = myListBox.SelectedItem.ToString();
    }


I''m going to respond to your question on a little "broader" level (once an educator, always an educator ?) than just the how of you get a specific DoubleClick EventHandler "wired-up" to a specific ListBox.

First, what are your goals here:

1. what happens if the user only single-clicks on an entry in the ListBox, but that single-click changes the current SelectedIndex Property of that ListBox: does that potential change in selection by single-click "mean" anything in the context of your solution ?

Please note that ... sometimes .... depending on external variables, system settings, mouse-type, how fast you click, how worn-down your mouse-buttons are, etc., it''s possible to "have trouble" getting a double-click Event.

Try this WinForm experiment: put a ListBox (here called ''listBox1), with the same three Items in it (Dog Cat Fish) you are using now, and add a large-sizedTextBox with ''Multi-Line set to ''true, and set to show a vertical scrollbar, named ''tbEventReport.

Add this code to the Form that contains the ''listBox1 ListBox, and the ''tbEventReport TextBox:

    private int eventId;

    private void ReportEvent(string eventName)
    {
        tbEventReport.Text +=
            "Event ID ="
            + eventId
            + " : "
            + eventName
            + " : "
            + "Selected Index = "
            // note that use of 'ToString() is not required here ...
            + listBox1.SelectedIndex
            + Environment.NewLine
            + "-----------------------------------------------------------------------------------------------------"
            +Environment.NewLine;

        eventId++;
    }

    private void listBox1_MouseClick(object sender, MouseEventArgs e)
    {
        ReportEvent("MouseClick");
    }

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        ReportEvent("MouseDoubleClick");
    }

    private void listBox1_Click(object sender, EventArgs e)
    {
        ReportEvent("Click");
    }

    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
        ReportEvent("DoubleClick");
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ReportEvent("SelectedIndexChanged");
    }
}

Do the right thing at design-time to "hook-up" those EventHandlers defined above to ''listBox1.

Then run the app, and experiment with clicking and double-clicking in a variety of ways: notice how clicking on the same item that''s already selected still fires the SelectedIndexChanged EventHandler.

Getting "grounded" in the sequence and behavior of typical Mouse action generated Events will, I believe, equip you with a "mental toolkit" you can use to design many types of solution strategies in the future.


这篇关于通过双击访问列表框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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