![](/img/trans.png)
[英]How to move multiple selected item from one listbox to another listbox?
[英]how to add selected item from one listBox to another listBox
我有listBox1中的16个项目和一个按钮“button1”,我需要能够在按下按钮时将所选项目从listBox1移动到listBox2。 目前我的代码是
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace courseworkmodule
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
String workingDir = Directory.GetCurrentDirectory();
XmlTextReader textReader = new XmlTextReader(workingDir + @"\modules.xml");
Console.WriteLine("BaseURI:" + textReader.BaseURI);
textReader.Read();
while (textReader.Read())
{
textReader.MoveToElement();
if (textReader.Name == "Name")
{
textReader.Read();
XmlNodeType nType = textReader.NodeType;
if (nType == XmlNodeType.Text)
{
listBoxAllModules.Items.Add(textReader.Value);
}
}
}
Console.ReadLine();
textReader.Close();
}
public void button1_Click(object sender, EventArgs e)
{
listBoxStudentModules.Items.Add(listBoxAllModules.SelectedItem);
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
private void listBoxAllModules_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
listBoxAllModule是listBox1,listBoxStudentModule是listBox2,提前感谢任何帮助
你可以明确地看看发生了什么:
string value = listBoxAllModules.SelectedItem.Value;
string text = listBoxAllModules.SelectedItem.Text;
ListItem item = new ListItem ();
item.Text = text;
item.Value = value;
listBoxStudentModules.Items.Add(item);
listBoxAllModules.Items
是一个ListBox.ObjectCollection
。 您正在尝试将其用作方法:
listBoxAllModules。 项目( listBoxAllModules.SelectedItem )
这不行。 您缺少Add
呼叫。 应该是.Items.Add()
。 你应该能够像TechnologRich显示的那样添加SelectedItem
:
listBoxStudentModules.Items.Add(listBoxAllModules.SelectedItem);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.