简体   繁体   English

用C#编写一个双击事件

[英]Coding a double click event in C#

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

Just put this code in DoubleClick event handler of your ListBox . 只需将这段代码放在ListBox DoubleClick事件处理程序中即可。 (let your listbox id is "ListBox1" ) (让您的列表框ID为“ ListBox1”

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

I did something like this. 我做了这样的事情。 Pretty much double click to add from one textbox to another, and double clicking from the other to remove it. 几乎双击可以从一个文本框添加到另一个文本框,然后双击另一个将其删除。 In fact, here! 其实在这里! Take the entire source code. 获取整个源代码。 Mane a new project, stick this in there, and run it. 进行一个新项目,将其粘贴在那里并运行它。 It should be exactly what you want. 它应该正是您想要的。 Remember, you can't just change the values on a frontside because backside will never see it, so we store the values in a hidden variable and then populate it on the back side. 请记住,您不能只更改正面的值,因为背面永远不会看到它,因此我们将这些值存储在一个隐藏变量中,然后在背面填充它。

ListBoxEvents.aspx (note: ommited header) ListBoxEvents.aspx(注意:省略标题)

<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>

ListBoxEvents.aspx.cs ListBoxEvents.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]));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM