将文本框的内容添加到列表框 [英] adding contents of textboxes into listbox
本文介绍了将文本框的内容添加到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
单击添加按钮时,我有四个texbox,应将文本框中的数据添加到列表框中,下一次当我添加数据时,必须对一个文本框进行验证,无论列表框中是否存在相同的数据( 与此无关的数据库")??
在此先感谢您,
I have four texboxes on click of add button the data in the textboxes should be added into a listbox,next time when i add the data, validation has to be done with respect to one textbox whether same data is present in listbox("No database involved in this")??
thanks in advance,
推荐答案
您可以使用listbox.Items.Add(http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcollection.add.aspx [ ^ ])添加列表中有一个新项目.
要测试是否存在,您可以使用例如简单的LINQ查询来检查集合中是否已存在文本,例如(根据您的需要进行修改):
You can use listbox.Items.Add ( http://msdn.microsoft.com/en-us/library/system.windows.controls.itemcollection.add.aspx[^] ) to add a new item to the list.
To test the existence you can use for example a simple LINQ query to check if the text already exists in the colection, something like (modify according to your needs):
var AmIThere = from item in listbox.Items
where item.ToString() == "SomeText" //most likely some variable here
select item;
if (AmIThere.Count() = 0) {
...
在添加按钮单击事件上添加此代码.
假设您的文本框的名称为textBox1,列表框的名称为listBox1.
add this code on the add button click event.
assuming your textbox''s name is textBox1 and the listbox''s name is listBox1.
string[] contents = new string[4]
{
textBox1.Text,
textBox2.Text,
textBox3.Text,
textBox4.Text
};
foreach (string content in contents)
{
bool isValid = true;
foreach (string item in listBox1.Items)
{
if (item == content)
{
MessageBox.Show("Already in the list.");
isValid = false;
}
}
if (isValid)
listBox1.Items.Add(content);
}
这篇关于将文本框的内容添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文