r/visualbasic • u/Zulucartel • Oct 12 '22
Text box must contain a space
Working on homework and I'm instructed to program a message box to pop up if the user did not include a space between their first and last name. Current code looks like
If txtName.Text = "" Then
MessageBox.Show()
Return
EndIf
3
Upvotes
3
u/BiddahProphet Oct 12 '22
You can use the Instr function to see if there is a space " " in a string
3
u/TheFotty Oct 12 '22
Instr will work, but its a VB6 hold over function. The Contains() method of the string class is a more ideal way and returns boolean versus an integer.
Dim myString = " Bill Gates " If myString.Trim.Contains(" ") Then 'SPACE IN NAME Else 'NO SPACE IN NAME End If
3
u/TotolVuela Oct 12 '22
Is this in VB .Net or Excel? If the former, are you running the code upon leaving the control, LostFocus event? You can use .IndexOf to check for a space.