用户窗体 VBA 的列表框项目上的双击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45323704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:58:36  来源:igfitidea点击:

Double Click Event on the items of the ListBox of a Userform VBA

excelvbaformslistbox

提问by D.Tailleur

I am trying to do a small "save as" interface on excel using vba, so that I can save some of the information that i have putted in my sheet in another sheet. I am almost done with it and it looks like this: click here to see my Save As interface

我正在尝试使用 vba 在 excel 上做一个小的“另存为”界面,以便我可以将我放在我的工作表中的一些信息保存在另一个工作表中。我快完成了,它看起来像这样:单击此处查看我的另存为界面

My problem is that I want to put an event on double clicking on one of the elements of my list, let's say SoCs2. On double click, I want the string "SoCs2" to appear in the textbox below.

我的问题是我想在我的列表中的一个元素上双击放置一个事件,比如说 SoCs2。双击时,我希望字符串“SoCs2”出现在下面的文本框中。

I tried something like that:

我试过这样的事情:

Private Sub Listbox1_BeforeDoubleClick(ByVal Cancel As MSForms.ReturnBoolean)

With Me.ListBox1
     For i = 0 To .ListCount - 1
       If .Selected(i) Then
         Me.TextBox1.Value = .List(i, 0)
         Exit For
       End If
     Next
End With

End Sub

My ListBox is called ListBox1 and my TextBox is called TextBox1. Unfortunately, this code doesn't work: when I double click on one of the items of my ListBox, it does absolutely nothing. Can somebody help me with this issue ?

我的 ListBox 被称为 ListBox1,而我的 TextBox 被称为 TextBox1。不幸的是,这段代码不起作用:当我双击 ListBox 的一项时,它完全没有任何作用。有人可以帮我解决这个问题吗?

回答by DC Holmes

The name of the Event Handler you are looking for is Listbox1_DblClick. Try this:

您要查找的事件处理程序的名称是 Listbox1_DblClick。尝试这个:

Private Sub Listbox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  With Me.ListBox1
    For i = 0 To .ListCount - 1
      If .Selected(i) Then
        Me.TextBox1.Value = .List(i, 0)
        Exit For
      End If
    Next
  End With

End Sub