|
-
February 21st, 2007, 05:42 AM
#1
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
-
February 21st, 2007, 06:59 AM
#2
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.
-
February 21st, 2007, 07:10 AM
#3
Re: Displaying characters remaining
Yes that works perfectly - thanks HanneSThEGreaT
-
February 21st, 2007, 07:17 AM
#4
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
-
February 21st, 2007, 07:28 AM
#5
Re: Displaying characters remaining
 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 ¿
-
February 21st, 2007, 07:28 AM
#6
Re: Displaying characters remaining
Can you elaborate a little more please - maybe with an example? I am a little half asleep today !!!
Thanks
-
February 21st, 2007, 07:32 AM
#7
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
-
February 21st, 2007, 07:37 AM
#8
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
-
February 21st, 2007, 07:46 AM
#9
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
-
February 21st, 2007, 08:00 AM
#10
Re: Displaying characters remaining
Will give it a try. Can't be too hard
-
February 21st, 2007, 08:05 AM
#11
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
-
February 21st, 2007, 08:05 AM
#12
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
-
February 21st, 2007, 08:10 AM
#13
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. . . . . .
-
February 21st, 2007, 08:13 AM
#14
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
-
February 21st, 2007, 01:49 PM
#15
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
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
|