Excel VBA:切换按钮仅适用于右键单击

时间:2014-10-07 16:04:56

标签: excel vba

我在Excel上使用Userform。从本质上讲,我想要一个" Photoshop-esque"我工作时浮动在电子表格上的工具栏,允许我选择和使用各种工具。

我已经设置了一系列切换按钮,这样当点击一个切换按钮时,任何其他切换按钮都会返回到未切换状态。它有点复杂,因为我有子按钮,如果这是有意义的,这意味着一旦我点击了一个按钮,我可以点击其他四个按钮中的一个来进行实际选择,这四个按钮是相互排斥的彼此也是。

奇怪的是:我还没有能够让这些按钮工作。除了。出于某种原因,当我只右键单击时,按钮就像一个魅力。左键单击:没有。请帮忙吗?

示例按钮代码:

Private Sub tMouse_MouseUp(ByVal button As Integer, _
       ByVal shift As Integer, ByVal X As Single, ByVal Y As Single)


tMouse.Value = True
tActual.Value = False
tSched.Value = False
tX.Value = False
tDiam.Value = False
tCirc.Value = False
tTri.Value = False
tTrash.Value = False
tText.Value = False

End Sub

编辑: 我尝试了关于打印切换按钮值的建议。我的电脑爆炸了消息箱。我已将所有操作更改为Click()事件。显然我是通过无限循环发送计算机。也许将按钮从true更改为false或反之亦然的行为就像点击并触发所有其他点击事件一样?

3 个答案:

答案 0 :(得分:0)

我建议使用“Option”控件而不是“Toggle”控件。 如果您在1帧中粘贴4个选项,那么这4个选项中只有1个会自动一次为真。只有当你想要同时为它的多个实例时才应该使用“ToggleButton”控件。

但是如果你拒绝这样做而且真的想要使用ToggleButtons。然后你可以编写一个程序,只要按下按钮发送它的名字(或其他标识它的东西)作为参数,就可以将除了它之外的所有其他togglebuttons设置为false。

干杯!

Form Reference

Private Sub ToggleButton1_Click()
    Dim s As String
    s = "ToggleButton1"
    Evaluate_Options s
End Sub

Private Sub ToggleButton2_Click()
    Dim s As String
    s = "ToggleButton2"
    Evaluate_Options s
End Sub

Private Sub ToggleButton3_Click()
    Dim s As String
    s = "ToggleButton3"
    Evaluate_Options s
End Sub

Private Sub ToggleButton4_Click()
    Dim s As String
    s = "ToggleButton4"
    Evaluate_Options s
End Sub

Private Sub ToggleButton5_Click()
    Dim s As String
    s = "ToggleButton5"
    Evaluate_Options s
End Sub

Private Sub ToggleButton6_Click()
    Dim s As String
    s = "ToggleButton6"
    Evaluate_Options s
End Sub

Private Sub ToggleButton7_Click()
    Dim s As String
    s = "ToggleButton7"
    Evaluate_Options s
End Sub

Private Sub ToggleButton8_Click()
    Dim s As String
    s = "ToggleButton8"
    Evaluate_Options s
End Sub

Private Sub Evaluate_Options(s As String)
    Dim tgl As Control

    For Each tgl In UserForm1.Frame1.Controls
        If InStr(tgl.Name, s) Then Set_Toggles_1 tgl
    Next
    For Each tgl In UserForm1.Frame2.Controls
        If InStr(tgl.Name, s) Then Set_Toggles_2 tgl
    Next
End Sub

Private Sub Set_Toggles_1(tglTrue As Control)
    Dim tglFalse As Control
    For Each tglFalse In UserForm1.Frame1.Controls
        If tglFalse.Name = tglTrue.Name Then tglFalse = True Else tglFalse = False
    Next
End Sub

Private Sub Set_Toggles_2(tglTrue As Control)
Dim tglFalse As Control
    For Each tglFalse In UserForm1.Frame2.Controls
        If tglFalse.Name = tglTrue.Name Then tglFalse = True Else tglFalse = False
    Next
End Sub

答案 1 :(得分:0)

尝试这些基本的鼠标事件捕获子并根据需要进行修改(tMouse =切换按钮名称):

Private Sub tMouse_Click()
'MsgBox "tb value = " & tMouse.Value
End Sub

注意:如果较低的子名称为

,则上子不起作用
Private Sub tMouse_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

If Button = 1 Then MsgBox "Left"
If Button = 2 Then MsgBox "Right"
End Sub

答案 2 :(得分:0)

您有触发其他事件的事件。您需要做的是设置一个布尔AllowEventsToRun变量(在模块或公共级别)并在代码的开头将其设置为false。运行您需要做的任何事情,然后在结束时将其设置为true。

诀窍是执行if语句以确保在运行任何其他代码之前将AllowEventsToRun设置为true。加载userform时,请确保将Boolean初始化为true(因为布尔值的默认值为false。所以像这样:

Option Explicit
Private AllowEventsToRun As Boolean

Private Sub ToggleButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If AllowEventsToRun Then

    AllowEventsToRun = False

    'whatever you're doing that's causing the events to chain fire

    AllowEventsToRun = True
End If

End Sub


Private Sub ToggleButton2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If AllowEventsToRun Then

    AllowEventsToRun = False

    'whatever you're doing that's causing the events to chain fire

    AllowEventsToRun = True
End If
End Sub


Private Sub UserForm_Initialize()
AllowEventsToRun = True
End Sub

来源:http://www.cpearson.com/excel/SuppressChangeInForms.htm