【发布时间】:2021-07-15 05:49:24
【问题描述】:
原代码:
下面的代码总是运行Case Else,因为文本值中有一个感叹号。如果我要删除它们,它们就会再次工作。
Private Sub CommandLineFunctions()
Select Case True
Case DeveloperCommandLine.Text = "!exit"
ToolStripButtonClose.PerformClick()
Case DeveloperCommandLine.Text = "!clear"
MessageDisplayBounds.Text = String.Empty
Case DeveloperCommandLine.Text = "!disable"
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub
编辑
尝试建议的方法(见下文)也只运行Case Else。
Private Sub CommandLineFunctions()
Select Case True
Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase)
ToolStripButtonClose.PerformClick()
Case DeveloperCommandLine.Text.Equals("!clear", StringComparison.CurrentCultureIgnoreCase)
MessageDisplayBounds.Text = String.Empty
Case DeveloperCommandLine.Text.Equals("!disable", StringComparison.CurrentCultureIgnoreCase)
DeveloperMode.Enabled = False
CloseForm(ToolStripButtonClose, EventArgs.Empty)
Case Else
MessageDisplayBounds.Text += DeveloperCommandLine.Text & vbCr
ScrollableContentCollection.VerticalScroll.Value = ScrollableContentCollection.VerticalScroll.Maximum
End Select
DeveloperCommandLine.Text = String.Empty
End Sub
【问题讨论】:
-
是的,我想检查字符串是否以除区分大小写之外的所有方式匹配。如果我必须使用区分大小写的验证方法,那就是找到原因,我只需
ToLower就可以绕过它。 -
如果你想要一个不区分大小写的匹配,你可以保持当前的形式并改变,例如,
[...] Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase) [...]——我不知道是什么检查字符串是否以各种方式匹配除了区分大小写意味着,在实践中。您必须在问题中添加 - 而不是在 cmets 中 - 更好地描述您的意图。 -
对不起,我不知道该怎么解释。我对编程很陌生,而且我是自学成才的,所以我很难很好地解释一些事情。我将投票删除该帖子,因为这已经变得比它的价值更成问题。不幸的是,
Case DeveloperCommandLine.Text.Equals("!exit", StringComparison.CurrentCultureIgnoreCase)也不起作用。不过谢谢你的帮助。我会在以后的问题中尝试更好地解释。 -
如果要测试Text是否包含字符串,可以使用,例如
Case DeveloperCommandLine.Text.IndexOf("!exit", StringComparison.CurrentCultureIgnoreCase) >= 0 -
只是一个问题,你正在尝试匹配“!exit”,那个解释标记不应该做一些逻辑否定吗?例如 DeveloperCommandLine.Text 不等于退出
标签: vb.net winforms text richtextbox