单击时突出显示文本框内容 [英] highlighting textbox content upon click
问题描述
我有由标签,复选框和文本框组成的动态生成的用户窗体.单击时是否可以选择文本框的内容?这是我用来创建文本框的方法:
i've got dynamically generated userform consisting of labels, checkboxes and text boxes. is it possible to have a contents of a textbox selected when clicked? this is method i'm using to create textbox:
Set NewTextBox = MainFrame.Controls.Add("Forms.TextBox.1")
With NewTextBox
.Name = "QtyTB" & row
.Value = Cells(cellrow - 1 + row, 11)
.Height = 18
.Left = 210
.Top = 18
.Width = 36
.Enabled = True
.BackColor = RGB(255, 255, 0)
End With
如果我要手动创建文本框,则可以为特定文本框编写on_click子.但是正如我所说,代码从头开始生成一切.因此,如果有一个属性或完成它的其他方法,我将不胜感激.
if i was to create textbox manually i could write on_click sub for specific text box. but as i said, code generates everything from scratch. so if there is a property, or some other way to get it done, i would be gratefull.
推荐答案
是的,可以通过创建具有事件处理功能的类模块来完成
Yes, this can be done by creating a class module with event handling
下面的代码将需要一些修改,因为问题中没有太多代码可做...
The following code will need a bit of adaption as there isn't much code to go on in the question...
在名为TextBoxEventHandler的类模块中
In a class module called TextBoxEventHandler
Private WithEvents FormTextBox As MSForms.TextBox
Public Property Set TextBox(ByVal oTextBox As MSForms.TextBox)
Set FormTextBox = oTextBox
End Property
Private Sub FormTextBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 1 Then
With FormTextBox
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End Sub
然后在用户窗体代码中
Then in the UserForm code
Private CollectionOfEventHandlers As Collection
Private Sub UserForm_Initialize()
Dim i As Long
Dim NewTextBox As MSForms.TextBox
For i = 0 To 4
Set NewTextBox = Me.Controls.Add("Forms.TextBox.1")
With NewTextBox
.Name = "QtyTB" & i ' Row
.Value = "Text " & i ' Cells(cellrow - 1 + Row, 11)
.Height = 18
.Left = 21
.Top = 18 + i * 25
.Width = 36
.Enabled = True
.BackColor = RGB(255, 255, 0)
End With
Next i
Call InitialiseHandlers
End Sub
Private Function InitialiseHandlers()
Set CollectionOfEventHandlers = New Collection
Dim FormControl As Control
For Each FormControl In Me.Controls
If TypeName(FormControl) = "TextBox" Then
Dim EventHandler As TextboxEventHandler
Set EventHandler = New TextboxEventHandler
Set EventHandler.TextBox = FormControl
CollectionOfEventHandlers.Add EventHandler
End If
Next FormControl
End Function
这篇关于单击时突出显示文本框内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!