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

时间:2021-10-27 20:37:38

I have started writing a Macro in Visual Studio 2005 like this:

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

But I'm completely at a loss as to how to display the ListBox,

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

I'd like behaviour similar to this InputBox example:

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

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

As we can see the InputBox can be constructed and displayed on the screen immediately, returning a String once the box is closed.

正如我们所看到的,InputBox可以立即构建并显示在屏幕上,一旦框关闭就返回一个String。

I tried called the following methods on myListBox after populating it with the Strings in xs, but the ListBox still does not appear of the screen:

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

myListBox.EndUpdate()
myListBox.Show()

I have also tried creating a System.Windows.Forms.Form and adding the ListBox to it, following a similar approach to the one outlined for a button here (under Examples, Visual Basic). Again nothing appears on the form.ShowDialog() call.

我还尝试创建一个System.Windows.Forms.Form并将ListBox添加到它,遵循类似于此处按钮的概述(在Examples,Visual Basic下)。再次没有任何内容出现在form.ShowDialog()调用上。

1 个解决方案

#1


The code below worked fine for me in Visual Studio 2008. The reference to System.Windows.Forms was already in place when I opened up the macros IDE, I simply had to add an Imports System.Windows.Forms at the top of the module.

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

#1


The code below worked fine for me in Visual Studio 2008. The reference to System.Windows.Forms was already in place when I opened up the macros IDE, I simply had to add an Imports System.Windows.Forms at the top of the module.

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