Using System.Windows.Forms in Visual Studio Macro

I 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 don't understand at all how to display ListBox

,

I like behavior like this InputBox example:

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

      

As we can see, it InputBox

can be constructed and displayed on the screen immediately, returning String

after the window is closed.

I tried to call the following methods on myListBox

after filling it String

in xs

, but it ListBox

still doesn't appear on the screen:

myListBox.EndUpdate()
myListBox.Show()

      

I also tried to create System.Windows.Forms.Form

and add to it ListBox

following a similar approach to the description for the button here (in the Examples section, Visual Basic) . Again nothing appears in the call form.ShowDialog()

.

+1


a source to share


1 answer


The code below worked fine for me in Visual Studio 2008. The link to System.Windows.Forms

was already in place when I opened the IDE macro, I just had to add 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

      

+6


a source







All Articles