0

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

4

2 回答 2

0

只需将此代码放在DoubleClick您的事件处理程序中ListBox。(让您的列表框 id 为"ListBox1"

MessageBox.Show(ListBox1.SelectedItem.ToString());
于 2012-05-24T22:58:06.547 回答
0

我做了这样的事情。几乎可以双击从一个文本框添加到另一个文本框,然后双击另一个文本框将其删除。其实这里!获取整个源代码。鬃毛一个新项目,把它放在那里,然后运行它。它应该正是你想要的。请记住,您不能只更改正面的值,因为背面永远看不到它,因此我们将值存储在隐藏变量中,然后将其填充到背面。

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>

列表框事件.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]));
        }
    }
}
于 2012-05-24T23:06:44.033 回答