2009-05-22 122 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