Results 1 to 9 of 9

Thread: Disallowing a null value in text box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Location
    UK
    Posts
    171

    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.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Location
    UK
    Posts
    171

    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.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Disallowing a null value in text box

    Put this in the click event of the Send Button

    vb Code:
    1. If Len(Trim(txtSend.Text)) = 0 Then
    2.     MsgBox "No Text To Send"
    3.     txtSend.SetFocus
    4. 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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Location
    UK
    Posts
    171

    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:
    1. If Len(Trim(txtSend.Text)) = 0 Then
    2.     MsgBox "No Text To Send"
    3.     txtSend.SetFocus
    4. 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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Location
    UK
    Posts
    171

    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:
    1. If Len(Trim(txtSend.Text)) = 0 Then
    2.     MsgBox "No Text To Send"
    3.     txtSend.SetFocus
    4. 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

  7. #7
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    Re: Disallowing a null value in text box

    use vbnullstring
    IT CTO & System Administrator.

  8. #8

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Location
    UK
    Posts
    171

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width