Results 1 to 5 of 5

Thread: There has to be a better way

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304

    There has to be a better way

    In VB6, if I wanted to check that data had been entered into a field I would say something like If Textbox1.text = "", then I knew there was nothing entered.

    In VB .Net, I ended up writing this:

    If Instr(Ltrim(Textbox1.text), " ") = 0 then there was nothng there, but even that solution is half baked as if the box is filled completely, then it won't work, anyone got any better ideas???

    The other alternative was to compare it exactly to the number of spaces between quotes but that seems very messy

  2. #2
    egiggey
    Guest
    hi Richard I just tried the following and it worked perfectly

    If TextBox1.Text = "" Then MsgBox("textbox is empty")

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    my fault for not explaining the problem fully, if you set the text to spaces as in the load event say textbox1.text = " "
    then if textbox1.text = "" doesn't work.

    In the end I declared a string

    dim blanks as string = " "

    much longer than my biggest text box and then used

    if instr(blanks, textbox1.text) <> 0 then

    - error message

    end if

    I was trying to make sure that on input for a mandatory text field the use couldn't enter spaces and defeat my edit check, I still think it looks very clumsy but I can't think of a better way
    Last edited by RichardAtherton; Mar 13th, 2002 at 07:31 PM.

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    This is a little more pleasing to look at..

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim sBuffer As String = "  "
    
            If sBuffer.Trim(sBuffer) = String.Empty Then
                MessageBox.Show("I'm Empty!!")
            End If
        End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    totally agree thanks!!!

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