Powershell winforms在子窗体中引用父窗体

发布于 2025-01-20 19:05:02 字数 1687 浏览 4 评论 0原文

我很难以子女的形式引用父母形式。 为此,我嘲笑了一个小片段。我知道我必须将父属性分配给子女形式,但是到目前为止,我还没有成功。请告知,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2025-01-27 19:05:02

您需要解决两个问题:

  • 您需要使用所有者属性来指定
  • 您尚未分配给 name $ date_label <的 父/拥有表格 /code>控制,so controls ['date_label']无法

通过将父式表单实例分配给所有者属性来解决第一个问题。子女表格:

$kibel = New-Object System.Windows.Forms.Form
$kibel.Text = 'Date'
$kibel.Size = New-Object System.Drawing.Size @(243,230)
$kibel.StartPosition = 'CenterScreen'
# designate owning form
$kibel.Owner = $date

然后,要命名标签,请在定义$ date_label之后添加此标签:

$date_label.Name = 'date_label'

最后,要通过父表正确解析控件,请更改单击事件处理程序以下内容:

$kibel_button.Add_Click({$kibel.Owner.Controls['date_label'].Text ='done'})

Two problems you need to address:

  • You need to use the Owner property to designate the parent/owning form
  • You haven't assigned a name to the $date_label control, so Controls['date_label'] won't resolve to anything

The first problem can be addressed by assigning the parent form instance to the Owner property on the child form:

$kibel = New-Object System.Windows.Forms.Form
$kibel.Text = 'Date'
$kibel.Size = New-Object System.Drawing.Size @(243,230)
$kibel.StartPosition = 'CenterScreen'
# designate owning form
$kibel.Owner = $date

Then, to name the label, add this after defining $date_label:

$date_label.Name = 'date_label'

And finally, to correctly resolve the control via the parent form, change the Click event handler as follows:

$kibel_button.Add_Click({$kibel.Owner.Controls['date_label'].Text ='done'})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文