Ticket T594971
Visible to All Users

How to inherit font from parent?

created 7 years ago (modified 7 years ago)

I have a custom HyperLinkEdit inherited from HyperLinkEdit. I know DE controls dont inherit font from parent. But here I do need to. How do I say this control always have to inherit font from parent with minimal code?

I tried

C#
protected override void OnParentFontChanged(EventArgs e) { base.OnParentFontChanged(e); Font = new Font(Parent.Font.FontFamily, Parent.Font.Size, Parent.Font.Style | FontStyle.Underline); }

The problem here is this is hit only if Parent's font is explicitly set. It doesnt get called when Parent has a default font.

I also tried

C#
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Font = new Font(Parent.Font.FontFamily, Parent.Font.Size, Parent.Font.Style | FontStyle.Underline); base.OnPaint(e); }

But this causes a freeze effect and things are painted correctly only if I drag and move the form. Screenshot: https://pasteboard.co/H2rTRvL.png

Now my custom control could be in inside a user control which could be inside form etc. I just want my control to have font from its immediate parent. Parent can have default font or custom font set programmatically or in designer. What would be the correct event or override?

I prefer not to make use of style controller because that means I got to add style controller in every control my HyperLinkEdit is going to be used. I prefer a solution where I only have to make change in my own control class.

Answers approved by DevExpress Support

created 7 years ago (modified 7 years ago)

Hello Nawfal,
To achieve the required behavior, you can override the InitLayout method of your HyperLinkEdit descendant as follows:

C#
protected override void InitLayout() { base.InitLayout(); if (Parent != null) Font = new Font(Parent.Font.FontFamily, Parent.Font.Size, Parent.Font.Style | FontStyle.Underline); }

This will set your control's Font on initialization. Also, as you mentioned, you need to override the OnParentFontChanged event as follows:

C#
protected override void OnParentFontChanged(EventArgs e) { base.OnParentFontChanged(e); Font = new Font(Parent.Font.FontFamily, Parent.Font.Size, Parent.Font.Style | FontStyle.Underline); }

This code will change your control's Font accordingly with its Parent's Font.
Let me know if this solution is helpful.

    Show previous comments (11)
      1. In the sample you provided, when sub-class-ing the RepositoryItem, why do you do:
      C#
      public override void Assign(RepositoryItem item) { BeginUpdate(); try { base.Assign(item); } finally { EndUpdate(); } }

      It is essentially doing nothing right? Can I remove it?

      1. Also, since the sub-classes of ViewInfo and Painter are not doing anything can I totally remove them and do this when registering?
      C#
      public static void RegisterCustomControl() { Image img = null; EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo( MyHyperLinkEdit.EditName, typeof(MyHyperLinkEdit), typeof(RepositoryItemMyHyperLinkEdit), typeof(HyperLinkEditViewInfo), // <-- notice here that I provide DE MyHyperLinkEdit new HyperLinkEditPainter(), // <-- notice here that I provide DE MyHyperLinkEdit true, img)); }

      Is my approach OK then?

        For God sake allow editing of comments!

        Dima (DevExpress Support) 7 years ago

          Hello Nawfal,
          The Assign method override is needed only if you add custom properties or events to the RepositoryItem descendant.
          The Painter and ViewInfo descendants are also optional. If you don't use them, you can safely remove them and use the HyperLinkEditViewInfo and HyperLinkEditPainter classes.
          As for your suggestion to provide the capability to edit comments, we will definitely consider implementing this feature.
          Should you have any further questions, do not hesitate to contact us.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.