My textbox change reacts too quickly

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
Hi all. When I type an 11 into my textbox (in my userform), I believe that the moment I type the first 1, the "event" kicks in. Is there anyway that I can have the change even "wait" until I am finished typing in all the digits of my number?


Code:
Private Sub TextBox1_Change()
Dim prime As Integer, divisor As Integer, currentcol As Integer
Dim faccount As Integer, currentresidue As Long, currentfactor As Long
Dim i As Integer, j As Integer
prime = TextBox1.Value
TextBox2.Value = prime - 1
'Write divisors across and phi of the divisors across at the bottom
Spreadsheet1.Range("c2").Value = 1
Spreadsheet1.Range("c2").Font.Bold = True
currentcol = 3
Spreadsheet1.Cells(prime - 1 + 3, 2) = "phi(d):"
Spreadsheet1.Cells(prime - 1 + 3, 2).Font.Bold = True
Spreadsheet1.Cells(prime - 1 + 3, 2).HorizontalAlignment = xlRight
Spreadsheet1.Cells(prime - 1 + 3, 3) = 1
MsgBox "prime-1+3 right before bolding is " & prime - 1 + 3
Spreadsheet1.Cells(prime - 1 + 3, 3).Font.Bold = True
For i = 2 To prime - 1
    If Factor(i, prime - 1) Then
       currentcol = currentcol + 1
       Spreadsheet1.Cells(2, currentcol) = i
       Spreadsheet1.Cells(2, currentcol).Font.Bold = True
       Spreadsheet1.Cells(prime - 1 + 3, currentcol) = phi(i)
       Spreadsheet1.Cells(prime - 1 + 3, currentcol).Font.Bold = True
    Else
    End If
Next i
'Write residues down
Spreadsheet1.Range("b3").Value = 1
Spreadsheet1.Range("b3").Font.Bold = True
For i = 2 To prime - 1
 Spreadsheet1.Cells(i + 2, 2) = i
 Spreadsheet1.Cells(i + 2, 2).Font.Bold = True
Next

faccount = Factorcount(prime - 1)
currentcol = 3
For i = 0 To faccount - 1 ' columns
 For j = 1 To prime - 1       ' rows
 
 
  currentresidue = Spreadsheet1.Cells(j + 2, 2)
  currentfactor = Spreadsheet1.Cells(2, i + 3)
  
  Spreadsheet1.Cells(j + 2, currentcol + i) = PowerMOD(currentresidue, currentfactor, prime)
  If PR(currentresidue, prime) Then Spreadsheet1.Cells(j + 2, currentcol + i).Interior.Color = RGB(0, 255, 0) 'Green
 
 Next
Next
End Sub
 
Oh ye of little faith.:laugh: The following code will do what you asked for (2 seconds seems kind of long, but 1 second does not give enough time to hit the second keystroke... unfortunately, we can't do 1.5 seconds which would probably "feel" right).
Rich (BB code):
Private Sub TextBox1_Change()
  Dim TimesUp As Date
  TimesUp = Now + TimeSerial(0, 0, 2)
  If Len(TextBox1.Text) > 0 Then
    Do While Now < TimesUp
      DoEvents
      If Len(TextBox1.Text) > 1 Then Exit Do
    Loop
    If Len(TextBox1.Text) Then
     '
      ' Your code goes here (I used a MsgBox to simulate it)
      '
      MsgBox TextBox1.Value
   End If
  End If
End Sub

Sorry Rick. I should have known not to doubt you. (I can't find the bowing symbol or I would have used it). I didn't check it, but I am sure it will work. I am going to stick to my command button solution. It is a less code kludge. Thank you though

Gene
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I didn't check it, but I am sure it will work. I am going to stick to my command button solution. It is a less code kludge. Thank you though
Since you didn't try it (yet), I should mention that there is no time delay whatsoever once you press the second keystroke... the delay only occurs if you type a single character into the TextBox. If you decide to try it, I would suggest setting the TextBox's MaxLen property to 2 so that you don't get odd effects for accidental third-character keypresses. By the way, I wouldn't consider the code all that kludgy... Microsoft uses a similar mechanism (the time delay) in their DatePicker control, but I think it is less than 2 seconds (but not by much as the delay is noticeable).

As for the CommandButton solution, make sure to set the CommandButton's Default property to True so that your users can, in addition to clicking the CommandButton, just press the Enter Key to activate the code.
 
Upvote 0
Since you didn't try it (yet), I should mention that there is no time delay whatsoever once you press the second keystroke... the delay only occurs if you type a single character into the TextBox. If you decide to try it, I would suggest setting the TextBox's MaxLen property to 2 so that you don't get odd effects for accidental third-character keypresses. By the way, I wouldn't consider the code all that kludgy... Microsoft uses a similar mechanism (the time delay) in their DatePicker control, but I think it is less than 2 seconds (but not by much as the delay is noticeable).

As for the CommandButton solution, make sure to set the CommandButton's Default property to True so that your users can, in addition to clicking the CommandButton, just press the Enter Key to activate the code.

Good suggestion, but I can't do it. I decided to add (which you did not know of course) a clear button as well. This way the user can clear the input and start again. I assume that hitting enter would confuse the system?

Gene
 
Upvote 0
Sorry Rick. I should have known not to doubt you. (I can't find the bowing symbol or I would have used it). I didn't check it, but I am sure it will work. I am going to stick to my command button solution. It is a less code kludge. Thank you though
Gene, you may want to try this out as it has a much better feel to it than my first attempt.... I think you may actually like it. On top of that, you can refine the delay down to hundredth of a second (just adjust the number in red which represents seconds, so my example shows a 1.2 second delay).
Rich (BB code):
Private Sub TextBox1_Change()
  Dim TimesUp As Double
  TimesUp = Timer + 1.2
  If Len(TextBox1.Text) > 0 Then
    Do While Timer < TimesUp
      DoEvents
      If Len(TextBox1.Text) > 1 Then Exit Do
    Loop
    If Len(TextBox1.Text) Then
      '
      ' Your code goes here (I used a MsgBox to simulate it)
      '
      MsgBox TextBox1.Value
    End If
  End If
End Sub
Your actual code would go in the section I highlighted in green. And I still recommend that you set the TextBox's MaxLen property to 2.
 
Upvote 0
another possible option to let them press enter after they finished their input and use the keydown event to check if enter was pressed and execute your macro

Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
     If KeyCode = 13 Then 'enter pressed
       MsgBox "enter pressed"
    End If
End Sub
 
Upvote 0
another possible option to let them press enter after they finished their input and use the keydown event to check if enter was pressed and execute your macro

Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
     If KeyCode = 13 Then 'enter pressed
       MsgBox "enter pressed"
    End If
End Sub

You mean require them to press enter. But that is really what I did, I guess, by requiring them to press a command button.

Gene
 
Upvote 0
I did Rick. I just decided to stay with the command button "solution". I have mentally saved your solution for future reference.

Gene
 
Upvote 0
I did Rick. I just decided to stay with the command button "solution". I have mentally saved your solution for future reference.
Just wondering if you tried it out though (maybe on a new user form just for testing purposes)? I thought it seemed natural enough with the "new" method I used to get the timing down to 1.2 seconds (which can be customized down to hundredth of a second if desired) and thought that you would have really liked it.
 
Upvote 0

Forum statistics

Threads
1,224,712
Messages
6,180,517
Members
452,986
Latest member
zenki1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top