English 中文(简体)
C# 中双点击事件编码
原标题:Coding a double click event in C#
  • 时间:2012-05-24 22:46:44
  •  标签:
  • c#

如果我想创建双击, 甚至从列表框和信件框中选择一个项目, 并选择删除选中的项目, 我将如何编码?

问题回答

只需将此代码放在您的 DoubleClick 事件处理器 ListBox 事件处理器中。 (请将您的列表框 id 设为 < strong> "ListBox1 > )

MessageBox.Show(ListBox1.SelectedItem.ToString());

我做了类似的事情。 相当多的双击可以将一个文本框添加到另一个文本框中, 并且将另一个文本框中去掉。 事实上, 这里! 请选择整个源代码 。 使用整个源代码 。 使用一个新工程, 将它粘在那里, 然后运行它 。 它应该是您想要的 。 记住, 您不能仅仅改变前侧的值, 因为后侧看不到它, 因此我们把数值存储在一个隐藏的变量中, 然后将它埋在后侧 。

ListBoxEvents.aspx (note: ommited header)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            //Listbox: ORIGINAL - Controls
            var removeOriginal = false;
            var lbOriginal =  ListBox1 ;
            var lbDestination =  ListBox2 
            var lineHeight = 12;
            var lbElementsHeight = (($( [id*=  + lbOriginal +  ] ).find("option").length) * lineHeight) +  px ;
            //var lh = $( [id*=  + lbOriginal +  ] ).css( line-height );

            //Normalize Width
            $( [id*=  + lbDestination +  ] ).width($( [id*=  + lbOriginal +  ] ).width());
            $( [id*=  + lbOriginal +  ] ).dblclick(function () {
                //Get name and value of a selected listbox item
                var itemName = "";
                var itemValue = "";
                $( [id*=  + lbOriginal +  ] option:selected ).each(function () {
                    itemName += $(this).text();
                    itemValue += $(this).val();
                });
                //If user doubleclicked on empty spot, return
                if (itemName == "" && itemValue == "")
                    return;
                //Prevent duplicate appends
                var itemAlreadyExists = false
                $( [id*=  + lbDestination +  ] option ).each(function () {
                    if ($(this).text() == itemName && $(this).val() == itemValue)
                        itemAlreadyExists = true;
                });
                if (!itemAlreadyExists) {
                    $( [id*=  + lbDestination +  ] ).append( <option value="  + itemValue +  ">  + itemName +  </option> );
                    //Select the last element in the destination listbox
                    $( [id*=  + lbDestination +  ] option[selected] ).removeAttr("selected");
                    $( [id*=  + lbDestination + "] option[value= " + itemValue + " ]").attr("selected", "selected");
                    $( [id*=  + lbDestination +  ] ).animate({ scrollTop: lbElementsHeight }, 800);
                    AddItem(itemName, itemValue);
                }
                if (removeOriginal)
                    $( [id*=  + lbOriginal + "] option[value= " + itemValue + " ]").remove();
            }).trigger( change );

            //Listbox: DESTINATION - Controls
            $( [id*=  + lbDestination +  ] ).dblclick(function () {
                //Get name and value of a selected listbox item
                var itemName = "";
                var itemValue = "";
                $( [id*=  + lbDestination +  ] option:selected ).each(function () {
                    itemName += $(this).text();
                    itemValue += $(this).val();
                });
                //If user doubleclicked on empty spot, return
                if (itemName == "" && itemValue == "")
                    return;
                //If we have removed the value from the original listbox, return it
                if (removeOriginal)
                    $( [id*=  + lbOriginal +  ] ).append( <option value="  + itemValue +  ">  + itemName +  </option> );
                //Remove the value from this listbox
                $( [id*=  + lbDestination + "] option[value= " + itemValue + " ]").remove();
                RemoveItem(itemName, itemValue);
            }).trigger( change );

            function AddItem(itemName, itemValue) {
                var hfLB2Items = $( [id*=hfLB2Items] );
                var item = itemName +  ~  + itemValue;
                if (hfLB2Items.val() != "")
                    item =  |  + item;
                hfLB2Items.val(hfLB2Items.val() + item);
            }

            function RemoveItem(itemName, itemValue) {
                var hfLB2Items = $( [id*=hfLB2Items] );
                var item = itemName +  ~  + itemValue;
                if (hfLB2Items.val().indexOf( |  + item) != -1)
                    hfLB2Items.val(hfLB2Items.val().replace( |  + item,   ));
                else if (hfLB2Items.val().indexOf(item +  | ) != -1)
                    hfLB2Items.val(hfLB2Items.val().replace(item +  | ,   ));
                else
                    hfLB2Items.val(hfLB2Items.val().replace(item,   ));
            }
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="ListBox1" runat="server" Width="80" Height="150" style="float:left"> 
                <asp:ListItem Value="one">1</asp:ListItem> 
                <asp:ListItem Value="two">2</asp:ListItem> 
                <asp:ListItem Value="three">3</asp:ListItem>
                <asp:ListItem Value="four">4</asp:ListItem>
                <asp:ListItem Value="five">5</asp:ListItem>
                <asp:ListItem Value="six">6</asp:ListItem>
                <asp:ListItem Value="seven">7</asp:ListItem>
                <asp:ListItem Value="eight">8</asp:ListItem>
                <asp:ListItem Value="nine">9</asp:ListItem>
                <asp:ListItem Value="ten">10</asp:ListItem> 
                <asp:ListItem Value="eleven">11</asp:ListItem> 
                <asp:ListItem Value="twelve">12</asp:ListItem>
                <asp:ListItem Value="thirteen">13</asp:ListItem>
                <asp:ListItem Value="fourteen">14</asp:ListItem>
                <asp:ListItem Value="fifteen">15</asp:ListItem>
                <asp:ListItem Value="sixteen">16</asp:ListItem>
                <asp:ListItem Value="seventeen">17</asp:ListItem>
                <asp:ListItem Value="eighteen">18</asp:ListItem>
            </asp:ListBox>
            <asp:ListBox ID="ListBox2" runat="server" Width="80" Height="150" style="float:left"></asp:ListBox>
        </div>
        <div style="clear:both"></div>
        <asp:TextBox ID="tbCount" runat="server" Enabled="False"></asp:TextBox><br />
        <asp:Button ID="btnCount" runat="server" onclick="btnCount_Click" Text="ListBox 2 Item Count" />
        <asp:HiddenField ID="hfLB2Items" runat="server" />
        <div>
            <h2>Double Click Event</h2><hr />

            <p id="MyPara" style="background-color:Yellow;color:Red;font-size:2.2em;">
                Double Click here to display alert
            </p>
            <br />
            <br />

            <script language="javascript" type="text/javascript">
                $(document).ready(function () {

                    $("#MyPara").dblclick(
                        function () {
                            ShowAlert();
                        }
                    );

                });

                function ShowAlert() {
                    alert("Alert message on double click");
                }
            </script>
        </div>
    </form>
</body>
</html>

列表框events.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ListBoxEvents : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCount_Click(object sender, EventArgs e)
    {
        PopulateDestinationListBox();
        int itemCount = ListBox2.Items.Count;
        tbCount.Text = itemCount.ToString();
    }
    protected void lbCount_Click(object sender, EventArgs e)
    {
        int itemCount = ListBox2.Items.Count;
        tbCount.Text = itemCount.ToString();
    }

    private void PopulateDestinationListBox()
    {
        ListBox2.Items.Clear();
        string[] pairs = hfLB2Items.Value.Split( | );
        if (pairs.Length == 0 && string.IsNullOrEmpty(pairs[0]))
            return;
        foreach (string pair in pairs)
        {
            string[] values = pair.Split( ~ );
            ListBox2.Items.Add(new ListItem(values[0], values[1]));
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...