Context Menu Customization Example for WinForms

Nevron NOV Schedule for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms Schedule examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Context Menu Customization Example for WinForms Description

This example demonstrates how to customize the NOV schedule context menu. A custom command is added at the end of the context menu.


Context Menu Customization Example

C# Context Menu Customization Example Code for Nevron Schedule for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Schedule;
using Nevron.Nov.Schedule.Commands;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NContextMenuCustomizationExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			// Create a simple schedule
			NScheduleViewWithRibbon scheduleViewWithRibbon = new NScheduleViewWithRibbon();
			m_ScheduleView = scheduleViewWithRibbon.View;

			m_ScheduleView.Document.PauseHistoryService();
			try
			{
				InitSchedule(m_ScheduleView.Content);
			}
			finally
			{
				m_ScheduleView.Document.ResumeHistoryService();
			}

			// Add the custom command action to the schedule view's commander
			m_ScheduleView.Commander.Add(new CustomCommandAction());

			// Change the context menu factory to the custom one
			m_ScheduleView.ContextMenu = new CustomContextMenu();

			// Return the commanding widget
			return scheduleViewWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			return null;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to customize the NOV schedule context menu. A custom command is added at the end of the context menu.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Schedule Context Menu", "https://www.nevron.com/docs/openvision/schedule-context-menu.html"), ("Schedule Commanding Overview", "https://www.nevron.com/docs/openvision/schedule-commanding-overview.html"), }; } #endregion #region Implementation private void InitSchedule(NSchedule schedule) { DateTime start = DateTime.Now; // Create an appointment NAppointment appointment = new NAppointment("Meeting", start, start.AddHours(2)); schedule.Appointments.Add(appointment); schedule.ScrollToTime(start.TimeOfDay); } #endregion #region Event Handlers #endregion #region Fields private NScheduleView m_ScheduleView; #endregion #region Static Methods #endregion #region Constants public static readonly NCommand CustomCommand = NCommand.Create(typeof(Nevron.Nov.Examples.Schedule.NContextMenuCustomizationExample), "CustomCommand", "Custom Command"); #endregion #region Nested Types public class CustomContextMenu : NScheduleContextMenu { /// /// Default constructor. /// public CustomContextMenu() { } /// /// Static constructor. /// static CustomContextMenu() { CustomContextMenuSchema = NSchema.Create(typeof(CustomContextMenu), NScheduleContextMenu.NScheduleContextMenuSchema); } protected override void CreateCustomCommands(NMenu menu) { base.CreateCustomCommands(menu); // Create a context menu builder NContextMenuBuilder builder = new NContextMenuBuilder(); // Add a custom command builder.AddMenuItem(menu, Nevron.Nov.Graphics.NImage.FromBytes(Nevron.Nov.NConvert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABsElEQVR4AYWTQ7BjQRSGZz3cjM04Y9u2bdu2bdu29bx83jxzvI79z+munDiVW/Vd/Lf/g0aF0MvyeUJT4ipRTsDLb+ImoQ4zhJifEYjBp2jmfAJWAsmTYP/mN9m+SE08WfsVar7PZuPHcajfRI2M68NFEGn6+3wM6jZSovTRyMAgH9jcgDM5v0/E5+ODUbOBCgeX9oAnfiLccRNxf1c/qV3Z1Ed+B7SjFQHOsiAyplHmSjUUuLZZDoYMemKw1F4eHAjX96AAt7l3MIifIDOZP/k1D5kubuxDCcazxvwTAZyBoouyImUykDCJmMjISeTyA+EAbMbjvf1xaFlP7FrYHTvmS7B7UQ+cWNULj/f0o5bCA+Tyh8jSTKFGU4UGzZXBNCOtWm2FrCYgwB8R4DQL4ue2ud3QpLkGOp0uiGYUZM6YDkDipMAAN0WAOgQYN5XYv0crNGyqhoJMCpUGjZqp0am9HuaP42H9EtRCC94LN1m0EkiahDeHB2LNjC5YOaUzHu3uL6uzBZtfhe7G7KDV+M4rEHH2i0KPQlAlMXga60TW8U5sHuEkXEQBcYFoGDr+P5RkQoMvtntjAAAAAElFTkSuQmCC")), CustomCommand); } /// /// Schema associated with CustomContextMenu. /// public static readonly NSchema CustomContextMenuSchema; } public class CustomCommandAction : NScheduleCommandAction { #region Constructors /// /// Default constructor. /// public CustomCommandAction() { } /// /// Static constructor. /// static CustomCommandAction() { CustomCommandActionSchema = NSchema.Create(typeof(CustomCommandAction), NScheduleCommandAction.NScheduleCommandActionSchema); } #endregion #region Public Overrides /// /// Gets the command associated with this command action. /// /// public override NCommand GetCommand() { return CustomCommand; } /// /// Executes the command action. /// /// /// public override void Execute(NNode target, object parameter) { NScheduleView scheduleView = GetView(target); NMessageBox.Show("Schedule Custom Command executed!", "Custom Command", ENMessageBoxButtons.OK, ENMessageBoxIcon.Information); } #endregion #region Schema /// /// Schema associated with CustomCommandAction. /// public static readonly NSchema CustomCommandActionSchema; #endregion } #endregion } }

Download a fully functional C# (.cs) Context Menu Customization Example for WinForms Solution

