【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>";

        }
    }
}

posted @ 2021-12-21 11:35  萌狼蓝天  阅读(38)  评论(0编辑  收藏  举报