【问题标题】:.NET 4 (WinForms) Dynamic Form Control Creation in Powershell.NET 4 (WinForms) 在 Powershell 中创建动态表单控件
【发布时间】:2011-06-16 02:37:50
【问题描述】:

我正在尝试将表单控件动态添加到我在 PowerShell 中开发的 gui。我查看了 VB 和 C# 示例,但似乎无法让我的表单实际使用新控件填充 gui。我想要的是能够将大量潜在的附加控件附加到表单中,并且添加的控件被转储到可滚动字段中,这样它们就不会出现在 gui 的末尾。我试图做到这一点的方法是将组合框附加到 tablelayoutpanel。我的想法是,使用新的组合框控件向 tablelayoutpanel 添加新行将完成此操作。所以我想我有两个问题:

  1. 这是将控件动态添加到允许可滚动溢出的对象的逻辑正确方法吗?
  2. 如果 #1 为真,我该如何完成这项任务?

这是我最近的一次尝试:

$button1_Click={
    $new = New-Object System.Windows.Forms.ComboBox
    Add-ComboBox $rowCount
    $rowCount++
}

function Add-ComboBox {
    param([string] $rowCount)
    $combobox = 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)
}

非常感谢您的帮助。

【问题讨论】:

    标签: winforms powershell


    【解决方案1】:

    这可能很有用,虽然它是 WPF,但 sn-p 会创建一个窗口,上面有几个按钮,这些按钮会根据添加到 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()
    

    【讨论】:

    • 我在 Windows 7 上使用 Windows PowerShell ISE 2.0
    • 而您只使用此代码来完成我所描述的内容??当我运行代码时,我收到有关在 Null 对象上调用方法的错误。
    【解决方案2】:

    我在 Microsoft Technet 论坛上找到了答案。论坛版主一直在帮助我解决这个问题。不过还是谢谢!

    http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/b2c8abeb-3ae6-4936-9bef-a50a79fdff45/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      相关资源
      最近更新 更多