【问题标题】:Using System.Windows.Forms in a Visual Studio Macro在 Visual Studio 宏中使用 System.Windows.Forms
【发布时间】:2009-05-22 11:30:29
【问题描述】:

我已经开始在 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

xs 中填充Strings 后,我尝试在myListBox 上调用以下方法,但屏幕上仍然没有出现ListBox

myListBox.EndUpdate()
myListBox.Show()

我还尝试创建一个System.Windows.Forms.Form 并将ListBox 添加到其中,其方法与为按钮here (under Examples, Visual Basic) 概述的方法类似。在form.ShowDialog() 调用上再次没有出现任何内容。

【问题讨论】:

  • 您是否添加了对 System.Windows.Forms 程序集的引用?顺便说一句,我认为您需要详细说明,因为我真的看不出 ListBox 和 InputBox 提示之间有任何关系。
  • 我没有添加参考,但现在我有,所以感谢您的建议。不幸的是 ListBox 仍然没有显示。为了澄清,我需要的与 InputBox 的关系只是它被显示!目前我找不到在屏幕上显示 ListBox 的方法。我已经编辑了我的问题以澄清这一点。

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


【解决方案1】:

下面的代码在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多