第20章 构件用户窗体

20.1 用户窗体开发特性 

20.2 预览一个简单窗体

 

代码清单20.1: 简单的窗体,简单的代码

 

ExpandedBlockStart.gif代码
'代码清单20.1: 简单的窗体,简单的代码
Private Sub cmdCancel_Click()
    Unload 
Me
End Sub

Private Sub cmdOK_Click()
    SaveSheetName
    Unload 
Me
End Sub

Private Sub SaveSheetName()
    
On Error Resume Next
    ActiveSheet.Name 
= txtActiveSheet.Text
End Sub

Private Sub UserForm_Initialize()
    txtActiveSheet.Text 
= ActiveSheet.Name
    
    
'Pre-select all of the text in the text box.
    txtActiveSheet.SelStart = 0
    txtActiveSheet.SelLength 
= Len(txtActiveSheet.Text)
End Sub

 

 

20.3 窗体意味着需要显示

20.3.1 先显示,再提问

代码清单20.2: 使用Show方法显示窗体

 

ExpandedBlockStart.gif代码

'代码清单20.2: 使用Show方法显示窗体
Sub SimpleFormExample()
    
'Show form modally
    ShowSimpleForm True    
    
MsgBox "ok - same form now, but modeless.", vbOKOnly
 
    'Show form modeless  
    ShowSimpleForm 
False    
    
MsgBox "Exiting the simpleFormExample procedure.", vbOKOnly
End Sub

'display the simple form
Sub ShowSimpleForm(bModal As Boolean)
    
If bModal Then
        frmSimpleForm.Show vbModal
    
Else
        frmSimpleForm.Show vbModeless
    
End If
End Sub

 

20.3.2 装入和现实

代码清单20.3: 在显示之前向内存装入一个窗体

 

ExpandedBlockStart.gif代码
'代码清单20.3: 在显示之前向内存装入一个窗体
'Modify the simple form before showing it
Sub ModifySimpleForm()
    
Dim sNewCaption As String    
    
'Load the form into memory
    Load frmSimpleForm    
    
'Prompt for a new caption
    sNewCaption = InputBox("Enter a caption for the form.")    
    
'Set the new caption
    frmSimpleForm.Caption = sNewCaption    
    
'Show another instance of the form
    MsgBox "OK - same form again except with default caption", vbOKOnly
    frmSimpleForm.Show
End Sub

 

20.3.3 Classy窗体

代码清单20.4:Classy窗体的代码

 

'代码清单20.4:Classy窗体的代码

Private Sub cmdOK_Click()
    
Me.Hide
End Sub

 

代码清单20.5: Classy窗体范例

 

ExpandedBlockStart.gif代码
'代码清单20.5: Classy窗体范例
Sub ClassyFormExample()
    
Dim frm As frmClassy
    
Dim vResponse As Variant
    
    
'Instantiate frmClassy
    'This has the same effect as: Load frmClassy
    Set frm = New frmClassy
    
    
'Prefill the edit box with a value(just for fun)
    frm.txtStuff = "Good Stuff"
    frm.Show
    
    
'Form is now hidden, but you can still manipulate it
    vResponse = MsgBox("the classy form text box says: " & frm.txtStuff & ". View again ?", vbYesNo)
    
    
If vResponse = vbYes Then
        
'The form is still alive - show it
        'See - txtStuff has the same value as before
        frm.Show
    
End If
    
    
'RIP o Classy one
    Set frm = Nothing
End Sub

 

代码清单20.6: Classy窗体的多个实例

 

ExpandedBlockStart.gif代码
'代码清单20.6: Classy窗体的多个实例

Sub ClassyFormExample2()
    
Dim frm1 As frmClassy
    
Dim frm2 As frmClassy
    
    
Set frm1 = New frmClassy
    
With frm1
        .Caption 
= "I am Classy"
        .Show
    
End With
    
    
Set frm2 = New frmClassy
    
With frm2
        .Caption 
= "I am Classy too."
        .txtStuff 
= "I am Classy said '" & frm1.txtStuff & "'"
        .Show
    
End With
    
    
Set frm1 = Nothing
    
Set frm2 = Nothing    
End Sub

 

 

20.4 窗体的生命周期

代码清单20.7: 跟踪窗体事件

 

ExpandedBlockStart.gif代码
'代码清单20.7: 跟踪窗体事件

Dim mws As Worksheet
Dim msColor As String

Private Sub chkGridlines_Click()
    RecordEvent chkGridlines.Name, 
"Click"
    ActiveWindow.DisplayGridlines 
= chkGridlines.Value
    SetSummary
End Sub

Private Sub chkWeirdFont_Click()
    
'It is possible that the font "Bradley Hand ITC"
    'may not be present on every PC
    On Error Resume Next
    
    RecordEvent chkWeirdFont.Name, 
"Click"
    
    
If chkWeirdFont.Value Then
        mws.Cells.Font.Name 
= "Bradley Hand ITC"
    
Else
        mws.Cells.Font.Name 
= "Arial"
    
End If
    SetSummary
End Sub

Private Sub cmdHide_Click()
    RecordEvent cmdHide.Name, 
"Click"
    
Me.Hide
    
'Pause for brief period and
    'then reshow the form
    Application.Wait Now + 0.00003
    
Me.Show
End Sub

Private Sub cmdOK_Click()
    RecordEvent cmdOK.Name, 
"Click"
    Unload 
Me
End Sub

Private Function RecordEvent(sControl As String, sEvent As String)
    
Dim rg As Range
    
    
Set rg = mws.Cells(655361).End(xlUp).Offset(10)
    rg.Value 
= sControl
    rg.Offset(
01).Value = sEvent
    
Set rg = Nothing
End Function

Private Sub frmOptions_Click()
    RecordEvent frmOptions.Name, 
"Click"
End Sub

Private Sub optBlack_Change()
    RecordEvent optBlack.Name, 
"Change"
End Sub

Private Sub optBlack_Click()
    RecordEvent optBlack.Name, 
"Click"
    msColor 
= "Black"
    mws.Cells.Font.Color 
= vbBlack
    
    SetSummary
End Sub

Private Sub optBlue_Change()
    RecordEvent optBlue.Name, 
"Change"
End Sub

Private Sub optBlue_Click()
    RecordEvent optBlue.Name, 
"Click"
    msColor 
= "Blue"
    mws.Cells.Font.Color 
= vbBlue    
End Sub

Private Sub optGreen_Change()
    RecordEvent OptGreen.Name, 
"Change"

End Sub

Private Sub optGreen_Click()
    RecordEvent OptGreen.Name, 
"Click"
    msColor 
= "Green"
    mws.Cells.Font.Color 
= vbGreen    
End Sub

Private Sub txtName_AfterUpdate()
    RecordEvent txtName.Name, 
"AfterUpdate"
    mws.Name 
= txtName.Value
    SetSummary    
End Sub

Private Sub txtName_Change()
    
On Error Resume Next
    RecordEvent txtName.Name, 
"Change"    
End Sub

Private Sub UserForm_Activate()
    RecordEvent 
Me.Name, "Activate"
    
End Sub

Private Sub UserForm_Deactivate()
    RecordEvent 
Me.Name, "Deactivate"    
End Sub

Private Sub UserForm_Initialize()
    
On Error GoTo ErrHandler
    
    
'Refer via worksheet code name
    'since this form can change the display name
    Set mws = wsEventTracing
    
    RecordEvent 
Me.Name, "initialize"
    
    
'Activate the worksheet so you
    'can watch the events occur
    mws.Activate
    
    
'Initialize controls on the form
    chkGridlines.Value = ActiveWindow.DisplayGridlines
    txtName.Text 
= mws.Name
    
If mws.Cells.Font.Name <> "Bradley Hand ITC" Then
        chkWeirdFont.Value 
= False
    
Else
        chkWeirdFont.Value 
= True
    
End If
    InitializeBackgroundOptions
    SetSummary    
    
Exit Sub
ErrHandler:
    Debug.Print 
"UserForm_Initialize: " & Err.Description
    Unload 
Me
End Sub

