简体   繁体   English

文本框中的字段不正确时如何显示两个不同的消息框

[英]How to display two different message boxes when fields are incorrect in text boxes

I need to display a message for two different textBoxes in my app if the information is entered incorrectly. 如果输入的信息不正确,我需要为我的应用程序中的两个不同的textBoxes显示一条消息。

Basically when the user enters the information it will then search the database to check if that info is there. 基本上,当用户输入信息时,它将搜索数据库以检查该信息是否存在。 If it is not then it must display a message saying the entered information is incorrect 如果不是,则必须显示一条消息,指出输入的信息不正确

This is my code I am using 这是我正在使用的代码

SqlConnection con = new SqlConnection(@"Data Source=DEVELOPMENT\ACCESSCONTROL;Initial Catalog=ACCESSCONTROL;User ID=sa;Password=P@55w0rd123");
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM NewVisitor WHERE (IDNumber = @IDNumber AND PersonVisit = @PersonVisit)", con);
check_User_Name.Parameters.AddWithValue("@IDNumber", idNumber_TxtBox.Text);
check_User_Name.Parameters.AddWithValue("@PersonVisit", personVisiting_TxtBox.Text);
con.Open();

int UserExist = (int)check_User_Name.ExecuteScalar();

if (UserExist > 0)
{
      .....
}
else
{
    MessageBox.Show("No Visitor with ID Number: " + idNumber_TxtBox.Text + " Exists");

    MessageBox.Show("No Employee,  " + personVisiting_TxtBox.Text + " Exists");
}

At the moment it will only display the first messageBox if either information is entered incorrectly 目前,如果任何一个信息输入错误,它将仅显示第一个messageBox

You need to combine the two messages into a single string. 您需要将两个消息合并为一个字符串。 Oh, and don't contatenate strings with a +, use string.Format or string interpolation ($) 哦,不要用+替换字符串,请使用string.Format或字符串插值($)

var sb = new StringBuilder();
sb.AppendLine($"No Visitor with ID Number: {idNumber_TxtBox.Text} Exists");
sb.AppendLine($"No Employee, {personVisiting_TxtBox.Text} Exists");
MessageBox.Show(sb.ToString());

It's not possible to show two Message Boxes at the same time since the Message Box is a Modal window . 由于消息框是模式窗口,因此无法同时显示两个消息框。 So it will stop the execution of the MainWindow untill that first message box window is closed. 因此它将停止MainWindow的执行,直到关闭第一个消息框窗口。

You could handle that by using a single message box with a combination of both messages. 您可以通过将单个消息框与两个消息组合使用来解决此问题。

string errorMessage = $"No Visitor with ID Number: {idNumber_TxtBox.Text} Exists") + Environment.NewLine + $"No Employee, {personVisiting_TxtBox.Text} Exists");
MessageBox.Show(errorMessage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM