在Visual Studio宏中使用System.Windows.Forms

时间:2009-05-22 11:30:30

标签: visual-studio winforms visual-studio-2005 vbscript macros

我已经开始在Visual Studio 2005中编写一个宏,如下所示:

Public Sub myMacro()
    Dim myListBox As New System.Windows.Forms.ListBox()
    For Each x As String In xs
        myListBox.Items.Add(x)
    Next

但我完全不知道如何显示ListBox

我想要与此InputBox示例类似的行为:

Dim str As String = InputBox("title", "prompt")

我们可以看到InputBox可以立即构建并显示在屏幕上,一旦关闭该框就会返回String

我尝试使用myListBox中的String填充后,在xs上调用了以下方法,但屏幕上仍未显示ListBox

myListBox.EndUpdate()
myListBox.Show()

我还尝试创建System.Windows.Forms.Form并向其添加ListBox,遵循与按钮here (under Examples, Visual Basic)概述的方法类似的方法。 form.ShowDialog()电话上没有任何内容。

1 个答案:

答案 0 :(得分:6)

下面的代码在Visual Studio 2008中对我来说很好。当我打开宏IDE时,System.Windows.Forms的引用已经到位了,我只需要在顶部添加Imports System.Windows.Forms模块。

Public Sub myMacro()

    Dim myListBox As New ListBox
    Dim xs As String() = New String() {"First", "Second", "Third", "Fourth"}

    For Each x As String In xs
        myListBox.Items.Add(x)
    Next

    Dim frm As New Form
    Dim btn As New Button

    btn.Text = "OK"
    btn.DialogResult = DialogResult.OK

    frm.Controls.Add(btn)
    btn.Dock = DockStyle.Bottom

    frm.Controls.Add(myListBox)
    myListBox.Dock = DockStyle.Fill

    If frm.ShowDialog() = DialogResult.OK Then
        MessageBox.Show(myListBox.SelectedItem)
    End If

End Sub