C#向列表框添加点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15646214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:27:59  来源:igfitidea点击:

C# adding click events to a list box

c#listboxonclick

提问by Matt Pruent

I'm trying to add a click event to a list box but for some reason nothing happens when I click an item in my list box.

我正在尝试向列表框中添加单击事件,但由于某种原因,当我单击列表框中的项目时没有任何反应。

My guess and from what I've read, the code should look something like this

我的猜测和我读过的内容,代码应该是这样的

private void listBox1_Click(object sender, EventArgs e)
        {
            //Code Ex. TextBox1.Text = "Success";
    }

That's not working though. Anyone know how to get this to happen? Wouldn't mind knowing the double click and other click variations too....

但这不起作用。有谁知道如何让这种情况发生?也不介意知道双击和其他点击变化......

采纳答案by Haedrian

If you want it to trigger when you 'click an item' in the box, why don't you try SelectedIndexChanged instead?

如果您希望它在您在框中“单击一个项目”时触发,为什么不试试 SelectedIndexChanged 呢?

回答by Tom

Webform? if so, did you check 'autopostback' = true?

网络表格?如果是这样,您是否检查了“autopostback”= true?

Example with selectindexchanged:

selectindexchanged 的​​示例:

<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
    <asp:ListItem>a</asp:ListItem>
    <asp:ListItem>b</asp:ListItem>
</asp:ListBox>

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

That will make a postback as soon as the user clicks on one item. I'm not sure there is a doubleclick for that kind of stuff.

这将在用户单击一个项目后立即进行回发。我不确定那种东西是否有双击。

回答by Sinaesthetic

I'm not sure the click events apply to the listbox items, probably just the parent box. I'd loop through the items and register the events. Or do it on databind.

我不确定点击事件是否适用于列表框项目,可能只是父框。我会遍历项目并注册事件。或者在数据绑定上做。

回答by Bob Provencher

You coded the event handler but didn't add it to the click event... something like this:

您对事件处理程序进行了编码,但没有将其添加到点击事件中……像这样:

listBox1.Click += new EventHandler( listBox1_Click );

回答by Olivier Laforest

Have you checked that the property "AutoPostBack" is enabled (="True")? :-) That happens often to me!

您是否检查过“AutoPostBack”属性是否已启用(=“True”)?:-) 这经常发生在我身上!

回答by Jim Stevens

In Microsoft visual studio 2017 Get your main form ( with the list box ) into Form design view Select your list box item which you have dragged onto the main form Now your properties box for the list box has come live on the right hand side of the development window Now look at the top of the properties box for the menu item with the 'jag of lightning' on it Click on that...Voila your double click event is there

在 Microsoft Visual Studio 2017 中将您的主表单(带有列表框)放入表单设计视图中选择您已拖到主表单上的列表框项目现在您的列表框属性框已在右侧开发窗口 现在查看带有“闪电锯齿”的菜单项的属性框顶部点击那个...瞧你的双击事件在那里

private void lbx _DoubleClick(object sender, EventArgs e)
{
    /*  Do good stuff here  */
}

Cheers me dears Jim

祝我亲爱的吉姆