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
 
How about specifying 2 digits and instructing users to put a 0 in front of any number less than 10...??​
Because 1) there is nothing wrong with a 1 digit number and
2) I am opposed (in principle) to making users live with the stupidity of software.

Gene
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about specifying 2 digits and instructing users to put a 0 in front of any number less than 10...??​
Because 1) there is nothing wrong with a 1 digit number and
2) I am opposed (in principle) to making users live with the stupidity of software.
The problem, as was reported earlier and which you repeated, is "how does the textbox know whether 1 is the number or 11 is your intent when you enter a 1 in an empty textbox"? I guess you could ask the textbox to wait to see if a second digit is going to be entered before reacting... although how long would be an acceptable delay for someone wanting to enter a single digit number?
 
Upvote 0
The problem, as was reported earlier and which you repeated, is "how does the textbox know whether 1 is the number or 11 is your intent when you enter a 1 in an empty textbox"? I guess you could ask the textbox to wait to see if a second digit is going to be entered before reacting... although how long would be an acceptable delay for someone wanting to enter a single digit number?

That will not do it Rick. The ENTIRE code executed after the first digit is entered. When the 2nd digit is entered, the code is executed again. All that would do is delay the execution twice (or three times or whatever the number of digits is)

Gene
 
Upvote 0
Gene

If you don't want the entire code to be executed when only one digit has been entered try what I originally suggested.


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

' if only 1 digit has been entered exit the sub and don't execute any other code

If Len(prime) = 1 Then Exit Sub

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
 
Upvote 0
That will not do it Rick. The ENTIRE code executed after the first digit is entered. When the 2nd digit is entered, the code is executed again. All that would do is delay the execution twice (or three times or whatever the number of digits is)
Not if I coded it correctly (assuming I can actually do that, which I have not looked at yet), so... What would be an acceptable delay for you to wait before the code decides only one digit will be typed in?
 
Upvote 0
No Norie. This will not work, since a one digit number is also a valid entry. It will also not work for a second reason. The moment the first digit is pressed - EVEN IF A TWO DIGIT NUMBER IS INTENDED - your line will be executed and the user will be thrown out. In short, your solution means NO INPUT will ever occur.

Gene



Norie;338183 7 said:
Gene

If you don't want the entire code to be executed when only one digit has been entered try what I originally suggested.


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

' if only 1 digit has been entered exit the sub and don't execute any other code

If Len(prime) = 1 Then Exit Sub

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
 
Upvote 0
Not if I coded it correctly (assuming I can actually do that, which I have not looked at yet), so... What would be an acceptable delay for you to wait before the code decides only one digit will be typed in?

Rick I suspect you can't - no offense - I don't think anyone can. I think the fact the the code is executed the moment a digit is entered is a bug in the system. But to answer your question - two seconds.

Gene
 
Upvote 0
Gene

They user will be thrown out of what?

All that will happen is the change event will be exited.

The user can continue typing in the textbox, they won't even notice anything.

What values do you want to allow?

When should the code run?

Why are you using the Change event?

You could easily add a command button and have that run the code when clicked.
 
Upvote 0
Gene

They user will be thrown out of what?

All that will happen is the change event will be exited.

The user can continue typing in the textbox, they won't even notice anything.

What values do you want to allow?

When should the code run?

Why are you using the Change event?

You could easily add a command button and have that run the code when clicked.

I did something like your suggestion. I made a command button, renamed it analyze, (cuz that sounds impressive) changed the textbox change to after update (as suggested above) and had the command button code DO NOTHING. It rubs me the wrong way that an extra button need be used, but as kludges go, it could be worse

Gene


Code:
Private Sub CommandButton1_Click()
'No need to do anything if I am right
End Sub
 
Upvote 0
Rick I suspect you can't - no offense - I don't think anyone can. I think the fact the the code is executed the moment a digit is entered is a bug in the system. But to answer your question - two seconds.
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
 
Upvote 0

Forum statistics

Threads
1,223,643
Messages
6,173,520
Members
452,518
Latest member
SoerenB

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