.NET 4 (WinForms) Dynamic Form Control Creation in Powershell
I am trying to dynamically add form controls to a gui I'm developing in PowerShell. I have looked at VB and C# examples but can't seem to get my form to actually populate the gui with a new control. What I want is to be able to append a potentially large number of additional controls to the form and the added controls get dumped into a scrollable field so they don't end up off the end of the gui. How I tried to accomplish this is append a combobox to a tablelayoutpanel. My idea being, adding a new row to the tablelayoutpanel with a new combobox control would accomplish this. So I suppose I have two questions:
- Is this the logically correct way of going about dynamically adding controls to an object that will allow for scrollable overflow?
- If #1 is true, how do I accomplish this tasK?
This is the most recent iteration of my attempts:
$button1_Click={
$new = New-Object System.Windows.Forms.ComboBox
Add-ComboBox $rowCount
$rowCount++
}
function Add-ComboBox {
param([string] $rowCount)
$co开发者_运维问答mbobox = New-Object System.Windows.Forms.ComboBox
$combobox.Dock = [System.Windows.Forms.DockStyle]::Fill
$combobox.Text = ""
$combobox.Tag = "ComboBox$rowCount"
$tablelayoutpanel1.Controls.Add($combobox, 1, $rowCount)
}
Your assistance is greatly appreciated.
This might be useful, though it's WPF, the snippet creates a window with a few buttons on it that resizes depending on how many buttons are added to the StackPanel
$window = new-object System.Windows.Window
$stackPanel = new-object System.Windows.Controls.StackPanel
$buttonNum = 20
for( $i = 0; $i -lt $buttonNum; $i++ )
{
$button = new-object System.Windows.Controls.Button
$button.Content = "Button Text" + $i
$stackPanel.Children.Add( $button )
}
$scrollViewer = new-object System.Windows.Controls.ScrollViewer
$scrollViewer.Content = $stackPanel
$window.Content = $scrollViewer
$window.SizeToContent = [System.Windows.SizeToContent]::Width
$window.Height = 100
$window.ShowDialog()
I found the answer on the Microsoft Technet forums. A forum moderator has been helping me out with this. Thanks though!
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/b2c8abeb-3ae6-4936-9bef-a50a79fdff45/
精彩评论