gpt4 book ai didi

c# - 如果两个文本框为空则显示错误信息

转载 作者:太空宇宙 更新时间:2023-11-03 18:03:20 24 4
gpt4 key购买 nike

我有两个文本框,如果用户没有输入任何信息并且它们是空的,我想显示一条错误消息。我有这段代码(但只适用于一个):

if (String.IsNullOrEmpty(textBox1.Text))
{
var dialog = new MessageDialog("Error");
await dialog.ShowAsync();
}

我已经试过了,但它不起作用:

if (String.IsNullOrEmpty(textBox1.Text || textBox2.Text))
{
var dialog = new MessageDialog("Error");
await dialog.ShowAsync();
}

最佳答案

此条件对 || 运算符无效,因为 || 运算符需要 bool 值作为其操作数。

String.IsNullOrEmpty(textBox1.Text || textBox2.Text)

所以这应该会抛出错误提示

Operator '||' cannot be applied to operands of type 'string' and 'string'

如果您想在任何文本框为空时显示错误消息:

String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text)

如果您想在两个文本框都为空时显示错误消息:

String.IsNullOrEmpty(textBox1.Text) && String.IsNullOrEmpty(textBox2.Text)

关于c# - 如果两个文本框为空则显示错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com