【C#】【平时练习】将左边列表框(List)的内容(月份)添加到右边列表框。最终右侧显示的内容(月份)要保持一定顺序...

Aspx - 点击查看代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_Demo01.aspx.cs" Inherits="WebApplication_ListBox.WebForm_Demo01" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        body {
            margin:0;
            padding:0;
            border:0;
            outline:none;
            
        }
        .faraway {
            margin:10px;
            padding:5px;
            
        }
    </style>
    <link href="css/xrilang-locationLayout.css" rel="stylesheet" />
</head>

<body>
    <form id="form1" runat="server" class="center-al">
        <div class="div-block-online">
            <div class="div-block-online border-r  border-BlueBlackish">
            <asp:ListBox ID="lbMonth1" runat="server" Height="200px" Width="100px" SelectionMode="Multiple">
                <asp:ListItem Value="1">January</asp:ListItem>
                <asp:ListItem Value="2">February</asp:ListItem>
                <asp:ListItem Value="3">March</asp:ListItem>
                <asp:ListItem Value="4">April</asp:ListItem>
                <asp:ListItem Value="5">May</asp:ListItem>
                <asp:ListItem Value="6">June</asp:ListItem>
                <asp:ListItem Value="7">July</asp:ListItem>
                <asp:ListItem Value="8">August</asp:ListItem>
                <asp:ListItem Value="9">September</asp:ListItem>
                <asp:ListItem Value="10">October</asp:ListItem>
                <asp:ListItem Value="11">November</asp:ListItem>
                <asp:ListItem Value="12">December</asp:ListItem>
            </asp:ListBox>
                </div>

            <div class="div-block-online" style="height:200px">
                   <div class="faraway">
                    <asp:Button ID="Button1" runat="server" Text="&gt;&gt;&gt;" OnClick="Button1_Click" class="border-Orange"/>
                   </div>
                   <br />
                 <div class="faraway">
                    <asp:Button ID="Button2" runat="server" Text="&lt;&lt;&lt;" OnClick="Button2_Click" class="border-Orange" />
                </div>
            </div>


            <div  class="div-block-online border-r border-BlueBlackish">
             <asp:ListBox ID="lbMonth2" runat="server" Height="200px" SelectionMode="Multiple" style="margin-top: 0px" Width="100px">
            </asp:ListBox>
            </div>
        </div>

        <p>
            <asp:Button ID="btTest" runat="server" Text="查询" OnClick="btTest_Click" class="border-Orange"/>
        </p>
        <div class="div-block border-GreenBlackish">
            <asp:Label ID="lblShow1" runat="server" Text=""></asp:Label>
           </div> 
        
        <div class="div-block border-GreenBlackish">
            <asp:Label ID="lblShow2" runat="server" Text="" ></asp:Label>
        </div>
    </form>
