User Interface / Widgets / Custom Widgets
In This Topic
    Custom Widgets
    In This Topic

    Nevron Open Vision makes it easy to create custom widgets when you want to extend the functionality of any of the existing widgets. To do that, create a class that inherits from the widget you want to extend and override the methods you want to change. For example, to paint some custom objects on a widget, you can override the OnPrePaint method of the widget.

    The following code demonstrates how to create a custom label widget that inherits from NLabel and paints an ellipse occupying 1/3rd of the label bounds (content edge):

    Create a custom label widget
    Copy Code
    public class CustomLabel : NLabel
    {
        #region Constructors
    
        /// <summary>
        /// Default constructor.
        /// </summary>
        public CustomLabel()
        {
        }
        /// <summary>
        /// Initializing constructor.
        /// </summary>
        /// <param name="text"></param>
        public CustomLabel(string text)
        {
        }
    
        /// <summary>
        /// Static constructor.
        /// </summary>
        static CustomLabel()
        {
            CustomLabelSchema = NSchema.Create(typeof(CustomLabel), NLabelSchema);
        }
    
        #endregion
    
        #region Protected Overrides - Paint
    
        protected override void OnPrePaint(NPaintVisitor visitor)
        {
            // Paint an ellipse that occupies 1/3rd of the label bounds
            NRectangle bounds = GetContentEdge();
            visitor.SetFill(new NHatchFill(ENHatchStyle.Sphere, NColor.Red, NColor.White));
            visitor.PaintEllipse(bounds.CenterX - bounds.Width / 6, bounds.X + 20, bounds.Width / 3, bounds.Height / 3);
    
            base.OnPrePaint(visitor);
        }
    
        #endregion
    
        #region Schema
    
        /// <summary>
        /// Schema associated with CustomLabel.
        /// </summary>
        public static readonly NSchema CustomLabelSchema;
    
        #endregion
    }
    

    You can then create a custom label and configure it:

    Configure the custom label
    Copy Code
    CustomLabel label = new CustomLabel("Hello World from Nevron Open Vision");
    label.TextAlignment = ENContentAlignment.MiddleCenter;
    label.PreferredSize = new NSize(400, 200);
    label.SetBorder(2, NColor.Red);
    

    This code will result in the following widget:

    See Also