Disallowing a null value in text box
Hi,
I'm currently toying with a chat client, like msn, but the user can currently spam the send button with nothing typed into box and get the result "example said: " continously, I'm trying to get it to blank out the send button / return key command when the field is empty, like MSN, so they can no longer return blank lines.
I've tryed several codes like:
Code:
If txtSend = "" Then
Do whatever
I've tryed calling msgbox/diabling the send button if it happens but it will do it even if i type characters into the box so i'm lost, also tryed various over codes.
It's a single line command box displaying the text typed in there into the main chat window(multiline):
Code:
txtMain = txtMain & "User says: " & txtSend & vbCrLf
for example, most likely the worst way to do it.
Any help be great, cheers.
Re: Disallowing a null value in text box
After validate empty string you can set focus back to textbox:
Code:
If Trim(txtSend.Text) = "" Then
txtSend.SetFocus
Exit Sub
End If
You can also use Validate event handler of your textbox but that may not necessary be what you want - gice it the try anyway.
In the Validate event instead of Exit sub you'll need to set Cancel flag to True:
Code:
Private Sub txtSend_Validate(Cancel As Boolean)
If Trim(txtSend.Text) = "" Then
Cancel = True
End If
End Sub
Re: Disallowing a null value in text box
Cheers for reply
Quote:
Originally Posted by RhinoBull
Code:
Private Sub txtSend_Validate(Cancel As Boolean)
If Trim(txtSend.Text) = "" Then
Cancel = True
End If
End Sub
That disallowed me to click send button with nothing typed in box which is good, only problem is it only does that if I click the box to type it in, i can click anywhere else and press Send or if i say somthing i can click send button with nothing in box(it deletes text after u sent from box)
Any ideas?
ps. it's not been long since i started learning VB, maybe i should wait a little longer to do that, but worth a try.
Re: Disallowing a null value in text box
Put this in the click event of the Send Button
vb Code:
If Len(Trim(txtSend.Text)) = 0 Then
MsgBox "No Text To Send"
txtSend.SetFocus
End If
Re: Disallowing a null value in text box
Quote:
Originally Posted by koolsid
Put this in the click event of the Send Button
vb Code:
If Len(Trim(txtSend.Text)) = 0 Then
MsgBox "No Text To Send"
txtSend.SetFocus
End If
It displays the message box while also displaying the blank space which i typed, it also does it no matter what i enter in like on previous codes.
edit:
rhino it works if i remove the code:
Me.txtSend.Text = ""
but looks bad when text is still in box after sent.
Re: Disallowing a null value in text box
Quote:
Originally Posted by koolsid
Put this in the click event of the Send Button
vb Code:
If Len(Trim(txtSend.Text)) = 0 Then
MsgBox "No Text To Send"
txtSend.SetFocus
End If
That works combined with rhino's, i removed the text box as it's not needed.
Just only problem is that it still sends the empty space there the first time i click send, after that it works.
p.s cheers for reply on other thread, got single line box now, looks better :P
Re: Disallowing a null value in text box
Re: Disallowing a null value in text box
Quote:
Originally Posted by Bountyhuntr
Just only problem is that it still sends the empty space ... :P
That's because it doesn't exit current procedure - see first code snippet in my previous reply (post #2).
Re: Disallowing a null value in text box
Quote:
Originally Posted by RhinoBull
That's because it doesn't exit current procedure - see first code snippet in my previous reply (post #2).
I just changed the "TabIndex" to 0 for the textbox so the focus is at textbox when app loads, sorted.
just got to sort out the hotkey for it as I press "return" it still spams nothing, even tryed setting the "default" to true instead of using the keypress function. ;/
cheers.