VB.NET Context Menu Customization Example Code for Nevron Schedule for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Schedule
Imports Nevron.Nov.Schedule.Commands
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NContextMenuCustomizationExample"

        ' Create the NOV example and its controls
        Dim exampleContent As NWidget = CreateExampleContent()
        Dim exampleControls As NWidget = CreateExampleControls()
        If Not exampleControls Is Nothing Then
            exampleControls.PreferredWidth = 300
        End If

        ' Place the example and its controls in a pair box
        Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
        pairBox.FillMode = ENStackFillMode.First
        pairBox.FitMode = ENStackFitMode.First
        pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

        ' Host the example in the form
        Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create a simple schedule
            Dim scheduleViewWithRibbon As NScheduleViewWithRibbon = New NScheduleViewWithRibbon()
            m_ScheduleView = scheduleViewWithRibbon.View

            m_ScheduleView.Document.PauseHistoryService()
            Try
                InitSchedule(m_ScheduleView.Content)
            Finally
                m_ScheduleView.Document.ResumeHistoryService()
            End Try

            ' Add the custom command action to the schedule view's commander
            m_ScheduleView.Commander.Add(New CustomCommandAction())

            ' Change the context menu factory to the custom one
            m_ScheduleView.ContextMenu = New CustomContextMenu()

            ' Return the commanding widget
            Return scheduleViewWithRibbon
        End Function
        Public Function CreateExampleControls() As NWidget
            Return Nothing
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to customize the NOV schedule context menu. A custom command is added at the end of the context menu.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Schedule Context Menu", "https://www.nevron.com/docs/openvision/schedule-context-menu.html"), ("Schedule Commanding Overview", "https://www.nevron.com/docs/openvision/schedule-commanding-overview.html")} End Function #End Region #Region "Implementation" Private Sub InitSchedule(schedule As NSchedule) Dim start = Date.Now ' Create an appointment Dim appointment As NAppointment = New NAppointment("Meeting", start, start.AddHours(2)) schedule.Appointments.Add(appointment) schedule.ScrollToTime(start.TimeOfDay) End Sub #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_ScheduleView As NScheduleView #End Region #Region "Static Methods" #End Region #Region "Constants" Public Shared ReadOnly CustomCommand As NCommand = NCommand.Create(GetType(NContextMenuCustomizationExample), "CustomCommand", "Custom Command") #End Region #Region "Nested Types" Public Class CustomContextMenu Inherits NScheduleContextMenu ''' ''' Default constructor. ''' Public Sub New() End Sub ''' ''' Static constructor. ''' Shared Sub New() CustomContextMenuSchema = NSchema.Create(GetType(CustomContextMenu), NScheduleContextMenuSchema) End Sub Protected Overrides Sub CreateCustomCommands(menu As NMenu) MyBase.CreateCustomCommands(menu) ' Create a context menu builder Dim builder As NContextMenuBuilder = New NContextMenuBuilder() ' Add a custom command builder.AddMenuItem(menu, Nevron.Nov.Graphics.NImage.FromBytes(Nevron.Nov.NConvert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABsElEQVR4AYWTQ7BjQRSGZz3cjM04Y9u2bdu2bdu29bx83jxzvI79z+munDiVW/Vd/Lf/g0aF0MvyeUJT4ipRTsDLb+ImoQ4zhJifEYjBp2jmfAJWAsmTYP/mN9m+SE08WfsVar7PZuPHcajfRI2M68NFEGn6+3wM6jZSovTRyMAgH9jcgDM5v0/E5+ODUbOBCgeX9oAnfiLccRNxf1c/qV3Z1Ed+B7SjFQHOsiAyplHmSjUUuLZZDoYMemKw1F4eHAjX96AAt7l3MIifIDOZP/k1D5kubuxDCcazxvwTAZyBoouyImUykDCJmMjISeTyA+EAbMbjvf1xaFlP7FrYHTvmS7B7UQ+cWNULj/f0o5bCA+Tyh8jSTKFGU4UGzZXBNCOtWm2FrCYgwB8R4DQL4ue2ud3QpLkGOp0uiGYUZM6YDkDipMAAN0WAOgQYN5XYv0crNGyqhoJMCpUGjZqp0am9HuaP42H9EtRCC94LN1m0EkiahDeHB2LNjC5YOaUzHu3uL6uzBZtfhe7G7KDV+M4rEHH2i0KPQlAlMXga60TW8U5sHuEkXEQBcYFoGDr+P5RkQoMvtntjAAAAAElFTkSuQmCC")), CustomCommand) End Sub ''' ''' Schema associated with CustomContextMenu. ''' Public Shared ReadOnly CustomContextMenuSchema As NSchema End Class Public Class CustomCommandAction Inherits NScheduleCommandAction #Region "Constructors" ''' ''' Default constructor. ''' Public Sub New() End Sub ''' ''' Static constructor. ''' Shared Sub New() CustomCommandActionSchema = NSchema.Create(GetType(CustomCommandAction), NScheduleCommandActionSchema) End Sub #End Region #Region "Public Overrides" ''' ''' Gets the command associated with this command action. ''' ''' Public Overrides Function GetCommand() As NCommand Return CustomCommand End Function ''' ''' Executes the command action. ''' ''' ''' Public Overrides Sub Execute(target As NNode, parameter As Object) Dim scheduleView = GetView(target) NMessageBox.Show("Schedule Custom Command executed!", "Custom Command", ENMessageBoxButtons.OK, ENMessageBoxIcon.Information) End Sub #End Region #Region "Schema" ''' ''' Schema associated with CustomCommandAction. ''' Public Shared ReadOnly CustomCommandActionSchema As NSchema #End Region End Class #End Region End Class

Download a fully functional VB.NET (.vb) Context Menu Customization Example for WinForms Solution