VBA 用户表单文件 Select 的省略号文本框

Ellipsis Textbox for VBA Userform File Select

我正在尝试为我一直在使用的扩展 VBA 程序创建路径选择用户界面,但我似乎无法获得我想要的省略号文本框。这是一个非常常见的功能,尤其是在选项表中。这是我想要直接从 VBA 选项面板获得的示例:

我很想找到一种在用户窗体中获得相同功能的方法。到目前为止,我发现的唯一解决方案是使用启用了省略号箭头选项的组合框。但是,似乎没有明显的方法可以使用组合框箭头激活 运行 对话框,似乎也没有办法让它看起来不像组合框。不得已,我在文本框下方使用了一个按钮,但我真的更喜欢使用不那么笨重的方法。

任何解决方案将不胜感激。

The only solution that I've found thus far is to use a combo box with the ellipsis arrow option enabled. However, there doesn't seem to be an apparent way to use the activation of the combo box arrow to run a dialog box, nor does there seem to be a way to make it look UNLIKE a combo box

你的建议确实有效,而且它肯定比让两个控件一起工作更简单、更优雅,Button + Textbox

Combo 可以通过以下方式完美实现所需的功能。

1) 在设计模式下,设置按钮样式为Ellipsis

         DropButtonStyle = fmDropButtonStyleEllipsis

最后,通过设置设计时 属性:

,使省略号仅在输入组合时显示
         ShowDropButtonWhen = ShowDropButtonWhenFocus

2) 如果需要,您可以设置其他设计时属性以具有一些外观。不过默认值看起来不错。

3) 将以下处理程序添加到父用户窗体。代码段 模拟 启动对话框并获取新值或取消。它不显示任何下拉菜单 window。 (但你仍然可以控制它:如果你想根据某些条件显示它,你仍然可以调用方法ComboBox1.DropDown

Private Sub ComboBox1_DropButtonClick()
    ' The following two lines avoid to call the routine twice, at entry and at exit
    Static i As Integer
    i = (i + 1) Mod 2: If i = 0 Then Exit Sub

    With ComboBox1
        s = InputBox("enter some text", , .Value) '<~~ simulates any dialog
        If s <> "" Then .Value = s
        SendKeys ("{Enter}") '<~~ to close immediately the dropdown window
    End With
End Sub

试试看 ;)

不仅组合框有下拉按钮,文本框也有(Excel 的 RefEdit 控件也是如此)。即使您无法在设计时访问文本框的下拉按钮,您也可以在运行时访问。使用文本框可以避免处理组合框的下拉列表。

给定一个名为 TextBox1 的文本框,以下代码将提供所需的省略号下拉按钮:

Private Sub UserForm_Initialize()
  With Me.TextBox1
    .DropButtonStyle = fmDropButtonStyleEllipsis
    .ShowDropButtonWhen = fmShowDropButtonWhenAlways
  End With
End Sub

然后使用文本框的 DropButtonClick 事件执行您想要的任何操作:

Private Sub TextBox1_DropButtonClick()
  '' Code here to do what you need
End Sub

我在 Alternative to Excel’s Flaky RefEdit Control 有一个广泛的示例,它使用带有 "Reduce" 下拉按钮的文本框来复制 Excel 不可靠的 RefEdit 控件的功能。