【问题标题】:how to add selected item from one listBox to another listBox如何将所选项目从一个列表框添加到另一个列表框
【发布时间】: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#


【解决方案1】:

您可以明确说明发生了什么:

 string value = listBoxAllModules.SelectedItem.Value; 
 string text = listBoxAllModules.SelectedItem.Text;  
 ListItem item = new ListItem (); 
 item.Text = text;                
 item.Value = value;
 listBoxStudentModules.Items.Add(item); 

【讨论】:

  • 我不想从 listBox1 中删除选定的项目,只需将其移动到 listBox2,同时将其保留在 listBox1 中
  • 我将其更改为不再删除。
  • 完成了伙计们.. 我复制了按钮 1 的按钮名称,例如“button1_Click”和“button1_Click_1”,我想将代码添加到后者。不是第一个。感谢您的帮助
【解决方案2】:

listBoxAllModules.Items 是一个ListBox.ObjectCollection。您正在尝试将其用作方法:

listBoxAllModules.Items( listBoxAllModules.SelectedItem )

这行不通。您错过了Add 电话。应该是.Items.Add()。您应该可以添加 SelectedItem,如 TechnologRich 所示:

listBoxStudentModules.Items.Add(listBoxAllModules.SelectedItem);

【讨论】:

  • 这就是为什么我最后有 .ToString() 。它可以通过 Microsoft 网站运行。
  • 字符串 curItem = listBox1.SelectedItem.ToString(); >>msdn.microsoft.com/en-us/library/…
  • 实际上你不需要那个。 ObjectCollection.Items.Add 采用 Object 和 SelectedItem 是 Object。
  • 你能检查一下我是否把button1_Click的方法放在正确的地方-更新我的代码-
  • 完成了伙计们.. 我复制了按钮 1 的按钮名称,例如“button1_Click”和“button1_Click_1”,我想将代码添加到后者。不是第一个。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 2012-10-14
  • 2016-09-23
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多