Мяв! Нужно по событию в Textbox'e получить ссылку на этот Textbox. Если он расположен непосредственно на форме, проблем нет. А как быть, если он на фрейме или мультипейдж? [vba]
Код
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim x As Object Set x = ActiveControl Call qq(x) End Sub Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim x As Object Set x = ActiveControl Call qq(x) End Sub Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim x As Object Set x = ActiveControl Call qq(x) End Sub Sub qq(x As Object) MsgBox x.Name End Sub
[/vba]
Мяв! Нужно по событию в Textbox'e получить ссылку на этот Textbox. Если он расположен непосредственно на форме, проблем нет. А как быть, если он на фрейме или мультипейдж? [vba]
Код
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim x As Object Set x = ActiveControl Call qq(x) End Sub Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim x As Object Set x = ActiveControl Call qq(x) End Sub Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim x As Object Set x = ActiveControl Call qq(x) End Sub Sub qq(x As Object) MsgBox x.Name End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Me.Tag = "TextBox1" Call qq End Sub Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Me.Tag = "TextBox2" Call qq End Sub Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Me.Tag = "TextBox3" Call qq End Sub Sub qq() MsgBox Me.Tag End Sub
[/vba] upd или вместо Activecontrol пишем Textbox1,2,3 - не пойдет?
например [vba]
Код
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Me.Tag = "TextBox1" Call qq End Sub Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Me.Tag = "TextBox2" Call qq End Sub Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Me.Tag = "TextBox3" Call qq End Sub Sub qq() MsgBox Me.Tag End Sub
[/vba] upd или вместо Activecontrol пишем Textbox1,2,3 - не пойдет?nilem
Яндекс.Деньги 4100159601573
Сообщение отредактировал nilem - Пятница, 20.10.2017, 11:29
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Call fnGetActiveObjectPath End Sub Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Call fnGetActiveObjectPath End Sub Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Call fnGetActiveObjectPath End Sub Sub fnGetActiveObjectPath() Dim obj As Object, sPath As String sPath = Me.Caption Set obj = Me.ActiveControl Do If obj Is Nothing Then sPath = "Nothing" Exit Do ElseIf TypeOf obj Is MSForms.Frame Or TypeOf obj Is MSForms.Page Then sPath = obj.Name Set obj = obj.ActiveControl ElseIf TypeOf obj Is MSForms.MultiPage Or TypeOf obj Is MSForms.TabStrip Then sPath = obj.Name Set obj = obj.SelectedItem Else ' must be Tab or native control sPath = obj.Name Exit Do End If Loop MsgBox sPath End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Call fnGetActiveObjectPath End Sub Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Call fnGetActiveObjectPath End Sub Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Call fnGetActiveObjectPath End Sub Sub fnGetActiveObjectPath() Dim obj As Object, sPath As String sPath = Me.Caption Set obj = Me.ActiveControl Do If obj Is Nothing Then sPath = "Nothing" Exit Do ElseIf TypeOf obj Is MSForms.Frame Or TypeOf obj Is MSForms.Page Then sPath = obj.Name Set obj = obj.ActiveControl ElseIf TypeOf obj Is MSForms.MultiPage Or TypeOf obj Is MSForms.TabStrip Then sPath = obj.Name Set obj = obj.SelectedItem Else ' must be Tab or native control sPath = obj.Name Exit Do End If Loop MsgBox sPath End Sub
Manyasha, Спасибо за идею и ссылку. В итоге сделал [vba]
Код
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) MsgBox GetActiveBox(ActiveControl).Name End Sub Function GetActiveBox(oParent As Object) As Object Dim oCild As Object Static sBoxName$ sBoxName = "" On Error GoTo err_ If TypeOf oParent Is MSForms.MultiPage Or TypeOf oParent Is MSForms.TabStrip Then Set oCild = oParent.SelectedItem Call GetActiveBox(oCild) ElseIf oParent.Controls.Count Then Set oCild = oParent.ActiveControl Call GetActiveBox(oCild) End If Set GetActiveBox = Controls(sBoxName) Exit Function err_: If Len(sBoxName) Then Else sBoxName = oParent.Name: Set GetActiveBox = Controls(sBoxName) End Function
[/vba] Достает активный Box любой вложенности.
Manyasha, Спасибо за идею и ссылку. В итоге сделал [vba]
Код
Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) MsgBox GetActiveBox(ActiveControl).Name End Sub Function GetActiveBox(oParent As Object) As Object Dim oCild As Object Static sBoxName$ sBoxName = "" On Error GoTo err_ If TypeOf oParent Is MSForms.MultiPage Or TypeOf oParent Is MSForms.TabStrip Then Set oCild = oParent.SelectedItem Call GetActiveBox(oCild) ElseIf oParent.Controls.Count Then Set oCild = oParent.ActiveControl Call GetActiveBox(oCild) End If Set GetActiveBox = Controls(sBoxName) Exit Function err_: If Len(sBoxName) Then Else sBoxName = oParent.Name: Set GetActiveBox = Controls(sBoxName) End Function