如何使按钮在另一个UserForm中显示可见的对象?

时间:2019-05-01 02:24:41

标签: excel vba

我想制作一个包含多个标签和文本框的UserForm,这些标签和文本框设置为visible = false,并且只有当用户从另一个UserForm中进行选择时,它才会显示我想要的标签和文本框

第一个用户窗体(主要)

Private Sub CommandButton1_Click()

If ComboBox1 = "Test1" Then
Test.Show
 --------------------
ElseIf ComboBox1 = "Test2" Then
Test.Show
 --------------------
Else
MsgBox "Select an option"

End If


End Sub

第二次使用表格(测试)

包含label1和label2,如果Main =“ Test1”中的ComboBox1我希望label1可见

2 个答案:

答案 0 :(得分:2)

我可能会使用第二种形式的属性来完成此操作

表格1可能看起来像这样

 <script charset="utf-8" type="text/javascript">
$(document).ready(function () {
    var element = $("#html-content-holder"); // global variable
    var getCanvas; // global variable

    $("#btn-Preview-Image").on('click', function () {
        html2canvas(element, {
            onrendered: function (canvas) {
                $("#previewImage").append(canvas);
                getCanvas = canvas;

            }
        });
    });

    $("#btn-Convert-Html2Image").on('click', function () {
     //   var imgageData = getCanvas.toDataURL("image/png", 9.5);
        var imgageData = getCanvas.toDataURL("image/png");
        // Now browser starts downloading it instead of just showing it
        var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
        $("#btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData);
    });
});
</script>

表格2可能看起来像这样

Private Sub CommandButton1_Click()
    test.textInForm1ComboBox1 = Me.ComboBox1.Text
End Sub

答案 1 :(得分:0)

第一个版本:当您使用default instance用户格式时,Main中的代码看起来像这样

Private Sub CommandButton1_Click()
    If ComboBox1.Value = "Test1" Then
        Test.Label1.Visible = True
        Test.Label2.Visible = False
    End If
    If ComboBox1.Value = "Test2" Then
        Test.Label1.Visible = False
        Test.Label2.Visible = True
    End If
    Test.Show
End Sub


Private Sub UserForm_Initialize()
    Load Test
    ComboBox1.AddItem "Test1"
    ComboBox1.AddItem "Test2"
End Sub

要使用默认实例,您可能需要查看以下链接,并且自己也可以自己搜索,因为这样做有一定的缺点

VBA Userforms used with no explicit instantiation

Rubberduck: Userform1.show

第二个版本:改进后的版本还需要第二个用户表单中的代码

主代码

Option Explicit
Dim fTest As Test
Private Sub CommandButton1_Click()
    If ComboBox1.Value = "Test1" Then
        fTest.Label1.Visible = True
        fTest.Label2.Visible = False
        fTest.Show
    End If
    If ComboBox1.Value = "Test2" Then
        fTest.Label1.Visible = False
        fTest.Label2.Visible = True
        fTest.Show
    End If
End Sub


Private Sub UserForm_Initialize()
    Set fTest = New Test
    ComboBox1.AddItem "Test1"
    ComboBox1.AddItem "Test2"
End Sub

然后在“测试”中编写代码,然后还需要一个“关闭”按钮

Option Explicit
Private Sub btnClose_Click()
    Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer _
                                       , CloseMode As Integer)

    ' Prevent the form being unloaded
    If CloseMode = vbFormControlMenu Then Cancel = True
    Hide

End Sub

第3版:,带有属性的外观可能如此

主代码

Option Explicit
Dim fTest As Test
Private Sub CommandButton1_Click()
        fTest.whichLbl = ComboBox1.Value
        fTest.Show
End Sub

Private Sub UserForm_Initialize()
    Set fTest = New Test
    ComboBox1.AddItem "Test1"
    ComboBox1.AddItem "Test2"
End Sub

然后在测试中编写代码

Option Explicit
Dim lbl As String
Private Sub btnClose_Click()
    Hide
End Sub

Private Sub UserForm_Activate()
    Select Case lbl
        Case "Test1"
            Label1.Visible = True
            Label2.Visible = False
        Case "Test2"
            Label1.Visible = False
            Label2.Visible = True
        Case Else
            Label1.Visible = False
            Label2.Visible = False
    End Select
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer _
                                       , CloseMode As Integer)

    ' Prevent the form being unloaded
    If CloseMode = vbFormControlMenu Then Cancel = True
    Hide

End Sub

Property Let whichLbl(selLbl As String)
    lbl = selLbl
End Property