我有16個項目在listBox1和一個按鈕「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 在此先感謝您的幫助
代碼有什麼問題?它拋出一些錯誤? – Shyju 2012-03-31 00:37:48
另外,當你說「移動」時,你的意思是你想刪除從listboxallmodules中選擇的條目嗎? – Rich 2012-03-31 00:45:00
它拋出錯誤的「項目」,如「錯誤:非可調用成員'System.Windows.Forms.ListBox.Items'不能像方法一樣使用」 – 2012-03-31 00:46:16