Vb.net IF/ELSE语句不起作用
我有一个包含5个值的组合框。空行、贷方、借方、贷方结算和借方结算。我正在尝试对其执行错误验证。如果用户在组合框中键入任何内容,或者选择了空字符串,我想触发一个错误。这就是我现在拥有的,但它不起作用。有什么想法吗Vb.net IF/ELSE语句不起作用,vb.net,vb.net-2010,Vb.net,Vb.net 2010,我有一个包含5个值的组合框。空行、贷方、借方、贷方结算和借方结算。我正在尝试对其执行错误验证。如果用户在组合框中键入任何内容,或者选择了空字符串,我想触发一个错误。这就是我现在拥有的,但它不起作用。有什么想法吗 If cboTypeRes.Text.Trim = "" or cboTypeRes.Text.Trim <> "Debit" Or cboTypeRes.Text.Trim <> "Credit" Or cboTypeRes.Text.Trim = "D
If cboTypeRes.Text.Trim = "" or cboTypeRes.Text.Trim <> "Debit" Or cboTypeRes.Text.Trim <> "Credit" Or cboTypeRes.Text.Trim = "Debit Settlement" Or cboTypeRes.Text.Trim = "Credit Settlement" Then
试试这个
cboTypeRes.SelectedText.Trim() ...
但是最好使用cboTypeRes.SelectedValue进行验证虽然在组合框上进行数据验证有更简单的方法,但您的方法应该有效,只不过最后两个方法是=而不是 你的声明应为:
If cboTypeRes.Text.Trim = "" or cboTypeRes.Text.Trim <> "Debit" Or cboTypeRes.Text.Trim <> "Credit" Or cboTypeRes.Text.Trim <> "Debit Settlement" Or cboTypeRes.Text.Trim <> "Credit Settlement" Then
你可能是说
If cboTypeRes.Text.Trim <> "Debit" AndAlso cboTypeRes.Text.Trim <> "Credit" AndAlso cboTypeRes.Text.Trim <> "Debit Settlement" AndAlso cboTypeRes.Text.Trim <> "Credit Settlement" Then
'...
End If
检查所选项目是否包含在项目集合中
if cboTypesRes.SelectedItem Is Nothing Then
Console.WriteLine("Error")
Else
Dim curText = cboTypesRes.SelectedItem.ToString().Trim()
if curText.Length = 0 OrElse Not cboTypesRes.Items.Contains(curText) Then
Console.WriteLine("Error")
End If
End If
一点更新:
当用户直接键入单词时,SelectedItem属性可以为Nothing,或者用户可以使用小写字母键入单词。
如果您发现这些场景可以接受,那么您可以尝试进行此更改
Dim curText = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(cboTypeRes.Text)
if curText.Length = 0 OrElse Not cbo.Items.Contains(curText) Then
Console.WriteLine("Error")
End If
上面的cboTypeRes.Trim只是一个打字错误吗?那不应该编译。是的,我编辑问题的时候打错了。不,那也不行。你还有什么建议吗?