|
-
Nov 19th, 2008, 09:57 AM
#1
Thread Starter
Addicted Member
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.
-
Nov 19th, 2008, 10:02 AM
#2
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
-
Nov 19th, 2008, 10:37 AM
#3
Thread Starter
Addicted Member
Re: Disallowing a null value in text box
Cheers for reply
 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.
-
Nov 19th, 2008, 10:41 AM
#4
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Nov 19th, 2008, 10:45 AM
#5
Thread Starter
Addicted Member
Re: Disallowing a null value in text box
 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.
-
Nov 19th, 2008, 10:51 AM
#6
Thread Starter
Addicted Member
Re: Disallowing a null value in text box
 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
-
Nov 19th, 2008, 02:17 PM
#7
Addicted Member
Re: Disallowing a null value in text box
IT CTO & System Administrator.
-
Nov 19th, 2008, 02:33 PM
#8
Re: Disallowing a null value in text box
 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).
-
Nov 20th, 2008, 06:17 PM
#9
Thread Starter
Addicted Member
Re: Disallowing a null value in text box
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|