Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Cursor Location

Hello all,

I have a text box on a form. In the text box, I do not
allow someone to use the "#" symbol, but using the method
I use, when I remove the symbol after someone tries to use
it, the cursor is restored to the beginning of the string
or not at all. I would like the cursor to be at the end of
the string so the user can just continue typing after
dismissing the error box. Here is the code I'm using:


Private Sub txtValue_Change()

If Right(txtValue, 1) = "#" Then
MsgBox "You may not use the ""#"" symbol for this
value!", vbOKOnly + vbExclamation, "Invalid Character..."
txtValue = Left(txtValue, Len(txtValue) - 1)
txtValue.SetFocus
End If

End Sub


Does anyone know how I can programmatically tell the
cursor to go to the end of the string in the textbox on
focus?

Thanks for any advice, Jack
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Cursor Location

You may like to consider using keypress event instead of change event. Your
code isn't full proof, the user can type "#" in the middle of the text and
the code won't detect it.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 35 Then
KeyAscii = 0
MsgBox "invalid character"
End If
End Sub

Use SelStart to place the cursor within the text box. To go to the end:
TextBox1.SelStart = 1000
or set it to Len(TextBox1.Text)

"Jack" wrote in message
...
Hello all,

I have a text box on a form. In the text box, I do not
allow someone to use the "#" symbol, but using the method
I use, when I remove the symbol after someone tries to use
it, the cursor is restored to the beginning of the string
or not at all. I would like the cursor to be at the end of
the string so the user can just continue typing after
dismissing the error box. Here is the code I'm using:


Private Sub txtValue_Change()

If Right(txtValue, 1) = "#" Then
MsgBox "You may not use the ""#"" symbol for this
value!", vbOKOnly + vbExclamation, "Invalid Character..."
txtValue = Left(txtValue, Len(txtValue) - 1)
txtValue.SetFocus
End If

End Sub


Does anyone know how I can programmatically tell the
cursor to go to the end of the string in the textbox on
focus?

Thanks for any advice, Jack



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Cursor Location

Jack use the KeyPress Event of your TextBox it is made for this situation


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If KeyAscii = 35 Then ' Detect the # Key
MsgBox "Sorry cannot Enter a " & Chr(35) & " in this Field "
KeyAscii = 0 'Cancel the KeyStroke
End If

End Sub


Fred
"Jack" wrote in message
...
Hello all,

I have a text box on a form. In the text box, I do not
allow someone to use the "#" symbol, but using the method
I use, when I remove the symbol after someone tries to use
it, the cursor is restored to the beginning of the string
or not at all. I would like the cursor to be at the end of
the string so the user can just continue typing after
dismissing the error box. Here is the code I'm using:


Private Sub txtValue_Change()

If Right(txtValue, 1) = "#" Then
MsgBox "You may not use the ""#"" symbol for this
value!", vbOKOnly + vbExclamation, "Invalid Character..."
txtValue = Left(txtValue, Len(txtValue) - 1)
txtValue.SetFocus
End If

End Sub


Does anyone know how I can programmatically tell the
cursor to go to the end of the string in the textbox on
focus?

Thanks for any advice, Jack



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Cursor Location

Absolutely awesome! Thanks for the help, kiat and
thanks also for pointing out the flaw in my using the
change event, I have changed it to the keypress event as
you suggested.

-Jack



-----Original Message-----
You may like to consider using keypress event instead of

change event. Your
code isn't full proof, the user can type "#" in the

middle of the text and
the code won't detect it.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As

MSForms.ReturnInteger)
If KeyAscii = 35 Then
KeyAscii = 0
MsgBox "invalid character"
End If
End Sub

Use SelStart to place the cursor within the text box. To

go to the end:
TextBox1.SelStart = 1000
or set it to Len(TextBox1.Text)

"Jack" wrote in message
...
Hello all,

I have a text box on a form. In the text box, I do not
allow someone to use the "#" symbol, but using the

method
I use, when I remove the symbol after someone tries to

use
it, the cursor is restored to the beginning of the

string
or not at all. I would like the cursor to be at the end

of
the string so the user can just continue typing after
dismissing the error box. Here is the code I'm using:


Private Sub txtValue_Change()

If Right(txtValue, 1) = "#" Then
MsgBox "You may not use the ""#"" symbol for this
value!", vbOKOnly + vbExclamation, "Invalid

Character..."
txtValue = Left(txtValue, Len(txtValue) - 1)
txtValue.SetFocus
End If

End Sub


Does anyone know how I can programmatically tell the
cursor to go to the end of the string in the textbox on
focus?

Thanks for any advice, Jack



.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to determine cursor location? Dan Excel Discussion (Misc queries) 4 March 12th 09 03:11 PM
Tracking cursor location JonStein Excel Worksheet Functions 1 May 6th 08 03:34 AM
Function for cursor location sandyboy Excel Worksheet Functions 8 March 28th 07 01:18 PM
Cursor location between grouped worksheets woodyjenk Excel Discussion (Misc queries) 0 November 14th 06 07:16 PM
Cursor Location Glen Excel Discussion (Misc queries) 2 July 21st 05 09:35 PM


All times are GMT +1. The time now is 10:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"