我使用 Windows 窗体创建了一个 gui,它允许您单击“添加选项卡”按钮,它会创建一个带有 datagridview 的新选项卡。
当我逐步执行选项卡创建代码时,它每次都有效,但是当我在没有断点的情况下正常运行程序时,似乎无法将选项卡页添加到选项卡控件。
我在绘制功能的第 1 步得到错误
'The property 'Controls' cannot be found on this object. Verify that the property exists.'
我猜这可能是控件被添加到选项卡的速度太快了,因为它在单步执行时可以工作,但我有点不知所措。我尝试在之前和之后添加一个睡眠,这工作了一段时间,但现在它总是失败。
我省略了创建初始标签页、标签控件 ($maintab) 和数据网格的代码,但代码如下:
$tabCount = 0
$tabPage = @()
$datagrid = @()
function paint($form, $ctrl, $TablIndex, $name, $Text, $x, $y, $Width, $Height){
try{$form.Controls.Add($ctrl) }catch{write-host step 1 error: $_}
try{$ctrl.TabIndex = $TablIndex }catch{write-host step 2 error: $_}
try{$ctrl.Text = $Text }catch{write-host step 3 error: $_}
try{$ctrl.name = $name }catch{write-host step 4 error: $_}
try{$ctrl.Location = System_Drawing_Point $x $y }catch{write-host step 5 error: $_}
try{$ctrl.size = System_Drawing_Size $Width $Height }catch{write-host step 6 error: $_}
try{$ctrl.DataBindings.DefaultDataSourceUpdateMode = 0 }catch{write-host step 7 error: $_}
$ctrl
}
function System_Drawing_Point($x, $Y) {$_ = New-Object System.Drawing.Point; $_.x = $X; $_.Y = $Y; $_}
function System_Drawing_Size( $Width, $Height){$_ = New-Object System.Drawing.Size; $_.Width = $Width; $_.Height = $Height; $_}
function add-tab
{
$tabcount ++
$tabpage += (paint $mainTab (New-Object System.Windows.Forms.TabPage) $null ('tabpage' + $tabcount) $null $null $null $null $null )
$datagrid += (paint $tabpage[$tabcount] (New-Object System.Windows.Forms.DataGridView) $null ('datagrid' + $tabcount) $Null 10 10 ($width-60) ($height-800))
}
编辑:我尝试在创建新选项卡之前放置 $maintab.refresh() ,这似乎工作了一段时间,但现在它又停止工作了。
我还发现您可以通过管道将命令传递给 wait-process 以使 powershell 等到一个进程完成,所以我也尝试了这个:
$maintab.refresh() | wait-process
当您可以逐步解决问题并找到问题时,这还不错,但是当我逐步解决问题时,它每次都有效!