【发布时间】:2012-03-31 00:34:50
【问题描述】:
我在 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 在此先感谢您的帮助
【问题讨论】:
-
代码有什么问题?它会抛出一些错误吗?
-
另外,当您说“移动”时,您的意思是要删除从 listboxallmodules 中选择的条目吗?
-
它在“项目”上抛出错误,例如“错误:不可调用的成员 'System.Windows.Forms.ListBox.Items' 不能像方法一样使用”
-
不,我希望它留在 listBoxAllModules 中,当我按下按钮时,它也应该出现在 ListboxStudentmodules 中
标签: c#