Jim*_*myK 21 excel vba module userform
我在表单上有以下按钮:
Private Sub CommandButton1_Click()
Dim pass As String
pass = UserForm1.TextBox1
Unload UserForm1
End Sub
Run Code Online (Sandbox Code Playgroud)
然后我有一个名为Module1的模块:
Public Sub Login()
...
UserForm1.Show
driver.findElementByName("PASSWORD").SendKeys pass
...
End Sub
Run Code Online (Sandbox Code Playgroud)
这个想法是用户输入到输入框的任何密码都将被分配给变量pass.然而,我遇到的麻烦是pass从UserForm1 传递到Module1的Login子.
我想Module1.Login (pass)在卸载它之前添加类似于我的表单的东西会起作用,但是这似乎没有通过任何东西.任何帮助将非常感激.谢谢.
Sid*_*out 31
不要在userform中声明变量.Public在模块中声明它.
Public pass As String
Run Code Online (Sandbox Code Playgroud)
在Userform中
Private Sub CommandButton1_Click()
pass = UserForm1.TextBox1
Unload UserForm1
End Sub
Run Code Online (Sandbox Code Playgroud)
在模块中
Public pass As String
Public Sub Login()
'
'~~> Rest of the code
'
UserForm1.Show
driver.findElementByName("PASSWORD").SendKeys pass
'
'~~> Rest of the code
'
End Sub
Run Code Online (Sandbox Code Playgroud)
您可能还想在调用该driver.find...行之前添加一个额外的检查?
If Len(Trim(pass)) <> 0 Then
Run Code Online (Sandbox Code Playgroud)
这将确保不传递空字符串.
Mat*_*don 21
Siddharth的答案很好,但依赖于全局范围的变量.有一种更好,更友好的OOP方式.
UserForm是一个类模块,与其他任何类似 - 唯一的区别是它有一个隐藏VB_PredeclaredId属性设置为True,这使得VB创建一个以类命名的全局范围对象变量 - 这就是你可以在UserForm1.Show不创建新实例的情况下编写的类.
远离这个,并将您的表单视为对象 - 公开Property Get成员并抽象出表单的控件 - 调用代码无论如何都不关心控件:
Option Explicit
Private cancelling As Boolean
Public Property Get UserId() As String
UserId = txtUserId.Text
End Property
Public Property Get Password() As String
Password = txtPassword.Text
End Property
Public Property Get IsCancelled() As Boolean
IsCancelled = cancelling
End Property
Private Sub OkButton_Click()
Me.Hide
End Sub
Private Sub CancelButton_Click()
cancelling = True
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
cancelling = True
Me.Hide
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
现在调用代码可以执行此操作(假设UserForm已命名LoginPrompt):
With New LoginPrompt
.Show vbModal
If .IsCancelled Then Exit Sub
DoSomething .UserId, .Password
End With
Run Code Online (Sandbox Code Playgroud)
哪些DoSomething程序需要两个字符串参数:
Private Sub DoSomething(ByVal uid As String, ByVal pwd As String)
'work with the parameter values, regardless of where they came from
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100503 次 |
| 最近记录: |