Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过获取ID来显示名称?_C#_Winforms_Messagebox - Fatal编程技术网

C# 如何通过获取ID来显示名称?

C# 如何通过获取ID来显示名称?,c#,winforms,messagebox,C#,Winforms,Messagebox,我问用户他/她的选民id是什么,如果它在我的列表中,程序会显示一个显示他/她的姓名的消息框。然后,如果用户按下“ok”,它将重定向到另一个表单 这是我的密码: public class Voter{ public string voterName {get; set;} public int voterID {get; set;} public override string ToString() { re

我问用户他/她的选民id是什么,如果它在我的列表中,程序会显示一个显示他/她的姓名的消息框。然后,如果用户按下“ok”,它将重定向到另一个表单

这是我的密码:

public class Voter{
        public string voterName {get; set;}
        public int voterID {get; set;}

        public override string ToString()
        {
            return "   Name: " + voterName;
        }
        }
void BtnValidateClick(object sender, EventArgs e)
    {
        int id = Int32.Parse(tbVotersID.Text);
        List<Voter> voters = new List<Voter>();
        voters.Add(new Voter() {voterName = "voter #1", voterID = 12345});
        voters.Add(new Voter() {voterName = "voter #2", voterID = 67890});
        voters.Add(new Voter() {voterName = "voter #3", voterID = 11800});

        if (voters.Contains(new Voter {voterID = id})){

        //prompts a messagebox that shows voterName
            }
        else{
        MessageBox.Show("ID not recognized.", "ID ENTRY", MessageBoxButtons.OK, 
        MessageBoxIcon.Error);
        }

    }
公共类投票者{
公共字符串voterName{get;set;}
公共int voterID{get;set;}
公共重写字符串ToString()
{
return“Name:+voterName;
}
}
无效BtnValidateClick(对象发送者,事件参数e)
{
intid=Int32.Parse(tbVotersID.Text);
列表投票者=新列表();
添加(new Voter(){voterName=“Voter#1”,voterID=12345});
添加(new Voter(){voterName=“Voter#2”,voterID=67890});
添加(new Voter(){voterName=“Voter#3”,voterID=11800});
if(投票者.Contains(新投票者{voterID=id})){
//提示显示voterName的消息框
}
否则{
MessageBox.Show(“ID未识别”),“ID输入”,MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
您可以使用LINQ和Find()来获取第一个结果的voterName。检查其是否为null,如果不是,则显示MessageBox()

void btnValidate单击(对象发送方,事件参数e)
{
intid=Int32.Parse(tbVotersID.Text);
列表投票者=新列表();
添加(new Voter(){voterName=“Voter#1”,voterID=12345});
添加(new Voter(){voterName=“Voter#2”,voterID=67890});
添加(new Voter(){voterName=“Voter#3”,voterID=11800});
var voterName=voctors.Find(voctor=>voctor.voterID==id)?.voterName;
如果(!string.IsNullOrEmpty(voterName)){
MessageBox.Show(voterName);
}
否则{
MessageBox.Show(“ID未识别”),“ID输入”,MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}

旁注:当用户花时间回答您的问题时,请确保您接受并投票支持他们的答案,如果这有助于解决您的问题;这是一种表示感谢的方式,任何人都知道这是很有帮助的。
void BtnValidateClick(object sender, EventArgs e)
{
    int id = Int32.Parse(tbVotersID.Text);
    List<Voter> voters = new List<Voter>();
    voters.Add(new Voter() {voterName = "voter #1", voterID = 12345});
    voters.Add(new Voter() {voterName = "voter #2", voterID = 67890});
    voters.Add(new Voter() {voterName = "voter #3", voterID = 11800});


    var voterName = voters.Find(voter => voter.voterID == id)?.voterName;
    if (!string.IsNullOrEmpty(voterName)){
            MessageBox.Show(voterName);
        }
    else{
    MessageBox.Show("ID not recognized.", "ID ENTRY", MessageBoxButtons.OK, 
    MessageBoxIcon.Error);
    }

}