如果我希望用户只在文本框中输入一个单词,但是他们输入两个单词,如何使程序停止并显示错误消息? [英] If i want a user to enter only one word in a text box, but they enter two, how do i make the program stop and display an error message?

查看:78
本文介绍了如果我希望用户只在文本框中输入一个单词,但是他们输入两个单词,如何使程序停止并显示错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,当用户在文本框中输入单词时,程序将显示输入的单词,单词的字符数,以及第一个和最后一个单词的内容字母是单词。我遇到的问题是当有人输入两个单词时,程序应该显示错误,告诉他们只输入一个单词。我的程序做的是它仍然表现得好像只有一个字。例如,如果有人进入,地狱o,它仍然说单词是Hello,有6个字符,第一个字母H,最后一个字母O.



什么我试过了:



我试图让程序使用ascii字符代码32搜索空格,但它似乎只是忽略了是不是空格。

解决方案

尝试:

 如果 myTextBox.Text.Contains( 然后 
MessageBox.Show( 仅限一个字!
返回
结束 如果


I am attempting to write a program where when a user enters a word in a text box the program will display the word entered, the number of characters the word is, and what the first and last letters are of the word. The problem I'm having is when someone enters two words, the program is supposed to display an error telling them to enter one word only. What my program does though is it still acts as if it is one word. For example, if some one enters, Hell o, it still says the word is Hello with 6 characters, 1st letter H, last letter O.

What I have tried:

I have tried to have the program search for a space using the ascii character code 32, but it seems to just ignore if there is a space or not.

解决方案

Try:

If myTextBox.Text.Contains(" ") Then
	MessageBox.Show("One word only, please!")
	Return
End If


这篇关于如果我希望用户只在文本框中输入一个单词,但是他们输入两个单词,如何使程序停止并显示错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