如何對ListBox實(shí)現(xiàn)添加移除操作-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)如何對ListBox實(shí)現(xiàn)添加移除操作,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

公司主營業(yè)務(wù):成都網(wǎng)站制作、做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出和田縣免費(fèi)做網(wǎng)站回饋大家。

前臺代碼:


代碼如下:


<div>
<asp:ListBox ID="ListBox1" runat="server" Height="123px" Width="113px" SelectionMode="Multiple">
<asp:ListItem>tom</asp:ListItem>
<asp:ListItem>jion</asp:ListItem>
<asp:ListItem>j</asp:ListItem>
<asp:ListItem>l</asp:ListItem>
<asp:ListItem>k</asp:ListItem>
</asp:ListBox>
&nbsp;<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="添加" />
&nbsp;
<asp:Button ID="btnRemove" runat="server" Text="移除" OnClick="btnRemove_Click" />
&nbsp;<asp:ListBox ID="ListBox2" runat="server" Height="123px" SelectionMode="Multiple" Width="113px"></asp:ListBox>
</div>



后臺代碼:


代碼如下:


protected void btnAdd_Click(object sender, EventArgs e)
{
#region listbox添加記錄的一種錯(cuò)誤理解
//選擇多條記錄的時(shí)候,會有一條沒有被添加,這是因?yàn)楫?dāng)一條記錄被移除后,原來的第二條記錄的index為0
//for (int i = 0; i < ListBox1.Items.Count; i++)
//{
// if (ListBox1.Items[i].Selected == true)
// {
// ListBox2.Items.Add(ListBox1.SelectedValue);
// ListBox1.Items.Remove(ListBox1.SelectedValue);
// }
//}
#endregion
#region listbox利用index索引號進(jìn)行添加的簡單寫法
//while (0 <= ListBox1.SelectedIndex)
//{
// ListBox2.Items.Add(ListBox1.SelectedItem);
// ListBox1.Items.Remove(ListBox1.SelectedItem);
//}
#endregion
#region listbox的另一種成功添加方法
List<ListItem> list = new List<ListItem>();
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
list.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
for (int i = 0; i <=list.Count - 1; i++)
{
ListBox2.Items.Add(list[i]);
}
#endregion
}
protected void btnRemove_Click(object sender, EventArgs e)
{
while (0 <= ListBox2.SelectedIndex)
{
ListBox1.Items.Add(ListBox2.SelectedItem);
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}


關(guān)于“如何對ListBox實(shí)現(xiàn)添加移除操作”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

當(dāng)前題目:如何對ListBox實(shí)現(xiàn)添加移除操作-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://muchs.cn/article22/dieicc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站建設(shè)、服務(wù)器托管做網(wǎng)站、移動網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)