如何避免将重复的项目从一个文本框中输入到另一个文本框中 [英] How to avoid entering duplicate item from one textbox to other

查看:97
本文介绍了如何避免将重复的项目从一个文本框中输入到另一个文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有两个列表框,所以当我选择一个项目并单击按钮时,列表框1中的项目将移至列表框2.完成了,但是如果移动一个项目后列表框1中存在类似的项目,则不允许移动重复的项目,而是显示已经可用的消息项.如何操作.


我的以下代码


Hi all,



I have two listboxs so when i select one item and click button then the items from listbox1 will move to listbox2.It is done but if there are similar items in listbox1 after moving one item it should not allow to move the duplicate items rather it will show a message item already available.How to do it.


My Code below


private void btnright_Click(object sender, EventArgs e)
        {

            listBox2.Items.Add(listBox1.SelectedItem);
            int i = 0;
            i = listBox1.SelectedIndex;
            listBox1.Items.RemoveAt(i);

        }


        private void btnleft_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(listBox2.SelectedItem);
            int i = 0;
            i = listBox2.SelectedIndex;
            listBox2.Items.RemoveAt(i);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("a");
            listBox1.Items.Add("a");
            listBox1.Items.Add("b");
            listBox1.Items.Add("c");
            listBox1.Items.Add("d");
            listBox1.Items.Add("e");
            listBox1.Items.Add("f");


        }





谢谢





Thanks

推荐答案

根据您的示例,您的问题没有任何实际意义.您说如果某个对象已经在列表框中,则不能将其添加到第二个列表框中,但是,第一个列表框中不允许该项目包含多个.这是无稽之谈.因为您是在暗示"a"的两个实例在一个列表框中相同,而在另一个列表框中却是不同的.
Your question makes no real sense based on your sample. You say an object can''t be added to the 2nd listbox if it''s already IN the listbox, yet, more than one of that item IS allowed in the first listbox. This is nonsense. Because you''re implying that the two instances of "a" are the same in one listbox, but DIFFERENT in the other.


您可以在btnRight
上使用以下代码
You can use the following code on btnRight
if (listBox2.Items.Contains(listBox1.SelectedItem)== true)
          {
              MessageBox.Show("Duplicate Value");
          }
          else
          {
              listBox2.Items.Add(listBox1.SelectedItem);
              i = 0;
              i = listBox1.SelectedIndex;
              listBox1.Items.RemoveAt(i);

          }


希望对您有帮助. :)

-MKB


I hope it will help you. :)

-MKB


这篇关于如何避免将重复的项目从一个文本框中输入到另一个文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