VBA: Need way to detect "mousedown" or something like it in dynamically created textbox

khervey

New Member
Joined
Nov 24, 2014
Messages
8
I hope my question is well-formed. If not, please ask for clarification.


Summary: VBA Need way to detect "mousedown" or something like it in dynamically created textbox


I have a dynamically creatd userform which asks for user input using code like:
Set SFPQtyValue = UserForm9.Controls.Add("Forms.TextBox.1", arrayS(varSowCounter - 1, 1) & "datum" & varSowCounter)


User input is a text box which defaults to zero, but after user entry could be 0, 1, 2, etc.


There is a maximum for the total of all text boxes, if the user exceeds the maximum, then all are set to zero.


Problem is that if user accidentally selects the left side of the existing number, then things get too large. If the existing value is "0" and the user wants to enter 2, then the value can become 20.


I want to select all text on entry so that does not happen using code such as:


With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
End With


However, that requires detection that user clicked in the textbox.


It is mandatory requirement that the user not accidentally enter the large number, so I am seeking help.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Why not use the Enter event like this
Code:
Private Sub TextBox1_Enter()
TextBox1.Value = ""
End Sub
 
Upvote 0
Why not use the Enter event like this
Code:
Private Sub TextBox1_Enter()
TextBox1.Value = ""
End Sub
Thanks

I don't know how that would work for textboxes created dynamically.

An example of static that does what I would need is--I just put this together as a learning tool thinking I could use the same technique for dynamically created textboxes:

PPrivate Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)MsgBox "you clicked in the textbox

With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
End With

End Sub

---

But I can't find any way to do the same for textboxes created with

Set SFPQtyValue = UserForm9.Controls.Add("Forms.TextBox.1", arrayS(varSowCounter - 1, 1) & "datum" & varSowCounter)



 
Last edited:
Upvote 0
Could you supply the entire code?
 
Upvote 0
Could you supply the entire code?

That would be very difficult.

I am looping through an array to add an unspecified number of textboxes each on a new row.

I am using the add method: https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/add-method-microsoft-forms

The technique is similar to what Darren shows in this thread, except that I am adding by looping as I mentioned above: https://www.mrexcel.com/forum/excel-questions/509807-how-create-user-form-using-vba.html
 
Upvote 0
If it's possible to do what you're after, I'm afraid I don't know how.
Rather than create them at runtime, why not have them on the userform, but hidden & only make them visible when required?
 
Upvote 0
For catching events of controls created at runtime , do a search on the board for textbox events and Class module ..
 
Upvote 0
For catching events of controls created at runtime , do a search on the board for textbox events and Class module ..

Thanks. I searched before I posted and just now. I am hoping I missed something or somebody knows something that has not been asked before.
 
Upvote 0
This is for Labels but the principle is the same.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

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