Private Sub InitializeBackgroundOptions()
    
Select Case mws.Cells.Font.Color
        
Case vbBlack
            optBlack.Value 
= True
            msColor 
= "Black"
        
Case vbBlue
            optBlue.Value 
= True
            msColor 
= "Blue"
        
Case vbGreen
            OptGreen.Value 
= True
            msColor 
= "Green"
        
Case Else
            mws.Cells.Interior.Color 
= vbBlack
            optBlack.Value 
= True
    
End Select
End Sub

Private Sub SetSummary()
    
Dim sGridlines As String
    
Dim sColor As String
    
Dim sFont As String
    
    
If chkWeirdFont.Value Then
        sFont 
= "weird"
    
Else
        sFont 
= "Standard"
    
End If
    
    lblSummary.Caption 
= mws.Name & " shows its data " & _
        sGridlines 
& " using a " & sFont & "" & _
        msColor 
& " font "
End Sub

 

 

20.5 用户友好设置

代码清单20.8: 管理Settings窗体

 

ExpandedBlockStart.gif代码
'代码清单20.8: 管理Settings窗体
Dim moSetting As setting
Dim moSettings As settings

Private Sub cboSetting_Change()
    
'Get indicated setting and update
    'controls appropriately
    RefreshControls
End Sub

Private Sub cmdCancel_Click()
    Unload 
Me
End Sub

Private Sub cmdEdit_Click()
    
Dim sPassword As String
    
    
If Not moSetting Is Nothing Then
        
'for setReadProtectedWrite, you need to call
        'ChangeEditMode using the Password parameter
        If moSetting.settingtype = setReadProtectedWrite Then
            
'have the user fill in their password
            frmPassword.Show
            sPassword 
= frmPassword.Password
            Unload frmPassword
            
            
'make sure they entered a password
            If frmPassword.Tag = CStr(vbCancel) Then Exit Sub
            
            
'try and change the edit mode
            If moSetting.changeeditmode(True, sPassword) Then
                txtValue.Enabled 
= True
            
Else
                txtValue.Enabled 
= False
                
MsgBox "invalid password", vbOKOnly
            
End If
        
Else
            
'Don't need a password for unrestricted
            'read/write settings.
            moSetting.changeeditmode True
        
End If
    
End If    
End Sub

Private Sub cmdSave_Click()
    
If Not moSetting Is Nothing Then
        moSetting.Value 
= txtValue.Text
        
'turn off editing ablility
        moSetting.changeeditmode False
        cmdSave.Enabled 
= False
        txtValue.Enabled 
= False
    
End If    
End Sub

Private Sub txtValue_Change()
    cmdSave.Enabled 
= True
End Sub

Private Sub UserForm_Initialize()
    
Set moSettings = New settings
    cmdSave.Enabled 
= False
    
    
'load cbosetting with settings
    LoadSettings
    
    
'default to first setting in list
    If cboSetting.ListCount > 0 Then
        cboSetting.ListIndex 
= 0
    
End If
End Sub

Private Sub LoadSettings()
    
Dim lRow As Long
    
Dim oSetting As setting
    
Dim nSettingCount As Integer
    
Dim nSetting As Integer
    
    nSettingCount 
= moSettings.Count
    
    
'exit if there are not any settings
    If nSettingCount = 0 Then Exit Sub
    
    
For nSetting = 1 To nSettingCount
        
'Get setting
        Set oSetting = moSettings.Item(nSetting)
        
        
'add all settings except private settings
        If oSetting.settingtype <> setprivate Then
            cboSetting.AddItem oSetting.Name
        
End If
    
Next
    
    
Set oSetting = Nothing
End Sub

Private Sub RefreshControls()
    
Dim sSetting As String
    
Dim sValue As String
    
Dim sComment As String
    
    
Set moSetting = moSettings.Item(cboSetting.Value)
    
If Not moSetting Is Nothing Then
    
        
'disable edit ablility for read-only settings
        If moSetting.settingtype = setreadonly Then
            cmdEdit.Enabled 
= False
        
Else
            
'enable edit ablility for other settings
            cmdEdit.Enabled = True
        
