如何将值从一个列表框移动到另一个列表框 [英] How to move values from one listbox to another listbox

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

问题描述

我知道如何将值从一个列表框转移或传递给另一个列表框

但是我不知道如何在没有选择的情况下转移它



我尝试过:



listBox2.Items.Add(listBox1.SelectedItem);

listBox1.Items.Remove(listBox1.SelectedItem);

这个我累了,但我选择了项目,但我想不选择,转移它们

i know how to transfer or pass value from one Listbox to Another Listbox
but i Don't know how transfer it without selection

What I have tried:

listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
this i tired but i select the items but i want to without selecting, transfer them

推荐答案

SelectedItem属性只返回当前选中的项目 - 但您已经在使用所需的属性:ListBox.Items

您所要做的就是决定哪个项目( s)你想要的,并通过Items属性直接访问它们:

The SelectedItem property just returns the currently selected item - but you are already using the property you need: ListBox.Items
All you have to do is decide which item(s) you want, and access them directly via the Items property:
listBox2.Items.Add(listBox1.Items[indexOfItemIWant]);
listBox1.Items.Remove(listBox1.Items[indexOfItemIWant]);


它基本相同,但你只需要定义项目的索引你想修改。



考虑以下示例

It's basically the same, but you just define the index of the item you want to modify.

Consider the following example
ListBox lb1 = new ListBox();
ListBox lb2 = new ListBox();

lb1.Items.Add("first");
lb1.Items.Add("second");
lb1.Items.Add("third");

// move the second item
lb2.Items.Add(lb1.Items[1]);
lb1.Items.RemoveAt(1);



[已添加]



移动多件物品的示例


[ADDED]

Example of moving several items

// move two items items 2 and 0
// remember that when using indexes loop from biggest to smallest
foreach (int index in new int[] { 2, 0 }) {
   lb2.Items.Add(lb1.Items[index]);
   lb1.Items.RemoveAt(index);
}



[已添加]

由于你使用多选列表作为源,你可以遍历选定的项目。例如


[ADDED]
Since you're using multiselect list as source, you can loop through the selected items. For example

ListBox lb1 = new ListBox();
ListBox lb2 = new ListBox();

lb1.Items.Add("first");
lb1.Items.Add("second");
lb1.Items.Add("third");

// select items 0 and 2
lb1.SelectionMode = SelectionMode.MultiSimple;
lb1.SelectedItems.Add(lb1.Items[0]);
lb1.SelectedItems.Add(lb1.Items[2]);

// move the selected items
for (int counter = lb1.SelectedItems.Count - 1; counter >= 0; counter-- ) {
   lb2.Items.Add(lb1.SelectedItems[counter]);
   lb1.Items.Remove(lb1.SelectedItems[counter]);
}


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

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