</body>
</html>
cs - 点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication_ListBox
{
    public partial class WebForm_Demo01 : System.Web.UI.Page
    {       
        protected void Page_Load(object sender, EventArgs e)
        {
            //@萌狼蓝天-Test05
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //下面的是老师的方法
            if (lbMonth1.SelectedIndex > -1)
            {
                //把左侧选中项添加到右侧
                //lbMonth2.Items.Add(lbMonth1.SelectedItem);
                //lbMonth2.Items.Insert(0,lbMonth1.SelectedItem);
                //2.1
                //检查右侧所有项,确保插入项的Value按顺序位 int correctIndex
                //2.2 在正确位置插入
                //将排序计算划为单独的方法直接使用,后续直接优化方法算法即可。
                lbMonth2.Items.Insert(OneToTwo(), lbMonth1.SelectedItem);
                //3.在左侧列表框中删除选中项
                lbMonth1.Items.RemoveAt(lbMonth1.SelectedIndex);
            }else {
                lblShow1.Text = "INDEX NOT > 0";
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (lbMonth2.SelectedIndex > -1)
            {
                lbMonth1.Items.Insert(TwoToOne(), lbMonth2.SelectedItem);
                lbMonth2.Items.RemoveAt(lbMonth2.SelectedIndex);
            }
            else
            {
                lblShow1.Text = "INDEX NOT > 0";
            }

        }
        /*
         * 左边往右边添加
         */
        protected int OneToTwo() {
            int correctIndex = 0;
            int valued = Int32.Parse(lbMonth1.SelectedItem.Value);
            if (lbMonth2.Items.Count == 0){
                return 0;
            };
            if (lbMonth2.Items.Count - 1 == 0) {
                if (valued > textToValue(lbMonth2.Items[lbMonth2.Items.Count - 1].Value))
                {
                    return lbMonth2.Items.Count;
                }
                else 
                { 
                    return 0; 
                }
            
            }

            for (int i = 0; i < lbMonth2.Items.Count - 1; i++)
            {
                int valuei = textToValue(lbMonth2.Items[i].Value);
                int valueii = textToValue(lbMonth2.Items[i + 1].Value);
                //不大不小
                if (valued > valuei & valued < valueii && valueii!=0)
                {
                    correctIndex = i + 1;
                    lblShow1.Text = ("[" + i + "]C2(不大不小):" + valuei + "<[" + valued + "]<" + valueii);
                    break;
                }
                //最大,新增
                else if (valued > textToValue(lbMonth2.Items[lbMonth2.Items.Count - 1].Value))
                {

                    lblShow1.Text = ("[" + i + "]C2(最大新增):" + textToValue(lbMonth2.Items[lbMonth2.Items.Count - 1].Value) + "<[" + valued + "]");
                    correctIndex = lbMonth2.Items.Count;
                    break;
                }
                //最小,插入
                else if (valued < textToValue(lbMonth2.Items[0].Value))
                {
                    lblShow1.Text = ("[" + i + "]C2(最小新增):" + valued + "<" + textToValue(lbMonth2.Items[0].Value));
                    correctIndex = 0;
                    break;
                }
                else {
                    lblShow1.Text = "valued:"+valued+" | "+ lbMonth2.Items[lbMonth2.Items.Count - 1].Value;
                }
            }
            
            return correctIndex;
        }
        protected int TwoToOne()
        {
            int correctIndex = 0;
            int valued = Int32.Parse(lbMonth2.SelectedItem.Value);
            if (lbMonth1.Items.Count == 0)
            {
                return 0;
            };
            if (lbMonth1.Items.Count - 1 == 0)
            {
                if (valued > textToValue(lbMonth1.Items[lbMonth1.Items.Count - 1].Value))
                {
                    return lbMonth1.Items.Count;
                }
                else
                {
                    return 0;
                }

            }
            for (int i = 0; i < lbMonth1.Items.Count - 1; i++)
                {
                    int valuei = textToValue(lbMonth1.Items[i].Value);
                    int valueii = textToValue(lbMonth1.Items[i + 1].Value);
                    //不大不小
                    if (valued > valuei & valued < valueii)
                    {
                        correctIndex = i + 1;
                        lblShow1.Text = ("[" + i + "]C2(不大不小):" + valuei + "<[" + valued + "]<" + valueii);
                        break;
                    }
                    //最大,新增
                    else if (valued > textToValue(lbMonth1.Items[lbMonth1.Items.Count - 1].Value))
                    {
                        lblShow1.Text = ("[" + i + "]C2(最大新增):" + textToValue(lbMonth1.Items[lbMonth1.Items.Count - 1].Value) + "<" + valued);
                        correctIndex = lbMonth1.Items.Count;
                        break;
                    }
                    //最小,插入
                    else if (valued < textToValue(lbMonth1.Items[0].Value))
                    {
                        lblShow1.Text = ("[" + i + "]C2(最小新增):" + valued + "<" + textToValue(lbMonth1.Items[0].Value));
                        correctIndex = 0;
                        break;
                    }
                }
            
            return correctIndex;
        }
            /**
            * 将月份转为数值,方便进行比对排序
            */
            protected int textToValue(String item)
        {
            lblShow2.Text = "方法被调用:" + item;
            if (item == "1")
            {
                return 1;
            }
            else if (item == "2")
            {
                return 2;
            }
            else if (item == "3")
            {
                return 3;
            }
            else if (item == "4")
            {
                return 4;
            }
            else if (item == "5")
            {
                return 5;
            }
            else if (item == "6")
            {
                return 6;
            }
            else if (item == "7")
            {
                return 7;
            }
            else if (item == "8")
            {
                return 8;
            }
            else if (item == "9")
            {
                return 9;
            }
            else if (item == "10")
            {
                return 10;
            }
            else if (item == "11")
            {
                return 11;
            }
            else if (item == "12")
            {
                return 12;
            }
            return 0;


        }
        /**
         * 测试专用 
         */
        protected void btTest_Click(object sender, EventArgs e)
        {

            lblShow1.Text = "[box1]" + "<br>" +
                "[当前选择索引]:" + lbMonth1.SelectedIndex + "<br>" +
                "[当前选择Value]:" + lbMonth1.SelectedValue + "<br>" +
                "[当前选择item]" + lbMonth1.SelectedItem + "<br>" +
                "[当前选择Mode]:" + lbMonth1.SelectionMode + "<br>"+
                "[Count]:" + lbMonth1.Items.Count + "<br>";
            lblShow2.Text = "[box2]" + "<br>" +
                "[当前选择索引]:" + lbMonth2.SelectedIndex + "<br>" +
                "[当前选择Value]:" + lbMonth2.SelectedValue + "<br>" +
                "[当前选择item]" + lbMonth2.SelectedItem + "<br>" +
                "[当前选择Mode]:" + lbMonth2.SelectionMode + "<br>"+
                "[Count]:" + lbMonth2.Items.Count + "<br>";

        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌狼蓝天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值