2009-05-22 121 views
1

使用System.Windows.Forms的我已经开始写这样的Visual Studio 2005中宏:在Visual Studio中宏

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以下方法xsString小号填充它之后,但在ListBox仍然没有出现在屏幕的:

myListBox.EndUpdate() 
myListBox.Show() 

我还试图建立一个System.Windows.Forms.Form并添加按照与按钮here (under Examples, Visual Basic)的概述类似的方法,向其输入ListBoxform.ShowDialog()电话再次没有出现。

+0

您是否添加了对System.Windows.Forms程序集的引用?顺便说一句,我想你会需要详细说明,因为我真的不能看到ListBox和InputBox提示之间的任何关系。 – Cerebrus 2009-05-22 11:55:34

回答

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