Results 1 to 5 of 5

Thread: [RESOLVED] How can I detect when ParentForm's properties are changing?

  1. #1

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Resolved [RESOLVED] How can I detect when ParentForm's properties are changing?

    Greetings to everyone!!!


    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.

    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.net Code:
    1. Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick     Me.ControlBox = Me.ParentForm.ControlBox
    2.     If ParentForm.ControlBox = True Then ControlsBox_FlowLayoutPanel.Visible = True Else ControlsBox_FlowLayoutPanel.Visible = False
    3.     If ParentForm.MinimizeBox = True Then MinimizeButton_PictureBox.Visible = True Else MinimizeButton_PictureBox.Visible = False
    4.     Me.MinimizeBox = Me.ParentForm.MinimizeBox
    5.     If ParentForm.MaximizeBox = True Then MaximizeButton_PictureBox.Visible = True Else MaximizeButton_PictureBox.Visible = False
    6.     Me.MaximizeBox = Me.ParentForm.MaximizeBox
    7. 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.net Code:
    1. Private WithEvents _ParentForm As Form
    2.  
    3. Protected Overrides Sub OnParentChanged(e As EventArgs)
    4.     MyBase.OnParentChanged(e)
    5.     _ParentForm = Me.ParentForm
    6.     Call ParentForm_TextChanged()
    7. End Sub
    8.  
    9. Private Sub ParentForm_TextChanged() Handles _ParentForm.TextChanged
    10.     If _ParentForm Is Nothing Then
    11.         FormTitle_Label.Text = FormTitle_Label.Text
    12.     Else
    13.         FormTitle_Label.Text = _ParentForm.Text
    14.     End If
    15.     Invalidate()
    16. End Sub
    Is there anything similar for properties like ControlBox, MinimizeBox and MaximizeBox?

    Thank you in advance!!!
    Last edited by Simonetos The Greek; Apr 18th, 2018 at 08:19 AM.

  2. #2

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I detect when ParentForm's properties are changing?

    Question is updated!!!
    Last edited by Simonetos The Greek; Apr 18th, 2018 at 08:20 AM.

  3. #3
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: How can I detect when ParentForm's properties are changing?

    Not reliably ... the problem is that there is no changed event for the stuff you listed...

    The property grid uses the PropertyDescriptors to change properties ... so I though I could hook into those in design time...
    However in design the ValueChanged is not triggered???

    Kris

  4. #4

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I detect when ParentForm's properties are changing?

    Well, here is an example on how we can detect when ParentForm's properties are changing and then do our stuff...
    vb.net Code:
    1. Private WithEvents _ParentForm As Form
    2.  
    3. Protected Overrides Sub OnParentChanged(e As EventArgs)
    4.     MyBase.OnParentChanged(e)
    5.     _ParentForm = Me.ParentForm
    6.     Call ParentForm_TextChanged()
    7.     Call ParentForm_StyleChanged()
    8. End Sub
    9.  
    10. Private Sub ParentForm_TextChanged() Handles _ParentForm.TextChanged
    11.     If _ParentForm IsNot Nothing Then FormTitle_Label.Text = _ParentForm.Text
    12.     Invalidate()
    13. End Sub
    14.  
    15. Private Sub ParentForm_StyleChanged() Handles _ParentForm.StyleChanged
    16.     If _ParentForm IsNot Nothing Then
    17.         If _ParentForm.ControlBox = True Then ControlsBox_FlowLayoutPanel.Visible = True Else ControlsBox_FlowLayoutPanel.Visible = False
    18.         If _ParentForm.MinimizeBox = True Then MinimizeButton_PictureBox.Visible = True Else MinimizeButton_PictureBox.Visible = False
    19.         If _ParentForm.MaximizeBox = True Then MaximizeButton_PictureBox.Visible = True Else MaximizeButton_PictureBox.Visible = False
    20.     End If
    21. End Sub

  5. #5
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: How can I detect when ParentForm's properties are changing?

    It seems to me that the above code doesn't do what you claim it does. The user could change the Form's properties such as Text or ControlBox over and over again, but the custom title bar won't change until its OnParentChanged sub is called. And when do you do that? The only way to trigger OnParentChanged in the Designer is to delete the existing custom title bar (if any) and add a new one.

    I think it would be better to do it the reverse way. In other words, define (or override) Text, ControlBox etc. as properties of the custom title bar. You could then use the Set clauses of these properties to change the corresponding parent form properties.

    BB

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width