CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Displaying characters remaining

    I need to give users a running total of how many characters they have remaining in a text box e.g. to start with it will say on a label next to the textbox (255 chars remaining) But as the user types, the amount goes down appropriately.

    Anyone point me in the right direction?

    Thanks in advance

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Displaying characters remaining

    Something like this :
    Code:
    Option Explicit
    Private CharsRem As Integer
    
    Private Sub Form_Load()
    CharsRem = 255
    Label1.Caption = CStr(CharsRem) & " Remaining"
    End Sub
    
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyBack Then
        CharsRem = CharsRem + 1
        
        If CharsRem > 255 Then CharsRem = 255
        Label1.Caption = CStr(CharsRem) & " Remaining"
    ElseIf KeyCode = vbKeyDelete Then
        CharsRem = CharsRem + 1
        Label1.Caption = CStr(CharsRem) & " Remaining"
    ElseIf KeyCode = vbKeyLeft Then
        CharsRem = CharsRem
    ElseIf KeyCode = vbKeyRight Then
        CharsRem = CharsRem
    ElseIf KeyCode = vbKeyUp Then
        CharsRem = CharsRem
    ElseIf KeyCode = vbKeyDown Then
        CharsRem = CharsRem
    Else
        CharsRem = CharsRem - 1
        
        If CharsRem < 0 Then CharsRem = 0
        Label1.Caption = CStr(CharsRem) & " Remaining"
    End If
    End Sub
    It works when normal typing is done, you'd still have to determine if other text editing keys (such as End & Home ) are clicked

    IS that what you want ¿
    Last edited by HanneSThEGreaT; February 21st, 2007 at 07:10 AM.

  3. #3
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Displaying characters remaining

    Yes that works perfectly - thanks HanneSThEGreaT

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Displaying characters remaining

    Umm,,

    I dont *think* this will work if the user uses cut/pase operations...

    I would go with an event handler on the text changed, and subtract the current length from the maximum size....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Displaying characters remaining

    Quote Originally Posted by TheCPUWizard
    Umm,,

    I dont *think* this will work if the user uses cut/pase operations...

    I would go with an event handler on the text changed, and subtract the current length from the maximum size....
    This will not work with Cut/ Paste operations.

    brjames, do you want to include cut / Paste functionality as well ¿

  6. #6
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Displaying characters remaining

    Can you elaborate a little more please - maybe with an example? I am a little half asleep today !!!

    Thanks

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Displaying characters remaining

    Do you want your users to be able to Cut text out of your textbox or Paste text into your Textbox ¿ Because as the above code stands, if a person were to paste text into your textbox, the Characters remaining won't update accordingly, same with when a person cut text out of your textbox

  8. #8
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Displaying characters remaining

    yes they probably will need to be able to cut and paste. Is it much work to change the code to make it work???

    Thanks

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Displaying characters remaining

    I would go with an event handler on the text changed, and subtract the current length from the maximum size....
    Why dont you give it a try, there should be enough information based on the previous sample and this quote.

    You will never become skilled unless you try..
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Displaying characters remaining

    Will give it a try. Can't be too hard

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Displaying characters remaining

    Based on my old example.
    This also gets the copied text, then subtract accordingly :
    Code:
    Option Explicit
    Private CharsRem As Integer
    Private ClipTextLength As Integer
    
    Private Sub Form_Load()
    CharsRem = 255
    Label1.Caption = CStr(CharsRem) & " Remaining"
    End Sub
    
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    
    If KeyCode = vbKeyBack Then
        CharsRem = CharsRem + 1
        
        If CharsRem > 255 Then CharsRem = 255
        Label1.Caption = CStr(CharsRem) & " Remaining"
    ElseIf KeyCode = vbKeyDelete Then
        CharsRem = CharsRem + 1
        Label1.Caption = CStr(CharsRem) & " Remaining"
    ElseIf KeyCode = vbKeyLeft Then
        CharsRem = CharsRem
    ElseIf KeyCode = vbKeyRight Then
        CharsRem = CharsRem
    ElseIf KeyCode = vbKeyUp Then
        CharsRem = CharsRem
    ElseIf KeyCode = vbKeyDown Then
        CharsRem = CharsRem
    ElseIf KeyCode = vbKeyV And Shift = 2 Then
    ClipTextLength = Len(Clipboard.GetText)
    CharsRem = CharsRem - ClipTextLength
    Label1.Caption = CStr(CharsRem) & " Remaining"
    
        If CharsRem < 0 Then
        CharsRem = 0
        Label1.Caption = CStr(CharsRem) & " Remaining"
        Text1.Enabled = False 'if 0 then disable
        End If
    Else
    If Shift = 2 Then Exit Sub 'check if ctrl is down, exit sub
    If Shift = 1 Then Exit Sub 'shift doesn't count either
    
        CharsRem = CharsRem - 1
        
        If CharsRem < 0 Then CharsRem = 0
        Label1.Caption = CStr(CharsRem) & " Remaining"
    
    End If
    
    End Sub

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Displaying characters remaining

    There are many here who will try to help. The level of help often depends on how much effort it appears you are putting in (which right now looks good).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Displaying characters remaining

    Cheers for the help guys - I am sure I can take it from here . . . .

    Got 1 probem where as I begin to type in the textbox, the chars remaining somehow gets set to 0 and only changes if I press the backdelete button!!!

    Will let you know how I get on. . . . . .

  14. #14
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Displaying characters remaining

    Debugging hint. Dont directly write to the variable throughout your code. Create a simple helper function or property. Then you can break when the value 0 is passed to the helper, and look at the stack trace.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  15. #15
    Join Date
    Aug 2006
    Posts
    145

    Re: Displaying characters remaining

    umm, am I missing something?
    Code:
    Private Sub Form_Load()
        Text1.MaxLength = 255
        Text1.Text = vbNullString
    End Sub
    
    Private Sub Text1_Change()
        Label1.Caption = "Characters Remaining: " & CStr(Text1.MaxLength - Len(Text1.Text))
    End Sub

Page 1 of 2 12 LastLast

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

Featured