我正在开发一个带有许多文本框的用户表单的程序:
(希望大家都懂德语)
用户尝试在黄色文本框中输入内容的事件应触发以显示新的用户表单:
这个用户表单包含一个更大的文本框,有更多的空间可以输入大文本。按下“Speichern”按钮后,第二个用户窗体卸载并且文本出现在第一个用户窗体的原始文本框中。因此,我有一个类模块来调用调用第二个用户窗体的 Keypress 事件。
我的问题:我希望在第二个用户表单文本框中看到触发事件的第一个键被按下。
示例:我选择第三个黄色文本框。我想输入“这是一条评论”。通过按下第一个键“T”,新的用户窗体已经在其文本框中打开,并带有“T”,因此我可以继续编写其余的文本,而无需再次输入“T”。
直到现在:我有一个工作代码可以通过按键触发事件。因此,我正在使用一个类模块和一个用户窗体初始化子。不幸的是,文本框还没有显示触发事件的第一个键。
我的班级模块:
Public WithEvents TextBoxEvents As msforms.TextBox
'referring to all textboxes(addmaterialuserform) ~~> restriction in Addmaterialuserform / Userform-Initialize Sub
Private Sub TextBoxEvents_KeyPress(ByVal Keyascii As msforms.ReturnInteger)
'Keypress-event start by clicking any button on keyboard
'for more information https://docs.microsoft.com/de-de/office/vba/api/access.textbox.keypress
Keyascii = 0 'return value ~~> 0 for no return value
UserForm2.CommentBoxUserform.Value = TextBoxEvents.Value 'if small textbox already owns a string it'll be showed in userform (big) textbox
DataBaseSheet.Cells(3, 3).Value = TextBoxEvents.Tag 'saves tag (name) of active comment box to fill up later with string of big textbox
userform2.Show
End Sub
用户表单子:
Private Sub UserForm_Initialize()
'all comment-textboxes have same properties to find in TextBoxClass
'when pressed any keyboard button the addcommentuserform will be opened to have a much bigger textbox to enter comment
Dim myTBs() As New TextBoxClass 'reference to TextBoxclass for Userform-initialize sub
Dim i As Integer
Dim objControl As Control
For Each objControl In Me.Controls
If TypeOf objControl Is msforms.TextBox Then
Select Case objControl.Name 'selects only comment-textboxes by referring to their name
Case "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C11", "C12", "C13", "C14", "C15", "C16" '<~~ Include only these"
i = i + 1
ReDim Preserve myTBs(1 To i) 'call textboxclass
Set myTBs(i).TextBoxEvents = objControl
End Select
End If
Next objControl
Set objControl = Nothing
End Sub