热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

当鼠标放入文本框或组合框时,使单位可见-Makeunitsvisiblewhenmousingintotextboxorcombobox

Ihaveauserformthatrequiresinputintovarioustextboxesandcomboboxes.Theinputsareformeas

enter image description hereI have a userform that requires input into various textboxes and comboboxes. The inputs are for measurements and have units associated with them. I want the userform to display which units are expected to be used when you click into each specific text/combobox.

我有一个userform,需要输入各种文本框和组合框。输入用于测量并具有与之相关的单元。我希望userform显示当您单击每个特定文本/组合框时预期使用的单位。

Currently, my code works for textboxes but does not recognize comboboxes. Here is my code.

目前,我的代码适用于文本框,但不识别组合框。这是我的代码。

Private Sub TextboxActions_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If InStr(1, TextboxActions.Name, "OD") > 0 Then
    UserForm1.inUnit.Visible = True
    UserForm1.mmUnit.Visible = False
    UserForm1.eaUnit.Visible = False

ElseIf InStr(1, TextboxActions.Name, "CE") > 0 Then
    UserForm1.inUnit.Visible = False
    UserForm1.mmUnit.Visible = True
    UserForm1.eaUnit.Visible = False

Else
    UserForm1.inUnit.Visible = False
    UserForm1.mmUnit.Visible = False
    UserForm1.eaUnit.Visible = True
End If
End Sub

Using this, when the user clicks a textbox it will display the units in, mm, or ea. I want this to be the case for comboboxes as well, but currently nothing happens when clicking into a combobox.

使用此功能,当用户单击文本框时,它将以mm,mm或ea显示单位。我希望这也是组合框的情况,但是目前在点击组合框时没有任何反应。

2 个解决方案

#1


2  

Using 3 Enter() events worked for me:

使用3个Enter()事件为我工作:

Private Sub ComboBox1_Enter()

UserForm1.Label1.Visible = True
UserForm1.Label2.Visible = False
UserForm1.Label3.Visible = False

End Sub
Private Sub ComboBox2_Enter()

UserForm1.Label1.Visible = False
UserForm1.Label2.Visible = True
UserForm1.Label3.Visible = False

End Sub
Private Sub ComboBox3_Enter()

UserForm1.Label1.Visible = False
UserForm1.Label2.Visible = False
UserForm1.Label3.Visible = True

End Sub

Example of clicking in ComboBoxes (In top Userform ComboBox1 is active, in second Userform ComboBox3 is active, etc.)

单击ComboBoxes的示例(在顶部Userform ComboBox1处于活动状态,在第二个Userform ComboBox3处于活动状态,等等)

img

#2


2  

First, I would use Enter event instead of Mouse up event, so same thing will happen when you Tab into the control. For each control's Enter event simply call this ShowUnit function and feed it the controls name.

首先,我会使用Enter事件而不是Mouse up事件,因此当Tab键进入控件时会发生同样的事情。对于每个控件的Enter事件,只需调用此ShowUnit函数并为其提供控件名称。

Private Sub CE2_Enter()
    Call ShowUnit(CE2)
End Sub

Private Sub OD1_Enter()
    Call ShowUnit(OD1)
End Sub

Private Sub ShowUnit(ByRef oControl As Control)
    If InStr(1, oControl.Name, "OD") > 0 Then
        UserForm1.inUnit.Visible = True
        UserForm1.mmUnit.Visible = False
        UserForm1.eaUnit.Visible = False

    ElseIf InStr(1, oControl.Name, "CE") > 0 Then
        UserForm1.inUnit.Visible = False
        UserForm1.mmUnit.Visible = True
        UserForm1.eaUnit.Visible = False

    Else
        UserForm1.inUnit.Visible = False
        UserForm1.mmUnit.Visible = False
        UserForm1.eaUnit.Visible = True
    End If
End Sub

edit: Changed names of controls to illustrate how to use the code and returned "OD" and "CE" in the instr() functions from my test values.

编辑:更改控件名称以说明如何使用代码并从我的测试值返回instr()函数中的“OD”和“CE”。

For additional clarification see the image below to see what's going on. The Blue is how you can use the GUI to ensure you are referencing the Controls Event. Red is indicating that we pass the full Control to the ShowUnit Function. The Purple is showing that we take are reading the Controls Name property to search for "OD" or "CE" note that these are case sensitive without using instr(1, UCASE(oControl.Name), "OD") > 0, so if your controls name is odSomeName it won't make the inUnit label visible.

有关其他说明,请参阅下图,了解正在发生的情况。 Blue是如何使用GUI来确保您引用控件事件的。红色表示我们将完整的Control传递给ShowUnit函数。紫色显示我们正在读取Controls Name属性来搜索“OD”或“CE”注意这些是区分大小写而不使用instr(1,UCASE(oControl.Name),“OD”)> 0,所以如果您的控件名称是odSomeName,则不会使inUnit标签可见。

enter image description here


推荐阅读
author-avatar
Amyb__ing舒
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有