End If
        
        txtValue.Text 
= moSetting.Value
        txtDescription.Text 
= moSetting.Description
    
End If
    
    txtValue.Enabled 
= False
    cmdSave.Enabled 
= False
End Sub

 

20.5.1 原始口令集

代码清单20.9: Password窗体使用的事件过程

 

ExpandedBlockStart.gif代码
'代码清单20.9: Password窗体使用的事件过程
Dim msPassword As String

Public Property Get Password() As Variant
    Password 
= msPassword
End Property

Private Sub cmdCancel_Click()
    msPassword 
= CStr(vbCancel)
    
'tag form to indicate how the form was dispatched
    Me.Tag = vbCancel
    
Me.Hide
End Sub

Private Sub cmdOK_Click()
    msPassword 
= txtPassword.Text
    
'tag form to indicate how the form was dispatched
    Me.Tag = vbOK
    
Me.Hide
End Sub

Private Sub UserForm_Initialize()
    txtPassword.SetFocus
End Sub

 

代码清单20.10: 从Password窗体中检索口令

 

ExpandedBlockStart.gif代码
'代码清单20.10: 从Password窗体中检索口令

Sub DemonstratePassword()
    
'Example 1: Retrieve password by inspecting txtPassword.value
    frmPassword.Show
    
If frmPassword.Tag <> vbCancel Then
        
MsgBox "you entered: " & frmPassword.txtPassword.Value, vbOKOnly
    
Else
        
MsgBox "you clicked cancel.", vbOKOnly
    
End If
    
    
'unload form from memory
    Unload frmPassword
    
    
'Example 2: Retrieve password as a property of the form
    frmPassword.Show
    
If frmPassword.Tag <> vbCancel Then
        
MsgBox "you entered: " & frmPassword.Password, vbOKOnly
    
Else
        
MsgBox "you clicked cancel.", vbOKOnly
    
End If
    
    
'unload form from memory
    Unload frmPassword
End Sub

 

 

转载于:https://www.cnblogs.com/csl-office-vb-sql-net/archive/2010/01/21/1653257.html

### MCGS 用户定制构件的功能介绍 MCGS是一款广泛应用于工业自动化领域的组态软件,其中的用户定制构件功能允许开发者根据具体需求创建个性化的组件。这些构件可以集成到MCGS项目中,从而扩展系统的功能并提升用户体验。 #### 定制构件的核心特点 这款构件的设计理念基于高度定制和强兼容性的原则[^1],这意味着开发人员可以根据实际应用环境调整构件的行为和外观。通过这种灵活性,用户可以在不同的硬件平台上部署相同的解决方案,减少重复工作量的同时提高效率。 #### 配置与使用说明 对于某些类型的设备连接,比如串口通信,通常需要设置相应的参数来确保数据交换正常进行。例如,在配置通用串口父设备时,主要涉及选择合适的通信线路及设定诸如波特率、数据位数、停止位数目以及奇偶校验方式等基本属性[^2]。这类细致入微的调节有助于增强系统稳定性,并使整个网络架构更加健壮可靠。 #### 特定版本支持情况 以昆仑通泰触摸屏为例,其发布的McgsPro视频播放构件V2.1专门针对该品牌旗下的G系列显示屏做了适配处理,尤其是像1570Gi这样的高分辨率型号[^3]。此外,为了保证最佳效果,建议搭配指定版次的McgsPro组态软件(即V3.3.1.4104 SP1.3),这样不仅可以获得更佳的操作体验,还能充分利用两者间深层次的技术协作优势。 以下是简单的Python脚本示例用于演示如何加载自定义库文件至当前工程目录下: ```python import sys sys.path.append('path_to_your_custom_component') # 添加路径以便导入模块 from custom_component import CustomWidget # 假设有一个名为CustomWidget 的类代表我们的新控件 widget_instance = CustomWidget() # 创建实例对象 print(widget_instance.get_properties()) # 输出一些默认特性供调试查看 ``` 上述代码片段展示了怎样动态引入外部资源包进入现有程序框架之中,这对于构建复杂的图形界面或者多功能应用程序非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值