views:

421

answers:

1

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

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,

I'd like behaviour similar to this InputBox example:

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.

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:

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.

+1  A: 

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.

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
KevB
Hi Kev, sorry I haven't had a chance to check/accept this yet..I haven't forgotten you :)
Dave Tapley