如何检测父窗体的属性何时发生变化? [英] How can I detect when parentform's properties are changing?

查看:57
本文介绍了如何检测父窗体的属性何时发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在处理几个 UserControls ,这次是自定义 TitleBar 。我想做的是检测设计时间 ParentForm的属性(例如 ControlBox )正由开发人员更改 b>然后更新我的 TitleBar



我尝试了什么:



所以我在 UserControl 中添加了计时器来完成工作。它有效,但我认为这不是最合适的方法......

 私有  Sub  Timer_Tick(sender  As   Object ,e  As  EventArgs)句柄 Timer.Tick 
.ControlBox = Me .ParentForm.ControlBox
If ParentForm.ControlBox = True 然后 ControlsBox_FlowLayoutPanel.Visible = True Else ControlsBox_FlowLayoutPanel.Visible = False
如果 ParentForm .MinimizeBox = True 然后 MinimizeButton_PictureBox .Visible = True Else MinimizeButton_PictureBox.Visible = False
Me .MinimizeBox = Me .ParentForm.MinimizeBox
如果 ParentForm.MaximizeBox = True 那么 MaximizeButton_PictureBox.Visible = True Else MaximizeButton_PictureBox.Visible = False
.MaximizeBox = .ParentForm.MaximizeBox
结束 Sub

有没有其他方法可以做到而不是使用定时器?例如,要检测 ParentForm 文本属性,然后更新我的 UserControl 文字,我这样做......

 私人  WithEvents  _ParentForm  As 表格

受保护的 覆盖 Sub OnParentChanged(e As EventArgs)
MyBase .OnParentChanged(e)
_ParentForm = .ParentForm
调用 ParentForm_TextChanged()
结束 Sub

私有 Sub ParentForm_TextChanged()< span class =code-keyword>句柄 _ParentForm.TextChanged
如果 _ParentForm 没什么 然后
FormTitle_Label.Text = FormTitle_Label.Text
否则
FormTitle_Label .Text = _ParentForm.Text
结束 如果
Invalidate()
结束 Sub

ControlBox <等属性是否有类似内容? / b>, MinimizeBox MaximizeBox

解决方案

我不确定你到底想要什么实现。从来没有......而不是使用Timer你可以采取ParentForm的StyleChanged-Event。

你们每个请求的属性(ControlBox,MinimizeBox,MaximizeBox等)正在改变的风格形成并举起这个活动 - 所以你可以在这个新方法中更好地调用你的Timer_Tick-Method的内容...



为了检测ParentForm,我不会使用OnParentChanged-Method ...我会使用OnHandleCreated-Method ......但是结果会是一样的......


这里有一个关于我们如何检测ParentForm的示例属性正在改变,然后做我们的东西...

 私有  WithEvents  _ParentForm  As 表格

受保护 覆盖 Sub OnParentChanged(e 正如 EventArgs)
MyBase .OnParentChanged(e)
_ParentForm = .ParentForm
调用 ParentForm_TextChanged()
调用 ParentForm_StyleChanged()
结束 Sub

私有 Sub ParentForm_TextChanged()句柄 _ParentForm.TextChanged
If _ParentForm IsNot Nothing 然后 FormTitle_Label.Text = _ParentForm.Text
Invalidate()
结束 Sub

私有 Sub ParentForm_StyleChanged()句柄 _ParentForm.StyleChanged
如果 _ParentForm IsNot 然后
如果 _ParentForm.ControlBox = True 然后 ControlsBox_FlowLayoutPanel.Visible = True 其他 ControlsBox_FlowLayoutPanel.Visible = False
如果 _ParentForm.MinimizeBox = True 那么 MinimizeButton_PictureBox.Visible = True Else MinimizeButton_PictureBox.Visible = False
如果 _ParentForm.MaximizeBox = True 然后 MaximizeButton_PictureBox.Visible = True Else MaximizeButton_PictureBox.Visible = False
结束 如果
结束 Sub


I am working on a couple of UserControls lately and this time is a custom TitleBar. What I want to do is to detect when ParentForm's properties (like ControlBox for example) is changing by developer in Design Time and then update my TitleBar.

What I have tried:

So I added a Timer into my UserControl to do this "work". It works, but I think this isn't the most appropriate approach...

Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
	Me.ControlBox = Me.ParentForm.ControlBox
	If ParentForm.ControlBox = True Then ControlsBox_FlowLayoutPanel.Visible = True Else ControlsBox_FlowLayoutPanel.Visible = False
	If ParentForm.MinimizeBox = True Then MinimizeButton_PictureBox.Visible = True Else MinimizeButton_PictureBox.Visible = False
	Me.MinimizeBox = Me.ParentForm.MinimizeBox
	If ParentForm.MaximizeBox = True Then MaximizeButton_PictureBox.Visible = True Else MaximizeButton_PictureBox.Visible = False
	Me.MaximizeBox = Me.ParentForm.MaximizeBox
End Sub

Is there any other way to do that and not by using a Timer? For example, to detect ParentForm's Text property and then update my UserControl's Text, I do something like this...

Private WithEvents _ParentForm As Form

Protected Overrides Sub OnParentChanged(e As EventArgs)
	MyBase.OnParentChanged(e)
	_ParentForm = Me.ParentForm
	Call ParentForm_TextChanged()
End Sub

Private Sub ParentForm_TextChanged() Handles _ParentForm.TextChanged
	If _ParentForm Is Nothing Then
		FormTitle_Label.Text = FormTitle_Label.Text
	Else
		FormTitle_Label.Text = _ParentForm.Text
	End If
	Invalidate()
End Sub

Is there anything similar for properties like ControlBox, MinimizeBox and MaximizeBox?

解决方案

I'm not quiet sure what you exactly trying to achieve. Never the less ... instead of using a Timer you can take the StyleChanged-Event of the ParentForm.
Each of you requested Properties (ControlBox, MinimizeBox, MaximizeBox etc.) are changing the Style of the Form and raise this Event - so you can call the Content of your Timer_Tick-Method better in this new method ...

For detecting the ParentForm I would not use the OnParentChanged-Method ... I would use the OnHandleCreated-Method ... but the result will be the same ...


Well, here is an example on how we can detect when ParentForm's properties are changing and then do our stuff...

Private WithEvents _ParentForm As Form

Protected Overrides Sub OnParentChanged(e As EventArgs)
	MyBase.OnParentChanged(e)
	_ParentForm = Me.ParentForm
	Call ParentForm_TextChanged()
	Call ParentForm_StyleChanged()
End Sub

Private Sub ParentForm_TextChanged() Handles _ParentForm.TextChanged
	If _ParentForm IsNot Nothing Then FormTitle_Label.Text = _ParentForm.Text
	Invalidate()
End Sub

Private Sub ParentForm_StyleChanged() Handles _ParentForm.StyleChanged
	If _ParentForm IsNot Nothing Then
		If _ParentForm.ControlBox = True Then ControlsBox_FlowLayoutPanel.Visible = True Else ControlsBox_FlowLayoutPanel.Visible = False
		If _ParentForm.MinimizeBox = True Then MinimizeButton_PictureBox.Visible = True Else MinimizeButton_PictureBox.Visible = False
		If _ParentForm.MaximizeBox = True Then MaximizeButton_PictureBox.Visible = True Else MaximizeButton_PictureBox.Visible = False
	End If
End Sub


这篇关于如何检测父窗体的属性何时发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