Results 1 to 11 of 11

Thread: Trim.length Help

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    40

    Trim.length Help

    Hi guys,
    I'm using VS2005 and VB.NET for this program.
    I'm trying to validate a textbox for a "State" so that the length is limited to two characters and they are both capitalized.
    for ex: CA for California
    any hints on how to do it using the Trim and Length methods?
    I know there are probably some easier ways out there but I'm required to get it done this way for class.

    Thank you

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Trim.length Help

    Well, .Trim will strip leading and trailing spaces from the text. .Length will tell you how long the string is. And .ToUpper will make the string upper case.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Trim.length Help

    Look at the MaxLength property of the textbox, this property lets you limit the number of characters that can be typed into the box.

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Trim.length Help

    nabilino, in this case it may be easier to consider using a combobox for this (set as a dropdownlist)

    here's all 50 state's abbreviations:
    Code:
    New String() {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"}
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Trim.length Help

    Juggalo, maybe you missed his quote:
    know there are probably some easier ways out there but I'm required to get it done this way for class.

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    40

    Re: Trim.length Help

    Ok! so what can I write to disable the user to input character other than letters? like if he puts in: "/S" it will generate an error...

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Trim.length Help

    honestly there are about 100 ways to do this, so we would really need to know the specs of the project. Also being that this is homework, most people on this forum will be pretty reluctant to just provide fully working solutions to your homework

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Trim.length Help

    Quote Originally Posted by kleinma
    Juggalo, maybe you missed his quote:
    Yes I did, sucks to be him
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2008
    Posts
    40

    Re: Trim.length Help

    I know...it's not really homework. it's an online lab and i'm not required to do it or graded for it. I just want to follow his rules to get the most out of it.
    vb Code:
    1. 'Validate State TextBox
    2.         If Me.stateTextBox.Text = "" Then
    3.             e.Cancel = True
    4.             ErrorProvider1.SetError(stateTextBox, "State cannot be missing!")
    5.         Else
    6.             ErrorProvider1.SetError(stateTextBox, "")
    7.         End If
    8.  
    9.         If Me.salesTextBox.Text IsNot "AA" Then
    10.             ErrorProvider1.SetError(stateTextBox, "State name has to be two letters!")
    11.         Else
    12.             ErrorProvider1.SetError(cityTextBox, "")
    13.         End If

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Trim.length Help

    why does your second else statement reference cityTextBox?

    Also I would be using if-elseif instead of multiple if-else-endif statements.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Trim.length Help

    Code:
    'validate state
            Dim theStates() As String = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"}
            If Array.IndexOf(theStates, what goes here) = -1 Then
               
                Stop
            Else
               
                Stop
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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