【问题标题】:C# Windows From add/remove list [duplicate]C# Windows 从添加/删除列表 [重复]
【发布时间】:2016-02-19 12:30:25
【问题描述】:

我需要在 C# Windows 窗体中添加一个“slushbucket”(ServiceNow 术语),如下图所示,但无法弄清楚它们的实际名称,因此无法查看它们是如何创建的.

有谁知道它们叫什么,或者更好的是如何将其实现到 Windows 窗体中?

我需要它在左侧列出 Table1 中的值,然后将它们添加到右侧列表并单击保存按钮时,将值写入 Table2。如果有的话,它还需要显示 Table2 的现有值。

【问题讨论】:

  • 只需使用两个 ListBox 或两个 ListView 和一些按钮自己编写代码。
  • 创建一个新表单,向其中添加 2 个列表框以及添加和删除按钮。尝试进行一些编码(填写左侧列表框,...),如果您有具体问题,可以再次在这里提问。
  • 写起来非常简单,按钮处理程序只需从框中取出选定的值并将它们添加到另一边
  • 好吧,你已经有了原型。应该很容易将这些控件放在表单上,​​单击几次以连接事件处理程序,并编写代码来添加/删除项目。我们不会为你写的。

标签: c# winforms


【解决方案1】:

假设表单是这样的: Form1.Png 这是一个实现您的想法的简单代码,如下所示:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        for (int i = 6; i <= 10; i++)
        {
            listBox1.Items.Add("item" + i);
        }
        for (int i = 1; i <= 5; i++)
        {             
            listBox2.Items.Add("item" + i);
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {` 

        if (listBox2.Items.Count >0)
        {
            string message = "";
            foreach (var item in listBox2.Items)
            {
                message += item + "\n";
            }
            MessageBox.Show(message);
        }`
        /// write here the code that save listbox1 and listbox2 items
        /// to a text file or a database table and load this text file or 
        /// the database table inside these two listboxes you could write the 
        /// code that load data to the two list boxes in the constructor instead
        /// of the tow for loops thats i've provided.
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (!listBox2.Items.Contains(listBox1.SelectedItem))
            {
                listBox2.Items.Add(listBox1.SelectedItem);
                listBox1.Items.Remove(listBox1.SelectedItem);
            }
            else
            {
                MessageBox.Show("Item already exists");
            }
        }
        catch (ArgumentNullException exc)
        {
            MessageBox.Show("Nothing selected to add");

        }         
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {
        try
        {
            if (!listBox1.Items.Contains(listBox2.SelectedItem))
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                listBox2.Items.Remove(listBox2.SelectedItem);
            }
            else
            {
                MessageBox.Show("Item already exists");
            }

        }
        catch (ArgumentNullException exc)
        {

            MessageBox.Show("Nothing selected to remove");
        }

    }
}

希望这就是您想要的 :) 谢谢。

【讨论】:

    【解决方案2】:

    这是图像输出:

    您的代码如下。我希望这对你有帮助。

    public partial class Form1 : Form
    {
        ArrayList Table1;
        ArrayList Table2;
        bool isSaved = false;
        public Form1()
        {
            InitializeComponent();
            Table1 = new ArrayList();
            Table2 = new ArrayList();
    
            Table1.Add("Value1");
            Table1.Add("Value2");
            Table1.Add("Value3");
            Table1.Add("Value4");
            Table1.Add("Value5");           
        }
        private void populateListBox1() {
            for (int i = 0; i < Table1.Count;i++)
            {
                listBox1.Items.Add(Table1[i]);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            populateListBox1();          
        }
        private void refreshListBox1() {
            listBox1.Items.Clear();
            populateListBox1();  
        }
        private void addToRight() {
            try
            {
                listBox2.Items.Add(Table1[listBox1.SelectedIndex]);
                Table1.RemoveAt(listBox1.SelectedIndex);
            }
            catch 
            {
                MessageBox.Show("You have to select an item first !");
            }
    
            refreshListBox1();
        }
        private void removeFromRight(){
            if (isSaved)
            {
                try
                {
                    listBox1.Items.Add(Table2[listBox2.SelectedIndex]);
                    Table1.Add(Table2[listBox2.SelectedIndex]);
                    isSaved = false;
                }
                catch 
                {
                    MessageBox.Show("You have to select an item first !");
                    isSaved = true;
                }
                try
                {
                    Table2.RemoveAt(listBox2.SelectedIndex);
                    listBox2.Items.RemoveAt(listBox2.SelectedIndex);
                    isSaved = false;
                }
                catch 
                {
                    if(listBox2.Items.Count==0)
                    MessageBox.Show("This list is an empty list");
                    isSaved = true;
                }
    
            }
            else {
                MessageBox.Show("You have to click save button first !");
            }
    
        }
        private void saveToTable2() {
            for (int i = 0; i < listBox2.Items.Count;i++)
            {
                Table2.Add(listBox2.Items[i].ToString());
            }
            MessageBox.Show("Saved !");
            isSaved=true;           
        }
        private void btn_add_Click(object sender, EventArgs e)
        {
            addToRight();
        }
    
        private void btn_save_Click(object sender, EventArgs e)
        {
            saveToTable2();           
        }
    
        private void btn_remove_Click(object sender, EventArgs e)
        {
            removeFromRight();
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 2020-09-12
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 2013-09-19
      • 2017-10-12
      • 1970-01-01
      • 2014-09-30
      相关资源
      最近更新 更多