将项目从一个列表框转移到另一个列表框 [英] Transferring item from one listbox to another

查看:93
本文介绍了将项目从一个列表框转移到另一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想将选定的(多个)项目从一个ListBox添加到另一个.我已经完成了以下代码,但没有得到我想要的:

Hi all,

I want to add selected(more than one) items from one ListBox to another. I have done the following code but I''m not getting exactly what I want:

protected void btn1_Click(object sender, EventArgs e)
{
    if (lst_employee.SelectedIndex > -1)
    {
        string value = lst_employee.SelectedItem.Value;
        string text = lst_employee.SelectedItem.Text;
        ListItem li = new ListItem();
        li.Text = text;
        li.Value = value;
        lstselected.Items.Add(li);
        lst_employee.Items.Remove(li);
    }
}
protected void btn2_Click(object sender, EventArgs e)
{
    if (lstselected.SelectedIndex > -1)
    {
        string value = lstselected.SelectedItem.Value;
        string text = lstselected.SelectedItem.Text;
        ListItem li1 = new ListItem();
        li1.Value = value;
        li1.Text = text;
        lstselected.Items.Remove(li1);
        lst_employee.Items.Add(li1);
    }
}
protected void btnall_Click(object sender, EventArgs e)
{
    int count = lst_employee.Items.Count;
    if (count != 0)
    {
        for (int i = 0; i < count; i++)
        {
            ListItem item = new ListItem();
            item.Text = lst_employee.Items[i].Text;
            item.Value = lst_employee.Items[i].Value;
            lstselected.Items.Add(item);
        }
    }
    lst_employee.Items.Clear();
}
protected void btnremove_Click(object sender, EventArgs e)
{
    int count = lstselected.Items.Count;
    if (count != 0)
    {
        for (int i = 0; i < count; i++)
        {
            ListItem item = new ListItem();
            item.Text = lstselected.Items[i].Text;
            item.Value = lstselected.Items[i].Value;
            lst_employee.Items.Add(item);
        }
    }
    lstselected.Items.Clear();
}


我希望将一个以上的选定项目添加到一个ListBox并将其从另一个项目中删除.

希望你能帮助我.
在此先感谢您.


I want more than one selected items to be added and removed from one ListBox to another.

Hope you will help me.
Thanks in advance.

推荐答案

根据您的帖子很难知道,它没有说您希望它做些什么,但是我想您想遍历选定的项目集合,以便所有选定的项目都得到移动.
Hard to know based on your post, which does not say what it is you want it to do extra, but I''d guess that you want to iterate over the selected items collection so that all the items you selected get moved.


您正在使用ListBox -es的SelectedIndex属性.
对于单选ListBox,这很好,但对于多选ListBox,则必须使用SelectedIndices和/或SelectedItems属性.
查看 ListBox.SelectedIndex [ ^ ]并阅读备注部分.在这里,您会找到有用的信息和链接.

:)
You are using SelectedIndex property of your ListBox-es.
For single-selection ListBox this is fine but for multi-selection ListBox you have to use SelectedIndices and/or SelectedItems properties.
Check out the documentation of ListBox.SelectedIndex[^] and read the remarks section. There you will find useful information and links.

:)


这篇关于将项目从一个列表框转移到另一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