Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...
VB
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...
VB
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?
Posted
Updated 18-Apr-18 23:51pm

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 ...
 
Share this answer
 
Comments
Simos Sigma 19-Apr-18 5:52am    
Thank you very much for your help my friend!!!
Ralf Meier 19-Apr-18 16:05pm    
Parakalo - you're welcome ...
Simos Sigma 20-Apr-18 1:36am    
Oh, "parakalo" I see!!! :) Do you have any connection with Greece?
Ralf Meier 20-Apr-18 16:27pm    
No ... only from holidays ... :(
Simos Sigma 21-Apr-18 2:19am    
You are a part of everything that you have into your heart!!! Summer comes and Greece is still here my friend... ;)
Well, here is an example on how we can detect when ParentForm's properties are changing and then do our stuff...
VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900