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

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

q1.png

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>";
        }
    }
}
KOTLIN 折叠 复制 全屏


相关文章
|
19天前
|
安全 C#
C# List基本用法
C# List基本用法
C# 找出泛型集合中的满足一定条件的元素 List.Wher()
在学习的过程中,发现泛型集合List有一个Where函数可以筛选出满足一定条件的元素,结合Lambda表达式使用特别方便,写出来与大家分享。 1.关于Func Func是一种有任意个输入参数,有一个返回值的委托,在使用的过程中,Func,前n-1个是输入参数类型,第N个是输出参数类型。
2014 0
|
4月前
|
Scala
Scala综合练习_基于以下List集合实现词频统计
Scala综合练习_基于以下List集合实现词频统计
21 0
|
4月前
|
存储 C# 索引
C# | 比较IEnumerable、List、数组
IEnumerable`定义了一组用于枚举集合的方法,包括`GetEnumerator`方法,该方法返回一个实现了`IEnumerator`接口的对象,用于枚举集合中的每个元素。`List`和数组都可以使用`foreach`循环来遍历其中的元素,这是因为它们都实现了`IEnumerable`接口。 由于数组在内存中开辟了一段连续的空间,因此可以直接通过索引访问元素,访问速度很快。而 List 则需要通过指针或引用来访问元素,速度相对较慢。 由于数组的大小是固定的,当需要添加或删除元素时,需要重新创建一个新数组,将原数组中的元素复制到新数组中,并添加或删除元素。
59 0
C# | 比较IEnumerable、List、数组
|
9月前
|
C#
C#List与ArrayList,Hashtable与Dictionary总结
C#List与ArrayList,Hashtable与Dictionary总结
35 0
C#List与ArrayList,Hashtable与Dictionary总结
|
9月前
|
安全 C# 索引
C# 泛型集合和非泛型集合(List ArrayLIst)
C# 泛型集合和非泛型集合(List ArrayLIst)
60 0
|
9月前
|
存储 安全 C#
C#里面的不同集合(数组、ArrayList集合、List泛型)
在内存中连续存储,因此可以快速而容易地从头到尾遍历元素,可以快速地修改元素
C#合并两个List并删除重复项
C#合并两个List并删除重复项
|
SQL 存储 开发框架
C#——List排序
C#——List排序
121 0