如何检查列表中是否已存在雇主,并在文本框中输入相同的代码 [英] how to Check if already exists a employer in the list with same code entered in textbox

查看:86
本文介绍了如何检查列表中是否已存在雇主,并在文本框中输入相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我需要你的帮助,如果可能的话。



我需要检查列表中是否已存在雇主,并在文本框中输入相同的代码。



我有雇主列表,我添加这样的雇主:

  else   if (cb_type.Text == type1 .text)
{

type1 typeone = new typeone(Convert.ToInt32(tb_Code.Text),tb_Name.Text,Convert .ToDateTime(dateTimePicker1.Text),cb_Sex.Text,tb_Adress.Text,Convert.ToInt32(tb_Phone.Text),tb_Email.Text,pictureBox1.Image);
LEmployer.Add(typeone);
listBox1.Items.Add(V.getName());
listBox1.Show();
}



在我添加新雇主的那一刻,我需要验证是否已存在使用与文本框中输入相同代码的雇主。 />


我试试这个:

  foreach (雇主E  in  Lemployer)
{
if tb_code.text == E.getCode())
{
Messagebox.show( 代码已存在);
}
}



但这不起作用,有人可以帮我这样做吗?



谢谢

解决方案

如果你从不想要重复,你最好使用一个不接受重复的集合,而不是列表,比如:

http://msdn.microsoft.com/en-us/library /bb359438.aspx [ ^ ],

http://msdn.microsoft.com/en-us /library/xfhwa508.aspx [ ^ ],

http://msdn.microsoft.com/en -us / library / f7fta44c.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ms132319.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dd412070.aspx [ ^ ]。



-SA


但如果我使用的是排序列表,我需要更改所有代码吗? :/

Hello,

I need your help, if it is possibe.

I need to check if already exists a employer in the list with same code entered in the textbox.

I have a list of employers, and I add employers like this :

else if (cb_type.Text == type1.text)
{

     type1 typeone = new typeone(Convert.ToInt32(tb_Code.Text), tb_Name.Text, Convert.ToDateTime(dateTimePicker1.Text), cb_Sex.Text, tb_Adress.Text, Convert.ToInt32(tb_Phone.Text), tb_Email.Text, pictureBox1.Image);
     LEmployer.Add(typeone);
     listBox1.Items.Add(V.getName());
     listBox1.Show();
}


In the moment that i add a new employer i need to verify if already exists a employer with the same code that is entered in textbox.

I try this :

foreach (employer E in Lemployer)
{
      if tb_code.text == E.getCode())
      {
           Messagebox.show("Code Already exists");
      }
}


But this isn''t working, someone can help me doing this?

Thanks

解决方案

If you never want duplicates, you should better use one of the collection which do not accept duplicates, instead of list, such as:
http://msdn.microsoft.com/en-us/library/bb359438.aspx[^],
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^],
http://msdn.microsoft.com/en-us/library/f7fta44c.aspx[^],
http://msdn.microsoft.com/en-us/library/ms132319.aspx[^],
http://msdn.microsoft.com/en-us/library/dd412070.aspx[^].

—SA


but if i use a sortedlist i need to change all code right? :/


这篇关于如何检查列表中是否已存在雇主,并在文本框中输入相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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