如果用户名不是唯一的(C#),则在列表框中检查输入的用户名以显示错误消息

问题描述 投票:0回答:1

我正在一个项目中,如果输入的username非唯一,则必须显示错误消息。我正在尝试在Listbox中搜索用户名是否已经存在。该程序运行正常,但无法正常工作(即使我输入的== true与我的username中的另一个[,也找到never Listbox)我很漂亮C#的新功能,所以我有点不理解语法,但似乎没有任何效果。非常感谢您的帮助!

private void txt_Username_TextChanged(object sender, EventArgs e) { bool find = lb_Form1_ListUsers.ContainsFocus.Equals(txt_Username.Text); if (find == true) { lbl_Form1_UniqueError.Visible = true; } else { lbl_Form1_UniqueError.Visible = false; } find = false; }
c# winforms listbox contains
1个回答
1
投票
没什么意义。

假设lb_Form1_ListUsers.Items包含类型为String(用户名)的值,如果Any等于txt_Username.Text,我们可以查询它们> using System.Linq; ... private void txt_Username_TextChanged(object sender, EventArgs e) { bool find = lb_Form1_ListUsers .Items .Cast<String>() //TODO: put the right type here if required .Any(item => item == txt_Username.Text); lbl_Form1_UniqueError.Visible = find; }

© www.soinside.com 2019 - 2024. All rights reserved.