簡體   English   中英

用C#編寫一個雙擊事件

[英]Coding a double click event in C#

如果我想創建雙擊甚至從列表框和消息框中選擇一個項目,並帶有刪除所選項目的選項,我將如何編碼?

只需將這段代碼放在ListBox DoubleClick事件處理程序中即可。 (讓您的列表框ID為“ ListBox1”

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

我做了這樣的事情。 幾乎雙擊可以從一個文本框添加到另一個文本框,然后雙擊另一個將其刪除。 其實在這里! 獲取整個源代碼。 進行一個新項目,將其粘貼在那里並運行它。 它應該正是您想要的。 請記住,您不能只更改正面的值,因為背面永遠不會看到它,因此我們將這些值存儲在一個隱藏變量中,然后在背面填充它。

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

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