Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Prevent cursor/focus from moving

Create a small Userform. Put two TextBoxes in it. Add the following
code.

Private Sub UserForm_Initialize()
TextBox1.Value = "0.00"
TextBox2.Value = "0.00"
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode _
As MSForms.ReturnInteger, _
ByVal Shift As Integer)

Dim sHours As String
Dim v As Integer

Select Case KeyCode
Case 37 ' Left
v = -1
Case 38
v = 4
Case 39 ' Right
v = 1
Case 40
v = -4
Case Else
v = 0
End Select

sHours = TextBox1.Text
If IsNumeric(sHours) And v < 0 Then
sHours = sHours + v * 0.25
If sHours < 0 Then sHours = 0
TextBox1.Text = Format(sHours, "#0.00")
End If
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode _
As MSForms.ReturnInteger, _
ByVal Shift As Integer)

Dim sHours As String
Dim v As Integer

Select Case KeyCode
Case 37 ' Left
v = -1
Case 38
v = 4
Case 39 ' Right
v = 1
Case 40
v = -4
Case Else
v = 0
End Select

sHours = TextBox2.Text
If IsNumeric(sHours) And v < 0 Then
sHours = sHours + v * 0.25
If sHours < 0 Then sHours = 0
TextBox2.Text = Format(sHours, "#0.00")
End If
End Sub

When the boxes are stacked, pressing the up arrow in the upper box has
the desired effect but pressing the down arrow increments the amount
by -1.00 then the cursor/focus jumps to the lower box. Similar
opposite behavior in the lower box. This does not happen when the
boxes are side-by-side. Can I control this, keep cursor/focus in boxes
when stacked?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Prevent cursor/focus from moving


Put

KeyCode = 0

at the end of both of your KeyDown events. This cancels the keystroke.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Sat, 20 Mar 2010 13:42:11 -0700 (PDT), Slim Slender
wrote:

Create a small Userform. Put two TextBoxes in it. Add the following
code.

Private Sub UserForm_Initialize()
TextBox1.Value = "0.00"
TextBox2.Value = "0.00"
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode _
As MSForms.ReturnInteger, _
ByVal Shift As Integer)

Dim sHours As String
Dim v As Integer

Select Case KeyCode
Case 37 ' Left
v = -1
Case 38
v = 4
Case 39 ' Right
v = 1
Case 40
v = -4
Case Else
v = 0
End Select

sHours = TextBox1.Text
If IsNumeric(sHours) And v < 0 Then
sHours = sHours + v * 0.25
If sHours < 0 Then sHours = 0
TextBox1.Text = Format(sHours, "#0.00")
End If
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode _
As MSForms.ReturnInteger, _
ByVal Shift As Integer)

Dim sHours As String
Dim v As Integer

Select Case KeyCode
Case 37 ' Left
v = -1
Case 38
v = 4
Case 39 ' Right
v = 1
Case 40
v = -4
Case Else
v = 0
End Select

sHours = TextBox2.Text
If IsNumeric(sHours) And v < 0 Then
sHours = sHours + v * 0.25
If sHours < 0 Then sHours = 0
TextBox2.Text = Format(sHours, "#0.00")
End If
End Sub

When the boxes are stacked, pressing the up arrow in the upper box has
the desired effect but pressing the down arrow increments the amount
by -1.00 then the cursor/focus jumps to the lower box. Similar
opposite behavior in the lower box. This does not happen when the
boxes are side-by-side. Can I control this, keep cursor/focus in boxes
when stacked?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Prevent cursor/focus from moving

Thanks Chip, that did the trick.
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 prevent focus by clicking combo box ? dennis logan[_2_] Excel Programming 7 October 22nd 07 03:50 PM
Prevent control taking focus when accelerator key is used? Michael Jones Excel Programming 4 July 10th 06 10:40 AM
cursor focus in excel fourthhorn Excel Discussion (Misc queries) 1 June 2nd 06 06:05 AM
How to prevent cursor from moving behind filled cells Spreadsheet Slave Excel Discussion (Misc queries) 1 April 4th 05 01:43 PM
Cursor Keys does not change cell focus Skypilot Excel Discussion (Misc queries) 3 February 24th 05 07:13 AM


All times are GMT +1. The time now is 02:20 AM.

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

About Us

"It's about Microsoft Excel"