Checking For Input in Text Boxes


When using textboxes we usually check that the user has actually entered something in our textbox by using code such as ;

Dim name as String
name = txtSearch.text


If name = "" then
messagebox.show("Please type something in the textbox!!!")
exit sub


The only problem with this is that if the user adds spaces into the textbox with no text, Visual Studio will still class the spaces as characters and there for the code will still go ahead and the messagebox will not show .

I have found a String method called Trim which can be used to take away the leading and trailing spaces in the String and solve the above problem. There are two ways you can use this method.

1)

Dim name as String
name = Trim(txtSearch.text)

If name = "" then
messagebox.show
("Please type something in the textbox!!!")exit sub

2)


Dim name as String
name = txtSearch.text
name = name.Trim


If name = "" then
messagebox.show
("Please type something in the textbox!!!")
exit sub