Powershell winforms在子窗体中引用父窗体
我很难以子女的形式引用父母形式。 为此,我嘲笑了一个小片段。我知道我必须将父属性分配给子女形式,但是到目前为止,我还没有成功。请告知,
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#### PARENT FORM
$date = New-Object System.Windows.Forms.Form
$date.Text = 'Date'
$date.Size = New-Object System.Drawing.Size @(243,230)
$date.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKbutton.Add_Click({$kibel.showdialog()})
$date.Controls.Add($OKButton)
$date_label = New-Object System.Windows.Forms.Label
$date_label.Location = New-Object System.Drawing.Point(75,70)
$date_label.Size = New-Object System.Drawing.Size(75,23)
$date_label.BorderStyle = 'Fixed3D'
$date_label.Add_Click({$kibel.showdialog()})
$date.Controls.Add($date_label)
##### CHILD FORM
$kibel = New-Object System.Windows.Forms.Form
$kibel.Text = 'Date'
$kibel.Size = New-Object System.Drawing.Size @(243,230)
$kibel.StartPosition = 'CenterScreen'
$kibel_texbox= New-Object System.Windows.Forms.TextBox
$kibel_texbox.Location = New-Object System.Drawing.Point(75,70)
$kibel_texbox.Size = New-Object System.Drawing.Size(75,23)
$kibel.Controls.Add($kibel_texbox)
$kibel_button = New-Object System.Windows.Forms.Button
$kibel_button.Location = New-Object System.Drawing.Point(75,120)
$kibel_button.Size = New-Object System.Drawing.Size(75,23)
$kibel_button.Text = 'KIBEL'
$kibel_button.Add_Click({$kibel.Parent.Controls['date_label'].Text ='done'})
$kibel.Controls.Add($kibel_button)
$date.ShowDialog()
因为现在我已经获得无法将其索引到null数组
错误
I'm having a difficulty referencing parent form in child form.
I've mocked up a little snippet for this purpose. I know that I have to assign parent property to child form, but I have not had success so far. Please advise
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#### PARENT FORM
$date = New-Object System.Windows.Forms.Form
$date.Text = 'Date'
$date.Size = New-Object System.Drawing.Size @(243,230)
$date.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKbutton.Add_Click({$kibel.showdialog()})
$date.Controls.Add($OKButton)
$date_label = New-Object System.Windows.Forms.Label
$date_label.Location = New-Object System.Drawing.Point(75,70)
$date_label.Size = New-Object System.Drawing.Size(75,23)
$date_label.BorderStyle = 'Fixed3D'
$date_label.Add_Click({$kibel.showdialog()})
$date.Controls.Add($date_label)
##### CHILD FORM
$kibel = New-Object System.Windows.Forms.Form
$kibel.Text = 'Date'
$kibel.Size = New-Object System.Drawing.Size @(243,230)
$kibel.StartPosition = 'CenterScreen'
$kibel_texbox= New-Object System.Windows.Forms.TextBox
$kibel_texbox.Location = New-Object System.Drawing.Point(75,70)
$kibel_texbox.Size = New-Object System.Drawing.Size(75,23)
$kibel.Controls.Add($kibel_texbox)
$kibel_button = New-Object System.Windows.Forms.Button
$kibel_button.Location = New-Object System.Drawing.Point(75,120)
$kibel_button.Size = New-Object System.Drawing.Size(75,23)
$kibel_button.Text = 'KIBEL'
$kibel_button.Add_Click({$kibel.Parent.Controls['date_label'].Text ='done'})
$kibel.Controls.Add($kibel_button)
$date.ShowDialog()
As it is now I am getting Cannot index into a null array
error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要解决两个问题:
所有者
属性来指定$ date_label <的 父/拥有表格 /code>控制,so
controls ['date_label']
无法通过将父式表单实例分配给
所有者
属性来解决第一个问题。子女表格:然后,要命名标签,请在定义
$ date_label
之后添加此标签:最后,要通过父表正确解析控件,请更改
单击
事件处理程序以下内容:Two problems you need to address:
Owner
property to designate the parent/owning form$date_label
control, soControls['date_label']
won't resolve to anythingThe first problem can be addressed by assigning the parent form instance to the
Owner
property on the child form:Then, to name the label, add this after defining
$date_label
:And finally, to correctly resolve the control via the parent form, change the
Click
event handler as follows: